Ad

How To Refresh Cart And Checkout In Every 5 Seconds In WooCommerce?

I am working on WordPress / WooCommerce

My requirement is when the customer is on the CART page, I want to refresh CART every 5 seconds, without page load.

For eg: If the customer has added 1 product to the cart and the price is ₹8.00

Later on, Admin changes the price for the product from ₹8.00 to ₹10.00

How can I show the latest price without page refresh?

I am using this code, but not working

(function( $ ) {
    'use strict';

    jQuery( document ).ready(function($) {
        function refresh_fragments() {
            console.log('fragments refreshed!');
            $( document.body ).trigger( 'wc_fragments_refreshed' );
        }
        refresh_fragments();
        setInterval(refresh_fragments, 5000);
    });

})(jQuery);
Ad

Answer

Try the below code. instead of using the wc_fragments_refreshed trigger, you can trigger the update cart button.

Cart refresh.

(function( $ ) {
    'use strict';
    jQuery( document ).ready(function($) {
        function refresh_fragments() {
            console.log('fragments refreshed!');
            jQuery( "[name='update_cart']" ).removeAttr( 'disabled' );
            jQuery( "[name='update_cart']" ).trigger( 'click' );
        }
        setInterval(refresh_fragments, 5000);
    });
})(jQuery);

UPDATE as per OP request.

Checkout Refresh.

Use update_checkout trigger.

(function( $ ) {
    'use strict';
    jQuery( document ).ready(function($) {
        function refresh_fragments() {
            $( document.body ).trigger( 'update_checkout' );
        }
        setInterval(refresh_fragments, 5000);
    });
})(jQuery);
Ad
source: stackoverflow.com
Ad