I wish to have a Quick Buy (Flipkart like) button on product page. Clicking on this button adds the selected product to cart and gets the user to the checkout (not on the cart page). Also note here that the user is not redirected to the cart page and takes the user in the checkout process as far as possible. I also do not want to replace default âadd to cartâ button, instead, I want to add another button that says Quickbuy. Can you help me with this?
What I have figured out so far?
Well, I have tried and create a URL structure that add products to cart and goes to checkout, something like this.
https://www.similarfeathers.com/checkout/?add-to-cart=6711&variation_id=6714&attribute_pa_size=M
(6711 being the product ID, 6714 being that variable ID, M being that size (attribute)).
Now, I want to add this hyperlink, that gets generated dynamically on the product page, takes the value of size attribute from the dropdown and get variationID. When a user will click this URL, my job will be done.
Any way to create the custom hyperlink URL on the product page?
All the required data to craft the link you are looking for is available on the single page in the form of hidden fields.
<input type="hidden" value="6438" name="add-to-cart">
<input type="hidden" value="6438" name="product_id">
<input type="hidden" value="6442" name="variation_id">
The value for
attribute_pa_size
can be grabbed from the select box.You need to add the quick buy button, and on its click event write some code to craft the URL and redirect to it.
Some quickly made up code presuming that you create the button with
id="quick-buy"
global $product;
After searching for a long time I came up with this solution. It may not be the best way but it works and its simple. You can do a quick buy on product variations with Javascript…
First I added a secondary button next to my add to cart button
Instant Buy
I used the hidden input that is standard for all Woocommerce.
<input type="hidden" name="variation_id" id="variation_num" class="variation_id" value="15959">
I added the id (id=”variation_num”) to the input which is located in /wp-content/themes/YOUR THEME/woocommerce/single-product/add-to-cart/variation-add-to-cart.php
Then at the bottom of this same variation-add-to-cart.php file I added my javascript. First get the value of the hidden input with the id we just added (var sel = document.getElementById(‘variation_num’);)
If you have a option selected by default you need to select hidden input value on page load and update your link url by id (quick-buy)
Next you need a function to update url when option is selected or changed…
So in total you will have the following..
Wala! I hope this saves others the time I wasted looking for a more complicated solution.
Also for simple products you will want to edit..
/wp-content/themes/YOUR THEME/woocommerce/single-product/add-to-cart/simple.php
Add your new link / button, I used the same as on the variation products. As for the url I used php.
Your button: