简体   繁体   English

更改woocommerce中特定产品的购物车项目数量

[英]Change the cart item quantity for specific products in woocommerce

Can I change WooCommerce quantity in some specific product?我可以更改某些特定产品的 WooCommerce 数量吗?

I have tried:我试过:

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

How to get specific product Id in cart?如何获取购物车中的特定产品 ID?

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

Code goes in function.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works测试和工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce 中特定产品的购物车批量折扣 - Cart bulk quantity discount for specific products in Woocommerce 在Woocommerce 3中隐藏购物车中特定产品的数量字段 - Hide quantity fields in cart for specific products in Woocommerce 3 如何更改woocommerce中购物车项目数量的设计? - How to change the design for cart item quantity in woocommerce? 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 如何将特定商品的 WooCommerce 购物车商品数量限制为一个 - How to restrict WooCommerce cart item quantity to one for specific item 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 根据 Woocommerce 中的产品数量替换特定的购物车项目 - Replace specific cart item based on product quantity in Woocommerce WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce 在 WooCommerce 中为特定不同类别的产品设置项目数量为“x”的倍数 - Set item quantity to multiples of “x” for products in a specific different categories in WooCommerce 对于 Woocommerce 中特定类别中的产品,将项目数量设置为“x”的倍数 - Set item quantity to multiples of “x” for products in a specific category in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM