Home > OS >  Check whether the order is in the shopping cart based on the order ID
Check whether the order is in the shopping cart based on the order ID

Time:10-16

When an order is pending payment, The same order remains in the cart, How to check if the same order remains in the cart or not? For example, based on the order ID, check whether the order is in the shopping cart.

I want to display a message on order-pay page if the same order ID is still in the cart

CodePudding user response:

I had a little trouble understanding what you need, because WC_Cart objects essentially don't contain WC_Order objects in any form.

If you need to detect on order-pay page whether the current cart is associated to that specific order, you can call and compare get_cart_hash() values of WC_Order object with the cart hash value from WooCommerce session.

You can do such comparison in order-pay template like this:

$order_cart_hash = $order->get_cart_hash();
$session_cart_hash = WC()->cart->get_cart_hash();

if( $order_cart_hash === $session_cart_hash ) {
    // Yep, current cart is associated to this unpaid order
}

Note: This comparison is safe. If the customer updates the cart in any way, session cart hash is altered. And if he then change the cart back to it's original state, the hash values will match again.

  • Related