wc_clear_cart_after_payment()
Clear cart after payment.
Hooks from the function
Return
null
. Nothing (null).
Usage
wc_clear_cart_after_payment();
wc_clear_cart_after_payment() wc clear cart after payment code WC 9.6.1
function wc_clear_cart_after_payment() { global $wp; $should_clear_cart_after_payment = false; $after_payment = false; // If the order has been received, clear the cart. if ( ! empty( $wp->query_vars['order-received'] ) ) { $order_id = absint( $wp->query_vars['order-received'] ); $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok. if ( $order_id > 0 ) { $order = wc_get_order( $order_id ); if ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) { $should_clear_cart_after_payment = true; $after_payment = true; } } } // If the order is awaiting payment, and we haven't already decided to clear the cart, check the order status. if ( is_object( WC()->session ) && WC()->session->order_awaiting_payment > 0 && ! $should_clear_cart_after_payment ) { $order = wc_get_order( WC()->session->order_awaiting_payment ); if ( $order instanceof WC_Order && $order->get_id() > 0 ) { // If the order status is neither pending, failed, nor cancelled, the order must have gone through. $should_clear_cart_after_payment = ! $order->has_status( array( OrderStatus::FAILED, OrderStatus::PENDING, OrderStatus::CANCELLED ) ); $after_payment = true; } } // If it doesn't look like a payment happened, bail early. if ( ! $after_payment ) { return; } /** * Determine whether the cart should be cleared after payment. * * @since 9.3.0 * @param bool $should_clear_cart_after_payment Whether the cart should be cleared after payment. */ $should_clear_cart_after_payment = apply_filters( 'woocommerce_should_clear_cart_after_payment', $should_clear_cart_after_payment ); if ( $should_clear_cart_after_payment ) { WC()->cart->empty_cart(); } }