Clearing the Cart After Placing an Order on WooCommerce: A PHP Code Snippet Guide


In this guide, we’ll show you how to clear the cart automatically after placing an order on WooCommerce using a PHP code snippet. By implementing this code snippet in the appropriate location, you can ensure a clean slate for your customers, promoting an organized and hassle-free shopping experience.

function clear_cart_on_order_creation( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    WC()->cart->empty_cart();
}
add_action( 'woocommerce_new_order', 'clear_cart_on_order_creation', 10, 1 );

You can add the PHP Snippet on Theme’s functions.php file or you can create a plugin to clear the cart when order is created.

,

Leave a Reply

Your email address will not be published. Required fields are marked *