skip to Main Content

Objective:

I would like the customer to click on a button, add an item with quantity = 1 to cart, and route to checkout page automatically.

What I did:

I’m using Elementor to add a button with a href value of:

https://fakeurl.com/checkout/?add-to-cart=59

Problem:

Once I click the button, it will route to the checkout page, however it will add 2 quantity instead of one to the cart.

enter image description here

What I’ve tried:

Explicitly specify the quantity count in the href:

https://fakeurl.com/checkout?add-to-cart=59&quantity=1

But I’m getting the same results.

My checkout page is just simple page with 2 shortcodes namely woocommerce_cart & woocommerce_checkout:

enter image description here

Any idea why? Do I need to empty the cart before the aforementioned button is pressed?

3

Answers


  1. Use your link structure as you already do > ?add-to-cart=59&quantity=1 and add below code in functions.php in your theme to just do checking

    the only thing this peace of code do is to loop your cart to see if this product is already there .. and if it is – it sets $valid var on false

    function is_product_in_cart( $valid, $product_id, $quantity) {
        global $woocommerce;
        if($woocommerce->cart->cart_contents_count == 0) return true;
        foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
            $_product = $values['data'];
            $id = $_product->id ;
            if( $product_id == $id )  $valid = false;
        }
        return $valid;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'is_product_in_cart', 10, 3 );
    
    Login or Signup to reply.
  2. Woocommerce default flow is that it will add the quantity to the cart whenever you add an item that is already in the cart.

    1. Empty your cart whenever a new product is added to the cart so that only one remains in the cart.
    Login or Signup to reply.
  3. In most cases the quantity is doubled because you are being redirected.
    So the quantity is added and after the redirect the quantity is added again.

    The reasons for this could be a couple of the things.

    1. Theme or wordpress permalinks setting are adding or removing // in the links
    2. Your checkout page is not set in woocommerce settings (woocommerce->advanced settings->Page settings)

    How to check if you are being redirected (chrome)
    At the top of Chrome’s inspector (in the Network tab) is a checkbox which says Preserve log. Enable this option. Now it doesn’t matter at all how the page navigates, the inspector will keep all log history — including the redirect response.
    (found here: See full redirect path and HTTP status code in Chrome)

    Possible solutions

    1. “Enable AJAX add to cart buttons on archives” (WooCommerce –> Settings –> Products -> General)
      found here (https://www.businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/)
      This sometimes help when having custom pages for cart and checkout.
    2. Disable "Redirect to the cart page after successful addition" under WooCommerce > Settings > Products.
      Because you are linking to the checkout page, this wil redirect you again to the cart page.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search