Skip to content

Instantly share code, notes, and snippets.

@vishalck
Created August 2, 2017 20:29
Show Gist options
  • Save vishalck/b4edb52b402fcc61748e5cca60f85344 to your computer and use it in GitHub Desktop.
Save vishalck/b4edb52b402fcc61748e5cca60f85344 to your computer and use it in GitHub Desktop.
Empty WooCommerce cart
<?php
/**
* Empties the cart and optionally the persistent cart too.
*
* @param bool $clear_persistent_cart (default: true)
*/
public function empty_cart( $clear_persistent_cart = true ) {
$this->cart_contents = array();
$this->reset( true );
unset( WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart );
if ( $clear_persistent_cart && get_current_user_id() ) {
$this->persistent_cart_destroy();
}
do_action( 'woocommerce_cart_emptied' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment