【问题标题】:Change the cart item quantity for specific products in woocommerce在 woocommerce 中更改特定产品的购物车商品数量
【发布时间】:2018-07-31 13:26:46
【问题描述】:

我可以更改某些特定产品中的 WooCommerce 数量吗?

我试过了:

global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    foreach($items as $item => $values) { 
        $_product = $values['data']->post; 
        echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        echo "  Price: ".$price."<br>";
    } 

如何在购物车中获取特定的产品 ID?

【问题讨论】:

    标签: php wordpress woocommerce cart product-quantity


    【解决方案1】:

    要更改数量,请参阅该代码之后。这是您重新访问的代码:

    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>";
    } 
    

    更新:现在要更改特定产品的数量,您需要使用挂钩在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
            }
        }
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且可以工作

    【讨论】:

    • 对我来说,数量正在更新,但价格已经消失,我将其用于复合组产品。你能帮我解决这个问题吗
    • @AnilSharma 作为第三方插件的一部分的复合产品,此代码不处理此自定义产品类型。您需要根据自己的需要对其进行调整。
    • 你能指导我实现这一目标吗?
    • @AnilSharma 也许在 StackOverFlow 中针对复合产品提出一个新问题……
    【解决方案2】:

    我需要显示产品的毛重和净重,并根据数量更改毛重和净重。在改变数量的同时,净重和毛重也应该改变。它在单个产品页面上运行良好,而分组产品无法实现这一点。

    这是我的单品工作正常:https://hamarafresh.com/product/salmon/

    这是我的分组产品不起作用:https://hamarafresh.com/product/lady-fish/

    我正在从自定义字段中获取毛重和净重。

    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
        }
        
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-10
      • 2019-09-18
      • 2021-05-16
      • 1970-01-01
      • 2019-01-27
      • 2020-11-15
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多