Skip to content

Instantly share code, notes, and snippets.

@elias1435
Created November 20, 2022 05:51
Show Gist options
  • Save elias1435/fbcf88186303c5122ed6110d3aecbf55 to your computer and use it in GitHub Desktop.
Save elias1435/fbcf88186303c5122ed6110d3aecbf55 to your computer and use it in GitHub Desktop.
Empty Woocommerce Cart before adding new item | only 1 product add to cart woocommerce, Code goes in function.php file of your active child theme (or theme)
<?php
// before addto cart, only allow 1 item in a cart
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment