Add only one product in Woocommerce cart or clear cart

Asked

Viewed 2,325 times

1

Hello, I’m making a subscription club using Woocommerce and I would like it to be possible to buy only one product at a time. I thought maybe clean the cart before adding the new product or even limit the quantity of products.

Does anyone know if it’s possible?

Thank you.

1 answer

1


To clear the cart before adding a new product, you can use the following filter:

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

In that thread do Soen (which is where I got the snippet from above) you can follow a little more in-depth discussion on how to do this, and also how to limit the number of cart items to just 1 (which, in practice, is the same thing).

  • 1

    It worked Caio, thank you very much.

  • @Ronaldothomaz, if the answer was useful to you, do not forget to mark it as correct. Taking advantage, as you are new here, I suggest you make the [tour].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.