Home > Software engineering >  WooCommerce how to refresh cart in every 5 seconds
WooCommerce how to refresh cart in every 5 seconds

Time:10-22

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 have added 1 product to 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);

CodePudding user response:

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);
  • Related