I want to make an “Add To Cart” button on a product page that would work with AJAX. How can I do it? When I add to cart on a product page – it refreshes the page, how can I make it work by AJAX?
The “Add to cart” button on “Quick View” on archive works by ajax – and it’s great, but how can I do the same on product page?
I want to click on “Take me Home” on the product page which would then
add the product with the selected attributes to my cart by ajax and will open that cart (like when you hover onto the bag image on menu) and shakes the bag image.
Just add the following attributes to the Add to Cart button to enable the Ajax button.
The
ajax_add_to_cart add_to_cart_button
classes, and thedata-product_id="<?php echo get_the_ID(); ?>" data-product_sku="<?php echo esc_attr($sku) ?>"
attributes are required.No need to apply any action or filter.
We can use ajax from archive page. it’s easy –
Remove old button which submiting form:
Add ajax-link from archive page to single product page:
P.S. JS Callback. For example you can show popup with links “Back to shop” and “Cart” or “Checkout”
Please start by reading this page:
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
First you need to add some code to your functions.php for example:
Then you have to add some javascript code that triggers the add to cart and makes a call to the function:
This is just an example of how it can be done. Although its very basic. This javascript checks for links with the classname .add-to-cart and checks the rel attribute for the corresponding product. It then sends the product id to the php class. There you need to add code to add the corresponding product to the cart.
I suggest you search some more about the topic to make it suit your needs. Good luck.
Woocommerce has come along way. I think the solution is quite easy now. In case I am missing something, all that is required it checking the “Enable AJAX add to cart buttons on archives” and using the
woocommerce_template_loop_add_to_cart()
function.The checkbox option is under Woocommerce > Settings > Products > General.
Settings > Products > General.”>
Then simply use woocommerce_template_loop_add_to_cart() wherever you wish to output the button.
If you are using a custom loop like I was, you need to be sure to make the product global in order for the
woocommerce_template_loop_add_to_cart()
to work.Below is a small example of using the function:
Copy this code into your file. For example: my-theme-wc-single-ajax-add-cart.js.
You may need to replace function, variable prefix
myThemeWc_
to what you want.This code use the original WooCommerce single product page add to cart button but stop its functional and then use ajax instead by remain all the values in the form.
Then enqueue this js file.
You may have to code your css button to make it show the loading, added icon.
Here is css.
You can replicate the behavour of the archives button in your single products
you can see the full tutorial here
https://quadmenu.com/add-to-cart-with-woocommerce-and-ajax-step-by-step/
I used this plugin for the backend part https://wordpress.org/plugins/woo-ajax-add-to-cart/
I’m not using the product page, so I modified the script to work with any button:
info: Tested with WooCommerce 2.4.10.
Hmm, well I did it in another way, used the woocommerce loop from add to cart (woocommerce/templates/loop/add-to-cart.php)
BUT the problem was that it was adding just 1 quantity, in fact, you can see in the code that is listed quantity : 1, so I had problems, until I bumped into this guys who saved me
ps. leaving the 1st part where it adds just 1 product for people who don’t need more than 1 product in the basket, but I added a solution for those who need more than just 1 product added to the cart.
add-to-cart.js
To make ajax woocomerce work on another page you will need.
goes in functions.php
paste this:
in footer.php in the end paste this:
For me working exelent. I think this is the simplest solution with Ajax.
I used Eh Jewel’s answer but then added some custom JS for once the product was added to the cart. This completes the final AJAX process of being able to update the page however you want.
For completeness, here is the HTML code I used (same as Eh Jewel’s):
Then for the custom JS bit: