$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - update cart after quantity change or a specific product|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - update cart after quantity change or a specific product

matteradmin9PV0评论
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 question

I 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 question

I 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?

Share Improve this question edited Apr 23 at 6:23 Ali Khakbaz 1032 bronze badges asked Apr 19 at 15:41 gstlouisgstlouis 111 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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);
Post a comment

comment list (0)

  1. No comments so far