简体   繁体   English

Woocommerce:以编程方式更新购物车中的项目

[英]Woocommerce: Programmatically Update Item In Cart

I need to programmatically and dynamically change the price of an item in the cart.我需要以编程方式动态更改购物车中商品的价格。

I've tried varying combinations of Woocommerce action hooks, the cart and session objects, but nothing quite seems to do the trick.我尝试了 Woocommerce 动作挂钩、购物车和会话对象的不同组合,但似乎没有什么能解决问题。 I thought this wouldn't be so challenging.我认为这不会那么具有挑战性。

add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );

function change_cart_item_price( $cart_object ) {

    foreach ( $cart_object->cart_contents as $key => $value ) {

        if( 123 == $value['data']->id ) {

            $new_price = function_to_get_new_price( $value, get_current_user_id( ) );
            $value['data']->price = $new_price;
        }
   }
}

The above code changes the price for each item only on the checkout page, or when updating the cart ( ie: the hook is called when removing an item from the cart ), but not indefinitely.上面的代码仅在结帐页面或更新购物车时更改每个项目的价格(即:从购物车中删除项目时调用钩子),但不是无限期的。

I'm using the Woocommerce Gravity Forms add-on.我正在使用 Woocommerce Gravity Forms 插件。 I have one product in particular, which will be ordered multiple times by a given user.我特别有一个产品,它会被给定的用户多次订购。 The user will be allowed 5x free with only shipping fees, and each above 5 will be $20.用户将被允许免费 5 次,只需支付运费,每 5 次以上将收取 20 美元。 I have this much coded and functional with Gravity Forms hooks that dynamically populate fields.我使用 Gravity Forms 钩子进行了大量编码和功能,这些钩子可以动态填充字段。 Shipping is specific to fields within the gravity form, therefore I am leaving that calculation to Gravity Forms.运输特定于重力形式中的字段,因此我将计算留给重力形式。

My issue is that if a user reduces the quantity of this product from their order (removes one of the items from the cart), it should re-calculate the price of each item of the same product within the cart, otherwise they could be over-charged ( an item that used to be the 6th is now the 4th, but the price remains the same, which it shouldn't )我的问题是,如果用户从他们的订单中减少了该产品的数量(从购物车中删除了其中一件商品),它应该重新计算购物车中同一产品的每件商品的价格,否则他们可能会结束-已收费(以前是第 6 项,现在是第 4 项,但价格保持不变,这不应该

Therefore , I would like to re-calculate the price of each item in the cart, based on the quantity of this particular product, every time something is removed from the cart.因此,每次从购物车中取出物品时,我想根据该特定产品的数量重新计算购物车中每件商品的价格。

--- EDIT --- - - 编辑 - -

The above code works, but I'm realizing the issue must be a custom loop I'm using to display the prices...上面的代码有效,但我意识到问题必须是我用来显示价格的自定义循环......

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product = $cart_item['data'];
    if( 123 == $_product->post->ID ) {
        $price_not_updated = $cart_item['data']->price;
    }
}

I figured it out... I looked at the woocommerce cart docs and essentially realized that the prices I was getting had yet to be calculated.我想通了……我查看了 woocommerce 购物车文档,基本上意识到我得到的价格还没有计算出来。 So, before running the loop, I had to do the action that I was initially hooking into to change the prices.因此,在运行循环之前,我必须执行最初用于更改价格的操作。

Thanks for your help!谢谢你的帮助!

function getUpdatedCartPrices() {

    do_action( 'woocommerce_before_calculate_totals', WC()->cart );

    $horray_updated_prices_works = array();

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product = $cart_item['data'];
        if( 123 == $_product->post->ID ) {

            $horray_updated_prices_works[] = $cart_item['data']->price;

        }
    }

}

I changed the function to have a second parameter so that you can use it dynamically with any product as long as you call the proper id.我将函数更改为具有第二个参数,以便您可以在任何产品中动态使用它,只要您调用正确的 id。 I also set the function return value to either a success or error message.我还将函数返回值设置为成功或错误消息。 This way you can know if it was changed, and if so, to what price.这样你就可以知道它是否被改变了,如果改变了,价格是多少。 Hope this helps.希望这可以帮助。 by the way, having this function called again when the cart is updated should solve the recalculation issue.顺便说一下,当购物车更新时再次调用这个函数应该可以解决重新计算的问题。 It would be great if i could see the code for your $function_to_get_new_price() function.如果我能看到您的 $function_to_get_new_price() 函数的代码,那就太好了。

add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );

function change_cart_item_price($id, $cart_object ) {

    foreach ( $cart_object->cart_contents as $key => $value ) {

        if( $id == $value['data']->id ) {

            $new_price = function_to_get_new_price( $value, get_current_user_id( ) );
            $value['data']->price = $new_price;

            $successMsg = "The price of item $value['data']->id was set to $value['data']->price".;
           return $successMsg;

        }else{
           $errorMsg = "Price was not changed!";
           return $errorMsg;
        }
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM