【问题标题】:Can't change product price with woocommerce_after_calculate_totals无法使用 woocommerce_after_calculate_totals 更改产品价格
【发布时间】:2022-01-27 17:04:36
【问题描述】:

我想检查一些条件,如果为真,我想将所有产品的价格更新为 10 美元。 使用此功能一切正常:

add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if(true) { // some condition
        foreach ( $cart_object->get_cart() as $key => $val ) {
            $current_product = $val['data'];
            $current_product->set_price( 10 );
        }
    }
}

但是当用户使用优惠券代码时,上述功能停止工作,价格不会改变。 为什么?

谢谢。

【问题讨论】:

  • 您是否还有未包含的其他代码?将上述内容与基本订单优惠券一起使用,效果很好。你能提供更多细节吗?
  • 嗨,我知道问题的原因,Woocommerce 的折扣插件。我可以做些什么来避免这种情况吗? @MarkTruitt
  • 假设它是免费的,您是否有指向该插件的链接?
  • 我现在不知道插件的确切名称,但我想知道是否有办法让我的功能比插件的功能更强大。
  • 我认为该插件也使用了 woocommerce_before_calculate_totals 钩子,这就是问题所在。

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

您可以提高或降低优先级。将其设置为 999 会将其作为最后运行的东西。

add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 999, 1 );
function conditionally_change_cart_items_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if(true) { // some condition
        foreach ( $cart_object->get_cart() as $key => $val ) {
            $current_product = $val['data'];
            $current_product->set_price( 10 );
        }
    }
}

【讨论】:

  • 谢谢你,但它不起作用
  • 那我需要这个插件。
  • 也许我会关闭那个插件。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-05-31
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多