function enable_ajax_add_to_cart_single_product() { if (is_product()) { ?> <script type="text/javascript"> jQuery(function($){ /* global wc_add_to_cart_params */ if ( typeof wc_add_to_cart_params === 'undefined' ) { return false; } // Ajax add to cart on the product page var $warp_fragment_refresh = { url: woocommerce_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ), type: 'POST', success: function( data ) { if ( data && data.fragments ) { $.each( data.fragments, function( key, value ) { $( key ).replaceWith( value ); }); $( document.body ).trigger( 'wc_fragments_refreshed' ); $('body').addClass('is-visible'); } } }; $('form.cart').on('submit', function (e) { e.preventDefault(); var form = $(this), button = form.find('.single_add_to_cart_button'); button.block({ message: null, overlayCSS: { opacity: 0 } }); var product_url = window.location; $.post(product_url, form.serialize() + '&_wp_http_referer=' + product_url, function (result) { // update fragments $.ajax($warp_fragment_refresh); button.unblock(); }); }); }); </script> <?php } } add_action('wp_footer', 'enable_ajax_add_to_cart_single_product'); function enqueue_woocommerce_ajax_add_to_cart() { if (is_product()) { wp_enqueue_script('wc-add-to-cart'); // WooCommerce Add to Cart Script wp_enqueue_script('woocommerce'); } } add_action('wp_enqueue_scripts', 'enqueue_woocommerce_ajax_add_to_cart');
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter