• Hi,

    Im selling a product to sold individually. So if user tried to add the product again to their cart, the notice message pops out that says “View Cart, You cannot add another product to your cart”.

    Is it possible for that statement to redirect to another page once it occur? I want to redirect it to the checkout page.

    Please advice.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • D Smith a11n

    (@dsmithweb)

    Automattic Happiness Engineer

    Hi Julius!

    You can do this, although it’s slightly complicated. That statement & functionality is in the file woocommerce/includes/class-wc-cart.php — specifically, at line 919:

    
    	// Force quantity to 1 if sold individually and check for existing item in cart
    			if ( $product_data->is_sold_individually() ) {
    				$quantity         = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
    				$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
    				if ( $in_cart_quantity > 0 ) {
    					throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another &quot;%s&quot; to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
    				}
    			}
    

    That is where the behavior around that action occurs, and is where you’d insert any other behavior you wanted (such as redirecting to the checkout page).

    Thread Starter Julz

    (@juliziz12)

    Thanks D Smith,

    I will try to play around with it.

    My other option is to empty the cart once any page loads or visited.

    I tried to use this function, I found in the web.

    add_action( 'init', 'woocommerce_clear_cart_url' );
    function woocommerce_clear_cart_url() {
    global $woocommerce;
    if ( isset( $_GET['empty-cart'] ) ) {
    $woocommerce->cart->empty_cart(); 
    }
    }

    However, the code above only triggers when you visit the homepage. For some instance I used a function is_page() but it seems doesn’t to work.

    Anyone had the same experience? Any advice would be appreciated.

    Thanks

    Hello,
    Have you found a solution? I’m stuck on this..

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How Redirect “View Cart, You cannot add another product to your cart”’ is closed to new replies.