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.
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:
Any idea why? Do I need to empty the cart before the aforementioned button is pressed?
3
Answers
Use your link structure as you already do >
?add-to-cart=59&quantity=1
and add below code infunctions.php
in your theme to just do checkingthe 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 onfalse
Woocommerce default flow is that it will add the quantity to the cart whenever you add an item that is already in the cart.
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.
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
found here (https://www.businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/)
This sometimes help when having custom pages for cart and checkout.
Because you are linking to the checkout page, this wil redirect you again to the cart page.