ajax_add_to_cart.js<\/i> file below:<\/p>\nfunction ql_woocommerce_ajax_add_to_cart_js() {\r\n\u00a0\u00a0\u00a0\u00a0if (function_exists('is_product') && is_product()) {\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wp_enqueue_script('custom_script', get_bloginfo('stylesheet_directory') . '\/js\/ajax_add_to_cart.js', array('jquery'),'1.0' );\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}\r\nadd_action('wp_enqueue_scripts', 'ql_woocommerce_ajax_add_to_cart_js');<\/pre>\nWith the conditional if (function_exists(‘is_product’) && is_product())<\/em> in line 2, you make sure that the JQuery is only applied on a product page.<\/span><\/p>\nC) Create a JS file<\/b><\/h4>\n
Once you\u2019ve done that, create the file you included in the previous step. Just go to your cPanel<\/strong> and then to the child theme folder<\/strong>. After that, follow the route wp_content \/themes\/ folder<\/i><\/strong>. There, create a new folder in the child theme called js<\/em>, and then a file inside it with the same name you used in the hook ajax-add-to-cart.js<\/i>. Keep in mind that this step also works even if you use an FTP client. If your child theme already has a JS folder, you can use it without creating a new one.<\/p>\nD) The JQuery\/JS file<\/b><\/h4>\n
The next step is to work on the JQuery file that you\u2019ve uploaded in your child theme. That file is empty so you need to add scripts. First, prevent the add to cart<\/em> button from reloading the page with the following script:<\/p>\n$('.single_add_to_cart_button').on('click', function(e) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0e.preventDefault();\r\n});<\/pre>\nPlease note that we use the $(‘.single_add_to_cart_button’)<\/i> selector to trigger the AJAX call when the button is clicked. You must understand this because all the following script will go inside this function.<\/p>\nE) JQuery WooCommerce AJAX add to cart on single products<\/b><\/h4>\n
So the full JQuery script for AJAX add to cart on single products is:<\/p>\n
jQuery(document).ready(function($) {\r\n\u00a0\u00a0\u00a0\u00a0$('.single_add_to_cart_button').on('click', function(e){\u00a0\r\n\u00a0\u00a0\u00a0\u00a0e.preventDefault();\r\n\u00a0\u00a0\u00a0\u00a0$thisbutton = $(this),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$form = $thisbutton.closest('form.cart'),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0id = $thisbutton.val(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_qty = $form.find('input[name=quantity]').val() || 1,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_id = $form.find('input[name=product_id]').val() || id,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0variation_id = $form.find('input[name=variation_id]').val() || 0;\r\n\u00a0\u00a0\u00a0\u00a0var data = {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0action: 'ql_woocommerce_ajax_add_to_cart',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_id: product_id,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_sku: '',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0quantity: product_qty,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0variation_id: variation_id,\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\r\n\u00a0\u00a0\u00a0\u00a0$.ajax({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0type: 'post',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0url: wc_add_to_cart_params.ajax_url,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data: data,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0beforeSend: function (response) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$thisbutton.removeClass('added').addClass('loading');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0complete: function (response) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$thisbutton.addClass('added').removeClass('loading');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0success: function (response) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (response.error & response.product_url) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0window.location = response.product_url;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $thisbutton]);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0});\u00a0\r\n});<\/pre>\nF) The PHP script<\/b><\/h4>\n
Afterward, you need the PHP handler. The best way is to use some of the WooCommerce filter hooks to build your add to cart<\/em>\u00a0function. This time you\u2019ll do it using AJAX. Simply copy and paste the following script in the functions.php<\/i><\/strong> file of the child theme below the previous script we used to include the JS file.<\/p>\nadd_action('wp_ajax_ql_woocommerce_ajax_add_to_cart', 'ql_woocommerce_ajax_add_to_cart');\u00a0\r\nadd_action('wp_ajax_nopriv_ql_woocommerce_ajax_add_to_cart', 'ql_woocommerce_ajax_add_to_cart');\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nfunction ql_woocommerce_ajax_add_to_cart() {\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0$product_id = apply_filters('ql_woocommerce_add_to_cart_product_id', absint($_POST['product_id']));\r\n\u00a0\u00a0\u00a0\u00a0$quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);\r\n\u00a0\u00a0\u00a0\u00a0$variation_id = absint($_POST['variation_id']);\r\n\u00a0\u00a0\u00a0\u00a0$passed_validation = apply_filters('ql_woocommerce_add_to_cart_validation', true, $product_id, $quantity);\r\n\u00a0\u00a0\u00a0\u00a0$product_status = get_post_status($product_id);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0do_action('ql_woocommerce_ajax_added_to_cart', $product_id);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if ('yes' === get_option('ql_woocommerce_cart_redirect_after_add')) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wc_add_to_cart_message(array($product_id => $quantity), true);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WC_AJAX :: get_refreshed_fragments();\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$data = array(\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'error' => true,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'product_url' => apply_filters('ql_woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo wp_send_json($data);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wp_die();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/pre>\nThat\u2019s it! You\u2019ve just created your WooCommerce AJAX add to cart<\/b> button<\/b>! But how do you know that you\u2019ve done everything correctly? You can check it easily. When you click on the add to cart<\/i> button, the product should be added to the cart without reloading the page.<\/p>\nConclusion<\/h2>\n
All in all, if you want to optimize your store, the WooCommerce AJAX add to cart<\/em> function is a must. The easy way to apply it is by using the AJAX add to cart<\/em> WooCommerce plugin<\/a>. However, if you have coding skills, you can code AJAX add to cart<\/em> yourself with a child theme and a bit of PHP as described above.<\/p>\nBoth options are excellent so choose the one that better suits your needs and skills. This will help you improve user experience and increase your conversion rates. Please let us know your thoughts in the comments section below.<\/p>\n
Finally, for more guides about WooCommerce, we recommend you check out our guides on how to add WooCommerce products<\/a> and how to create default product attributes in WooCommerce<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"Some weeks ago we talked about how to add the cart function programmatically to boost your sales. Today, we\u2019ll have […]<\/p>\n","protected":false},"author":1465,"featured_media":88920,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2218],"tags":[1262,1261,1598],"coauthors":[1327,1328],"yoast_head":"\n
WooCommerce AJAX add to cart - Step-by-Step Tutorial - QuadLayers<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\n\n\t\n\t\n\t\n