Mud Kip
Mud Kip

Reputation: 67

woocommerce product addon - empty cart upon going to product page

Right now I am trying to get it so that whenever someone gets to my site's product page their cart is automatically emptied.

I am using a woocommerce product addon called "Product & Checkout Options for WooCommerce" that allows me to use radiobuttons/checkboxes for my products, I don't know if that will alter any of the code.

I've tried php code such as this but it hasn't worked:

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart' , 10, 1);

function only_one_in_cart( $cart_item_data ) {
   global $woocommerce;
   $woocommerce->cart->empty_cart();
   unset($cart_item_data['product_meta']);
   return true;
}

Upvotes: 0

Views: 237

Answers (1)

Skatox
Skatox

Reputation: 4284

It's better to add the hook on your single product page, do it with the action woocommerce_before_single_product:

add_action( 'woocommerce_before_single_product', `only_one_in );
function only_one_in_cart() {
   global $woocommerce;
   $woocommerce->cart->empty_cart();
}

This will empty your cart each time you visit the page, if it's late, then you can add the function into the wp_head hook and validate if you are in the product page by is_product():

add_action( 'wp_head', `only_one_in );
function only_one_in_cart() {
   if ( is_product() ){
      global $woocommerce;
      $woocommerce->cart->empty_cart();
   }
}

Upvotes: 0

Related Questions