410

How to Automatically Update WooCommerce Cart Quantities

Powered by WPCodeBox

This WooCommerce code snippet automatically updates cart quantities, eliminating the need to click “Update Cart” and streamlining the shopping experience for your customers.


<?php 

add_action('wp_head', function(){
    
    $css_code = <<<CSS
    <style>
    .woocommerce-notices-wrapper div.woocommerce-message[role=alert] {
        display: none;
    }
    
    .woocommerce button[name="update_cart"] {
        display: none !important;
    }
    </style>
CSS;
    $js_code = <<<JS
        <script type="text/javascript">
        jQuery(document).ready(function(){
            var wpcbScrollToNotices;
        
            jQuery(document).ajaxComplete(function (e) {
                jQuery.scroll_to_notices = wpcbScrollToNotices;
            });
            
            jQuery('.woocommerce').on('change', 'input.qty', function(){
            
                wpcbScrollToNotices = jQuery.scroll_to_notices;
                jQuery.scroll_to_notices = function( scrollElement ) {
                    return false;
            	};
            	
            	setTimeout(function(){
            	    jQuery('button[name="update_cart"]').trigger('click');    
            	}, 200);
                
            
            });
        });
        </script>
JS;
    echo $css_code;
    echo $js_code;
});

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.