6

我可以更改某些特定产品中的 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?

4

2 回答 2

17

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

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动作挂钩中:

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 文件中。

测试和工作

于 2018-02-21T04:09:04.267 回答
0

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

这是我的单一产品工作正常: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
    }
    
    }

自定义字段 分组产品页面 单品页面

于 2021-10-04T18:37:18.870 回答