Auto trigger Update on quantity change in WooCommerce cart page
To make it work, you need to delegate the "change" event to the document body, this way:
add_action( 'wp_footer', 'auto_update_cart_on_qty_change' );
function auto_update_cart_on_qty_change() {
if ( is_cart() ) :
?>
<script type="text/javascript">
(function($){
$( document.body ).on( 'change input', 'input.qty', function() {
$('[name=update_cart]').trigger('click');
});
})(jQuery);
</script>
<?php
endif;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Added also "input" event when customer input a value in the quantity field.
Auto update cart totals on cart item quantity change in Woocommerce
You need to use it as a document.body delegated event (on change
and input
events) as follow:
add_action( 'wp_footer', 'update_cart_on_item_qty_change');
function update_cart_on_item_qty_change() {
if (is_cart()) :
?>
<script type="text/javascript">
jQuery( function($){
$(document.body).on('change input', '.qty', function(){
$('button[name="update_cart"]').trigger('click');
// console.log('Cart quantity changed…');
});
});
</script>
<?php
endif;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Similar: Avoid a function to only runs once on button click in WooCommerce cart
Auto update cart on quantity change in Woocommerce cart page
Just to make this question somewhat useful for anyone who reads here in the future.. here is the script that ended up working for me. I found the answer here from this helpful guide.
jQuery('div.woocommerce').on('click', 'input.qty', function(){
jQuery("[name='update_cart']").trigger("click");
});
WooCommerce cart quantity won't change after cart update
To make it work everywhere with different settings based on specific products, try the following instead:
// General quantity settings
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ){
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
if( ! is_cart() ) {
$args['input_value'] = $condition ? 25 : 26; // Starting value
}
$args['min_value'] = $condition ? 25 : 26; // Minimum value
$args['max_value'] = $condition ? 500 : 260; // Maximum value
$args['step'] = $condition ? 25 : 26; // Step value
return $args;
}
// For Ajax add to cart button (define the min and max value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
$args['quantity'] = $condition ? 25 : 26; // Min value
return $args;
}
// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
function custom_available_variation_min_qty( $data, $product, $variation ) {
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
$args['min_qty'] = $condition ? 25 : 26; // Min value
$args['max_qty'] = $condition ? 500 : 260; // Max value
return $data;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Enable Update cart button on quantity change with custom quantity buttons in WooCommerce cart
In your code, you need to remove disabled and aria-disabled properties from the "Update" cart button using this code line:
$('[name=update_cart]').prop({'disabled': false, 'aria-disabled': false });
So in your code, when quantity change with your custom "minus" and "plus" buttons:
function btnproduct() {
$('.btn-product-up').on('click', function (e) {
e.preventDefault();
var numProduct = Number($(this).next().val());
if (numProduct > 1) {
$(this).next().val(numProduct - 1);
$('[name=update_cart]').prop({'disabled': false, 'aria-disabled': false });
}
});
$('.btn-product-down').on('click', function (e) {
e.preventDefault();
var numProduct = Number($(this).prev().val());
$(this).prev().val(numProduct + 1);
$('[name=update_cart]').prop({'disabled': false, 'aria-disabled': false });
}
});
};
Tested and works.
Which Hook to alter quantity update in WooCommerce cart page?
You should use woocommerce_after_cart_item_quantity_update
action hook that has 4 arguments. But when quantity is changed to zero, woocommerce_before_cart_item_quantity_zero
action hook need to be used instead (and has 2 arguments).
Below is a working example that will limit the updated quantity to a certain amount and will display a custom notice:
add_action( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $quantity, $old_quantity, $cart ){
if( ! is_cart() ) return; // Only on cart page
// Here the quantity limit
$limit = 5;
if( $quantity > $limit ){
// Change the quantity to the limit allowed
$cart->cart_contents[ $cart_item_key ]['quantity'] = $limit;
// Add a custom notice
wc_add_notice( __('Quantity limit reached for this item'), 'notice' );
}
}
This code goes on function.php file of your active child theme (or theme). Tested and works.
As this hook is located in
WC_Cart
set_quantity()
method, is not possible to use that method inside the hook, as it will throw an error.
To trigger some action when quantity is set to Zero use:
add_action( 'woocommerce_before_cart_item_quantity_zero', 'action_before_cart_item_quantity_zero', 20, 4 );
function action_before_cart_item_quantity_zero( $cart_item_key, $cart ){
// Your code goes here
}
Related Topics
How to Prevent a User from Directly Accessing My HTML Page by Writing Url
Sending Post Requests Without Waiting for Response
Validating Base64 Encoded Images
How to Get Only Date from Datetime in Codeigniter
Count Same Values in Array and Combine into Array
Laravel Convert Query MySQL to Model Query
Regex - Exclude "-" Between Numbers
How to Get Last Insert Id in Sqlite
How to Use Two Foreach to Insert Data in Database
How to Add Dynamic Filter Using PHP MySQL Ajax
Get Selected Option in PHP Without Pressing Submit
How to Update Field to Add Value to Existing Value
Laravel PHP Fatal Error: Require(): Failed Opening Required ... Bootstrap/Autoload.Php on Line 17
How to Remove Duplicate Values from a Multi-Dimensional Array in PHP