Change the cart item quantity for specific products in woocommerce Change the cart item quantity for specific products in woocommerce wordpress wordpress

Change the cart item quantity for specific products in woocommerce


To change quantities, see after that code. Here your revisited code:

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {     $product = $cart_item['data']; // Get an instance of the WC_Product object    echo "<b>".$product->get_title().'</b>  <br> Quantity: '.$cart_item['quantity'].'<br>';     echo "  Price: ".$product->get_price()."<br>";} 

Updated: Now to change the quantity on specific products, you will need to use this custom function hooked in woocommerce_before_calculate_totals action hook:

add_action('woocommerce_before_calculate_totals', 'change_cart_item_quantities', 20, 1 );function change_cart_item_quantities ( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )        return;    // HERE below define your specific products IDs    $specific_ids = array(37, 51);    $new_qty = 1; // New quantity    // Checking cart items    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {        $product_id = $cart_item['data']->get_id();        // Check for specific product IDs and change quantity        if( in_array( $product_id, $specific_ids ) && $cart_item['quantity'] != $new_qty ){            $cart->set_quantity( $cart_item_key, $new_qty ); // Change quantity        }    }}

Code goes in function.php file of the active child theme (or active theme).

Tested and works


My need is to display gross weight and net weight of the products and change the gross weight and net-weight based on the quantity. While change the qty the net weight and gross-weight also should change. It was working fine in single product page while coming to the grouped products unable to achieve this.

Here is my single product working fine: https://hamarafresh.com/product/salmon/

Here is my grouped products not working: https://hamarafresh.com/product/lady-fish/

I am getting gross weight and net weight from custom field.

add_action( 'woocommerce_after_quantity_input_field', 'woocommerce_total_product_price', 31 );    function woocommerce_total_product_price() {                 if ( is_product() ) {                     global $woocommerce, $product;                $gross_weight = get_post_custom_values($key = 'gross_weight');           $net_weight = get_post_custom_values($key = 'net_weight');        get_post_meta( $post_id, $key, $single );        // let's setup our divs      ?>    <?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) ); ?>                    <?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) ); ?>                    <?php          echo sprintf('<div id="net_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Net Weight:'),'<span class="price">'.$netweight5.'</span>');                echo sprintf('<div id="gross_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Gross Weight:'),'<span class="price">'.$grossweight5.'</span>');                  echo sprintf('<div id="product_total_price" style="margin-bottom:20px; text-align: center;">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');                         ?>            <script>                jQuery(function($){                                        var price = <?php echo $product->get_price(); ?>,                                  currency = '<?php echo get_woocommerce_currency_symbol(); ?>';                        <?php   $net_weight = get_post_custom_values($key = 'net_weight'); ?>;                                        <?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) );   ?>;                                         var net_weightt = <?php echo $netweight5; ?>;                                                                 <?php   $gross_weight = get_post_custom_values($key = 'gross_weight'); ?>;                        <?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) );   ?>;                     var gross_weightt = <?php echo $grossweight5 ?>;                                                                                          $('[name=quantity]').change(function(){                        if (!(this.value < 0.5)) {                                                        var net_weighttt = (net_weightt* this.value);                            var gross_weighttt = (gross_weightt* this.value);                            var product_total = parseFloat(price * this.value);                                $('#product_total_price .price').html( currency + product_total.toFixed(2));                                                     $('#net_weight_disp .price').html( currency + net_weighttt.toFixed(2));                                                         $('#gross_weight_disp .price').html( currency + gross_weighttt.toFixed(2));                            }                    });                });            </script>        <?php    }        }

custom fieldGrouped product pageSingle Product page