0

Hi I have a similar problem to

How to remove woocommerce added cart items and redirect to checkout?

On product page When I click on add to cart I always get this error "You cannot add another [Product] in the cart woocommerce"

I tried the solution posted by LoicTheAztec

1) Empty cart before add-to-cart (if cart is not empty)

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;
}

2) Add-to-cart redirection to checkout:

add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 10, 1 );
function add_to_cart_checkout_redirection( $url ) {
    return wc_get_checkout_url();
}

3) Skip cart page redirecting to checkout:

add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
    if( is_cart() )
        wp_redirect( wc_get_checkout_url() );
}

This solution work and solve my problem but the first snippet delete all other product that I already have in the basket, I should only reset the current product, not the entire cart. How can I fix the problem?

1 Answers1

0

Try this code in the first snippet, it will check if the cart has the product and then redirect if found here:

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;
        }
    }
}
Heba Fareed
  • 437
  • 2
  • 6
  • I tried and give me "Fatal error: Uncaught Error: Call to a member function get_id() on null" on line "$id = $product->get_id();" – Simone Gatti Dec 11 '19 at 15:22
  • I have edited the code, please replace it and try again – Heba Fareed Dec 11 '19 at 15:27
  • Uncaught Error: Call to a member function get_id() on boolean on the same line "$id=$product->get_id();" – Simone Gatti Dec 11 '19 at 15:36
  • Oh, I found that the product id is included in parameters already, I have edited the code now. please try again :) Sorry about that! – Heba Fareed Dec 11 '19 at 15:45
  • Thanks for helping me, I've been beating my head for 2 days. Now the code has no errors but it only eliminates the product, when I click on "add to cart" for a product that I already had in the cart it just deletes it. I think that to make it work I have to cancel the product and immediately add it back to the cart, so I don't get the error "your product is already in the cart" but how do I do this? Thank you very much for helping me – Simone Gatti Dec 11 '19 at 15:55
  • I see, please try the edited code, it adds the product after removing. – Heba Fareed Dec 11 '19 at 16:09
  • I also tried to add that but it doesn't work :( it doesn't add the product – Simone Gatti Dec 11 '19 at 16:17
  • Ok, let's check the root problem, you need to get rid of "You cannot add another [Product] in the cart woocommerce" error. regardless of the error and the above code, does it add the product? – Heba Fareed Dec 11 '19 at 16:21
  • I think I figured out what you need, I have edited the code to proceed to checkout if the product exists or add product if not. – Heba Fareed Dec 11 '19 at 16:29
  • Maybe I didn't explain myself well. The code I posted does what I want. I'm selling "single digital products". They can be added only once to the cart otherwise woocommerce shows that error "you cannot add the product to the cart twice." I want that when the user adds the product the second time does not show that error and go immediately to the checkout. The snippet that i posted empties the cart before redirecting and works because the cart is empty, but the problem is that I want to keep the products that the user has already added to the cart and go to the checkout without errors. :( – Simone Gatti Dec 11 '19 at 16:56
  • Yes, I have edited it now. Please check and let me know the feedback :) – Heba Fareed Dec 11 '19 at 16:58