Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 16 mins ago.
Improve this questionI created this function in functions.php, and it does trigger the default quantity for a specific product. But it does not update the cart for this product for the subtotal to change, reflecting the quantity and total price.
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
//var_dump($product->get_sku());
if ( is_cart() && str_starts_with( $product->get_sku(), "TC" ) || is_checkout() && str_starts_with( $product->get_sku(), "TC" ) ){
$WC_Cart = new WC_Cart();
//$WC_Cart->set_quantity( $product->get_id(), 36, true );
var_dump($product->get_id());
$args['input_value'] = 36; // Start from this value (default = 1)
$args['min_value'] = 36; // Minimum value
//$args['step'] = 36; // Quantity steps
//WC()->cart->set_quantity( $product->get_id(),36 );
return $args;
}else{
$args['input_value'] = 1;
$args['min_value'] = 1;
return $args;
}
}
How can I run a cart update in my functions.php?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 16 mins ago.
Improve this questionI created this function in functions.php, and it does trigger the default quantity for a specific product. But it does not update the cart for this product for the subtotal to change, reflecting the quantity and total price.
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
//var_dump($product->get_sku());
if ( is_cart() && str_starts_with( $product->get_sku(), "TC" ) || is_checkout() && str_starts_with( $product->get_sku(), "TC" ) ){
$WC_Cart = new WC_Cart();
//$WC_Cart->set_quantity( $product->get_id(), 36, true );
var_dump($product->get_id());
$args['input_value'] = 36; // Start from this value (default = 1)
$args['min_value'] = 36; // Minimum value
//$args['step'] = 36; // Quantity steps
//WC()->cart->set_quantity( $product->get_id(),36 );
return $args;
}else{
$args['input_value'] = 1;
$args['min_value'] = 1;
return $args;
}
}
How can I run a cart update in my functions.php?
you need to hook into a WooCommerce action before the cart totals are calculated and set the quantity there.
add_action(
'woocommerce_before_calculate_totals',
static function (\WC_Cart $cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
return;
}
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( str_starts_with( $product->get_sku(), 'TC' ) && $cart_item['quantity'] < 36 ) {
$cart->set_quantity( $cart_item_key, 36 );
}
}
});
Your woocommerce_quantity_input_args
filter is fine for the UI input.
add_filter(
'woocommerce_quantity_input_args',
static function (array $args, $product): array {
if ( ( is_cart() || is_checkout() ) && str_starts_with( $product->get_sku(), 'TC' ) ) {
$args['input_value'] = 36; // Default quantity
$args['min_value'] = 36; // Minimum allowed
}
return $args;
},
10,
2);