mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-01 11:52:25 +08:00
https://stackoverflow.com/questions/49382193/empty-cart-before-add-to-cart-in-woocommerce/49382476
15 lines
480 B
Text
15 lines
480 B
Text
add_action( 'woocommerce_before_calculate_totals', 'keep_only_last_cart_item', 30, 1 );
|
|
function keep_only_last_cart_item( $cart ) {
|
|
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
|
|
return;
|
|
|
|
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
|
|
return;
|
|
|
|
$cart_items = $cart->get_cart();
|
|
|
|
if( count( $cart_items ) > 1 ){
|
|
$cart_item_keys = array_keys( $cart_items );
|
|
$cart->remove_cart_item( reset($cart_item_keys) );
|
|
}
|
|
}
|