Answer the question
In order to leave comments, you need to log in
How to remove item from woocommerce cart automatically?
Tell me guys how to automatically remove goods from the cart in woocommerce? The task is as follows: On the site, the downloaded product is sold individually (in one copy), the settings are set so that when you click on the "Buy" button, the client goes directly to placing an order. But at this moment, the product is added to the cart, which is hidden, and if the client did not place an order, for example, went to another page, then when trying to buy this product again, the message "You cannot add "Product" to the basket" again appears. Or if he chooses another product, then the previously selected product is displayed for payment as it was added to the cart. It seems to have described correctly. It is necessary to remove the product from the cart if the client did not buy it and left the checkout page.
Answer the question
In order to leave comments, you need to log in
1) Empty cart before adding to cart (if cart is not empty) will reset whole cart:
add_filter( 'woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3 );
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty())
WC()->cart->empty_cart();
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3);
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if(! WC()->cart->is_empty()) {
$cartId = WC()->cart->generate_cart_id($product_id);
$cartItemKey = WC()->cart->find_product_in_cart($cartId);
if ($cartItemKey) {
return $passed;
} else {
$woocommerce->cart->add_to_cart( $product_id );
return $passed;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question