php 在 Woocommerce 3 中更改购物车项目价格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43324605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:48:51  来源:igfitidea点击:

Change cart item prices in Woocommerce 3

phpwordpresswoocommercecartproduct

提问by Archana

I am trying to change product price in cart using the following function:

我正在尝试使用以下功能更改购物车中的产品价格:

    add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price' 
     );
      function add_custom_price( $cart_object ) {
         foreach ( $cart_object->cart_contents as $key => $value ) {
         $value['data']->price = 400;
        } 
     }

It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+

它在 WooCommerce 2.6.x 版中正常工作,但在 3.0+ 版中不再工作

How can I make it work in WooCommerce Version 3.0+?

我怎样才能让它在 WooCommerce 3.0+ 版中工作?

Thanks.

谢谢。

回答by LoicTheAztec

Update(September 2018)

更新(2018 年 9 月)

With WooCommerce version 3.0+you need:

使用WooCommerce 3.0+ 版,您需要:

Here is the code:

这是代码:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        $item['data']->set_price( 40 );
    }
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

This code is tested and works.

此代码经过测试并有效。

Note:you can increase the hook priorityfrom 20to 1000(or even 2000)when using some few specific plugins or others customizations.

注意:您可以将钩子优先级20to (甚至10002000)当使用一些特定的插件或其他自定义。

Related:

有关的:

回答by Tony

With WooCommerce version 3.2.6, @LoicTheAztec's answer works for me if I increase the priority to 1000.

使用 WooCommerce 3.2.6 版,如果我将优先级提高到 1000,@LoicTheAztec 的答案对我有用。

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

I tried priority values of 10,99and 999but the price and total in my cart did not change (even though I was able to confirm with get_price()that set_price()had actually set the price of the item.

我试过的优先级值1099并且999但在我的车的价格和总没有变化(即使我能够证实与get_price()set_price()实际上已经设置的项目的价格。

I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.

我有一个自定义挂钩,可以向我的购物车添加费用,并且我正在使用添加产品属性的 3rd 方插件。我怀疑这些 WooCommerce“附加组件”会导致延迟,需要我延迟自定义操作。