1. Home
  2. Docs
  3. SNIPPETS – WORDPRESS
  4. WOOCOMMERCE
  5. Update cart when modifying the item quantity

Update cart when modifying the item quantity

This code snippet will allow you to update the cart by only updating the quantities, without needing to click “Update Cart”.

<?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;
});