woocommerce - 以编程方式更新购物车项目数量 [英] woocommerce - programmatically update cart item quantity

查看:25
本文介绍了woocommerce - 以编程方式更新购物车项目数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果满足某些条件,我正在尝试以编程方式更新购物车中特定产品的数量.

I am trying to programmatically update the quantity of a specific product in the cart if certain criteria is met.

我可以使用以下内容轻松更新购物车商品的价格:

I can easily update the price of the cart items with the following:

add_action( 'woocommerce_before_calculate_totals', 'wwpa_simple_add_cart_price' );
function wwpa_simple_add_cart_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $key => $value ) {
                $value['data']->price = '1';
}}

在上面的函数中我尝试添加:

In the function above I tried to add:

$value['data']->quantity= '10';

这不起作用,但不太确定如何或是否可以编辑数量?

This doesn't work but not quite sure how or if I can edit the quantity?

我还尝试了在 WooCommerce 中挖掘时发现的这些组合:

I also tried a these combinations that I found while digging around WooCommerce:

$value['data']->quantity= '10';
$value['data']->qty= '10';
$value['quantity'] = '10';

这些都没有奏效.

推荐答案

更新数量:

global $woocommerce;
$woocommerce->cart->set_quantity($cart_item_key, '100');

如何获取 $cart_item_key 示例:

How to get the $cart_item_key example:

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
    echo $cart_item_key;
} 

另一个例子是已知的cart_item_key:

And another example with a known cart_item_key:

global $woocommerce;
$woocommerce->cart->set_quantity('8d317bdcf4aafcfc22149d77babee96d', '100');

希望这有帮助:)

这篇关于woocommerce - 以编程方式更新购物车项目数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆