{"id":6297,"date":"2022-05-23T06:08:47","date_gmt":"2022-05-23T10:08:47","guid":{"rendered":"https:\/\/onecommerce.io\/blog\/?p=6297"},"modified":"2023-08-14T23:51:25","modified_gmt":"2023-08-15T03:51:25","slug":"ajax-add-to-cart-woocommerce","status":"publish","type":"post","link":"https:\/\/onecommerce.io\/blog\/2022\/05\/23\/ajax-add-to-cart-woocommerce\/","title":{"rendered":"How To Set Up &#8220;Ajax Add To Cart&#8221; On WooCommerce"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">A lot of online sellers like WooCommerce because of its simplicity and convenience. Anyone can create a website of their own with a few simple setup steps or install a few plugins.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But not every website brought to users the best experience and an \u201c<strong>add to cart<\/strong>\u201d action is an example.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Having to reload the entire website every time a customer adds a product to the cart will limit the user experience. Sometimes, this also limits your opportunity to reach out to potential customers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">That is the reason why you have entered this article. In this, I will show the cause and how to install the \u201c<strong><a href=\"https:\/\/onecommerce.io\/blog\/?p=6297&amp;preview=true#ajax-add-to-cart\">Ajax add to cart<\/a><\/strong>\u201d button which makes your website smoother.<\/span><\/p>\n<h2><strong>What is AJAX?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">AJAX stands for Asynchronous Javascript and XML is a method of exchanging data with the server without reloading the existing web page.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It is written in Javascript and runs on the client-side so that every browser runs independently.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And this is done by sending an XMLHttpRequest object containing the new information to a web server, which returns the new information only for the portion that has changed.<\/span><\/p>\n<h2><strong>How does it work?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">From the client browser, we have an event to call Ajax. At that time, JavaScript will create an object XMLHTTPRequest. And this object will be sent a request to the server.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, when the user clicks on the Input box and selects the skill to find the user&#8217;s job. Then we will take that information and send it to the server and need to return the corresponding actions suitable for users.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When the server receives HTTPRequest it will process the request and return the response to the web.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The server processes JavaScript jobs such as the skill that users need and returns the jobs&#8217; data.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After receiving a response from the server, JavaScript will process and update the website for us.<\/span><\/p>\n<h2><strong>Benefits of \u201cAjax add to cart\u201d\u00a0<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">The reason why you should choose an \u201cAjax add to cart\u201d for your website are:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It only sends the needed data and reloads a small part instead of the whole page to update the information.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The website created will be smoother.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It increases users&#8217; experience.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It\u2019s a highly useful tool for those who want to make their online store experience as user-friendly as possible.<\/span><\/li>\n<\/ul>\n<h2><b>There are 2 ways to set up an \u201cAjax add to cart\u201d button:<\/b><\/h2>\n<h3><span style=\"font-size: 12pt;\">For common users:\u00a0<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">It is easier for most users who don\u2019t know how to code that you can <\/span><b>download it from <a href=\"https:\/\/wordpress.org\/plugins\/woo-ajax-add-to-cart\/\" target=\"_blank\" rel=\"noopener\">here<\/a><\/b><span style=\"font-weight: 400;\">. The plugin is free and it works for both single and variable products.<\/span><\/p>\n<div id=\"attachment_6317\" style=\"width: 1189px\" class=\"wp-caption alignleft\"><img aria-describedby=\"caption-attachment-6317\" class=\"size-full wp-image-6317\" src=\"http:\/\/onecommerce.io\/blog\/wp-content\/uploads\/2022\/05\/ajax-add-to-cart.jpg\" alt=\"Set up Ajax add to cart on WooCommerce\" width=\"1179\" height=\"800\" title=\"\"><p id=\"caption-attachment-6317\" class=\"wp-caption-text\">Ajax add to cart on WooCommerce<\/p><\/div>\n<h3><span style=\"font-size: 12pt;\"><b>For developers:<\/b><\/span><\/h3>\n<p><b>Create a Child Theme by using Child Theme Generator.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">A child theme is a theme that is created and inherits all the features and functions of the parent theme.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The main factor for creating it is that if you directly modify the theme&#8217;s files, the new files will override your changes, and all of your changes will be lost the next time the theme is updated. However, if you modify the child theme, the changes will not be overwritten.<\/span><\/p>\n<p><b>Add the Javascript<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Add the following code to the functions.php file in your child theme.\u00a0<\/span><span style=\"font-size: 10pt;\"><code><\/code><\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\"><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">function woocommerce_ajax_add_to_cart_js() {<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (function_exists('is_product') &amp;&amp; is_product()) {<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wp_enqueue_script('woocommerce-ajax-add-to-cart', plugin_dir_url(__FILE__) . 'assets\/ajax-add-to-cart.js', array('jquery'), '', true);<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">}<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">add_action('wp_enqueue_scripts', 'woocommerce_ajax_add_to_cart_js', 99);<\/span><\/code><\/span><b>Create Javascript File<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Create a new folder in your child&#8217;s theme called &#8220;JS&#8221; and add a new javascript called &#8220;ajax-add-to-cart.js&#8221; after adding the javascript.<\/span><\/p>\n<p><strong>Add jQuery to avoid recharging<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Use this code to prevent information is sent to the database and reload the whole page.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\"><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">(function ($) {<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">$( document ).on( 'click', '.single_add_to_cart_button', function(e) {<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">e.preventDefault();<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">});<\/span><\/code><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><code><span style=\"font-weight: 400;\">})(jQuery);<\/span><\/code><\/span><\/p>\n<p><span style=\"font-weight: 400;\">After that, you need to add the below code<\/span><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">$thisbutton = $(this),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$form =$thisbutton.closest('form.cart'),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0id = $thisbutton.val(),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_qty=<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">$form.find('input[name=quantity]').val() || 1,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_id =$form.find('input[name=product_id]').val() || id,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0variation_id =<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">$form.find('input[name=variation_id]').val() || 0;<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">var data = {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0action: 'woocommerce_ajax_add_to_cart',<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_id: product_id,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product_sku: '',<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0quantity: product_qty,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0variation_id: variation_id,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">$(document.body).trigger('adding_to_cart', [$thisbutton, data]);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">$.ajax({<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0type: 'post',<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0url: wc_add_to_cart_params.ajax_url,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data: data,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0beforeSend: function (response) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$thisbutton.removeClass('added').addClass('loading');<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0complete: function (response) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$thisbutton.addClass('added').removeClass('loading');<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0success: function (response) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (response.error &amp; response.product_url) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0window.location = response.product_url;<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return;<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\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]);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return false;<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0});<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">})(jQuery);<\/span><\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">It will step-by-step perform the process such as:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Add the product to the cart<\/span><\/li>\n<\/ul>\n<p style=\"padding-left: 40px;\"><span style=\"font-weight: 400;\">jQuery will be used to collect data from the website and add a product from the site to the cart. Incorporate this code into the previous code that hooks into the add to cart single product button event to collect product data.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Use add to cart event<\/span><\/li>\n<\/ul>\n<p style=\"padding-left: 40px;\"><span style=\"font-weight: 400;\">Following this code, in which we define an array with all required variables, we will fire the add to cart event, just in case a theme is using this function to make changes.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Use add to cart function<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Use add to cart on single products script<\/span><\/li>\n<\/ul>\n<p><b>Add to cart PHP function<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Last but not least, the PHP function will be required. The simple way to implement your add to cart feature is to use some of the WooCommerce filter hooks. You will do it again this time, but with AJAX.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let the script just below the previous script we used to add the Javascript file to the child theme.<\/span><code><\/code><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\"><code><span style=\"font-weight: 400; font-size: 10pt;\">add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');<\/span><\/code><\/p>\n<p><code><span style=\"font-size: 10pt;\"><span style=\"font-weight: 400;\">add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0\u00a0<\/span><\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">function woocommerce_ajax_add_to_cart() {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$variation_id = absint($_POST['variation_id']);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$product_status = get_post_status($product_id);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if ($passed_validation &amp;&amp; WC()-&gt;cart-&gt;add_to_cart($product_id, $quantity, $variation_id) &amp;&amp; 'publish' === $product_status) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0do_action('woocommerce_ajax_added_to_cart', $product_id);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wc_add_to_cart_message(array($product_id =&gt; $quantity), true);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WC_AJAX :: get_refreshed_fragments();<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$data = array(<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'error' =&gt; true,<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'product_url' =&gt; apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo wp_send_json($data);<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wp_die();<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400; font-size: 10pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code><\/code><b><\/b><\/p>\n<p><b>Test the code<\/b><\/p>\n<p><span style=\"font-weight: 400;\">If you press \u201cEnter\u201d and it runs, so Congratulations!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If not, you should check the coding again.<\/span><\/p>\n<p><b>Enable the \u201cadd to cart&#8221; button<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You have to go to Dashboard &gt; WooCommerce &gt; Setting &gt; Products tab.<\/span><\/p>\n<h2><b>NOTE:<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h2>\n<p><b>What is the role of jQuery in \u201cAjax add to cart&#8221;?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The jQuery code was used to prevent information from being exchanged with the database and to reload the entire page. Using jQuery will only require a certain reload of the web page.<\/span><\/p>\n<p><b>What are the Advantages and Disadvantages of \u201cAjax add to cart&#8221;?<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Advantage:\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Improved User Experience: The most significant advantage is the rich user experience provided by AJAX.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By exchanging small amounts of data with the server, AJAX allows websites to update the serial number. This allows you to update parts of the site without having to reload the entire page. AJAX improves browser performance and allows for faster browsing speed, resulting in a more responsive user experience.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Enhanced User Productivity: The AJAX library includes object-oriented helper functions that boost productivity while decreasing frustration.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Reduce bandwidth usage and increase speed: AJAX communicates with the webserver and exchanges data using JavaScript via client-side scripting. Response times are faster when Ajax is used, so performance and speed are improved.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Disadvantages:<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Increase the load on the webserver: If you add a type of automatic update to the server every few seconds, the load may increase depending on the user.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Browser incompatibility: AJAX is heavily reliant on JavaScript, which is implemented differently in different browsers. This proves to be a drawback, especially when AJAX is required to work across browsers.<\/span><\/p>\n<p><b>Wrapping up<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In conclusion, the best way to set up an add to cart button on WooCommerce is by using the WooCommerce Ajax Cart plugin.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This plugin is highly customizable and easy to use. It\u2019s a great option for those who want to add an \u201c<strong>Ajax Add to Cart<\/strong>\u201d button on their site that can update the total amount of their customers\u2019 carts in the cart without having to refresh the page.\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of online sellers like WooCommerce because of its simplicity and convenience. Anyone can&hellip;<\/p>\n","protected":false},"author":8,"featured_media":18612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[44],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/posts\/6297"}],"collection":[{"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/comments?post=6297"}],"version-history":[{"count":11,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/posts\/6297\/revisions"}],"predecessor-version":[{"id":18619,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/posts\/6297\/revisions\/18619"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/media\/18612"}],"wp:attachment":[{"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/media?parent=6297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/categories?post=6297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onecommerce.io\/blog\/wp-json\/wp\/v2\/tags?post=6297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}