Skip to content
Advertisement

Ajax add to cart button for product variation in WooCommerce 3

I have this button here. The use of this button is to add to cart a product has a product id of 237, variation id of 208673, and attribute_pa_option of bluetooth. Is there a way to AJAX this?

JavaScript

Advertisement

Answer

To make it work I use a custom ajax add-to-cart for product variations exclusively.

1). I have first changed a bit your button html:

JavaScript

As you will see I don’t use the button href attribute, as I post the data through ajax.

For your attributes, if you have more than one, you will separate each pair by a coma like:

JavaScript

2). The PHP (WordPress-Ajax) and the jQuery (Ajax) code:

JavaScript

Code goes in function.php file of your active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement