【问题标题】:woocommerce_before_calculate_totals fires twicewoocommerce_before_calculate_totals 触发两次
【发布时间】:2022-02-05 23:18:17
【问题描述】:

正在向网站添加内容,想要修改购物车中的价格。有以下代码:

function apd_product_custom_price($cart_item_data, $product_id)
{
    if (isset($_POST['use_rewards']) && !empty($_POST['use_rewards'])) 
    {
       $cart_item_data['use_rewards'] = $_POST['use_rewards'];
    }
    return $cart_item_data;
}

add_filter('woocommerce_add_cart_item_data', 'apd_product_custom_price', 99, 2);

function apd_apply_custom_price_to_cart_item($cart_object)
{
   if( !WC()->session->__isset( 'reload_checkout' )) {
        foreach ($cart_object->cart_contents as $value) {
            if(isset($value['use_rewards'])) {
                $price = $value['data']->get_price() - 
   $value['use_rewards'];
                $value['data']->set_price($price);
            }
        }
    }

}
add_action('woocommerce_before_calculate_totals', 'apd_apply_custom_price_to_cart_item',10);

由于某种原因,钩子 woocommerce_before_calculate_totals 触发了两次。如果我只用 echo 1 替换函数 apd_apply_custom_price_to_cart_item($cart_object) 中的代码;它在购物车页面中显示 11。有人可以帮忙吗?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我忘记了我应该参考的链接以获得解决方案。您可以在函数顶部使用它:

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    

    【讨论】:

      【解决方案2】:

      很可能有一些其他插件与“woocommerce_before_calculate_totals”操作挂钩。

      尝试一一禁用插件,看看女巫导致了这种行为。

      就我而言,我发现 WooCommerce 多语言插件与 woocommerce_before_calculate_totals 操作挂钩。

      所以我这样做了:

      add_action( 'woocommerce_before_calculate_totals', '_wm_ponc_upte_prc', 99 );
      function _wm_ponc_upte_prc( $cart_object ) {
          if ( !WC()->session->__isset( "reload_checkout" ) ) {
              foreach ( WC()->cart->get_cart() as $key => $value ) {
                  // checking if user checked checkbox (optional)
                  if ( isset( $value['wm_ponc_fee'] ) && ( $value[ 'wm_ponc_fee' ] === 'yes' ) ) {
                      // your calculations ....
                      // and Remove your action after you are done.
                      remove_action( 'woocommerce_before_calculate_totals', '_wm_ponc_upte_prc', 99 );
                  }
              }
          }
      }
      

      我在自定义计算后删除了操作。就是这样。

      【讨论】:

        【解决方案3】:

        我看到了完全相同的问题——我的钩子函数触发了两次,导致期权价格以成本的 2 倍增加。我的解决方案是加载产品,从中获取价格,然后将我的增量成本添加到其中。

        函数 tbk_woo_update_option_price($cart_object) {

        $option_price = 3.50;
        foreach ( $cart_object->get_cart() as $cart_item_key => $cart_item ) {
        
                $item_id = $cart_item['data']->id;
                //Hook seems to be firing twice for some reason... Get the price from the original product data, not from the cart...
                $product = wc_get_product( $item_id );
                $original_price = $product->get_price();
                $new_price = $original_price + $option_price;
                $cart_item['data']->set_price( $new_price );
        }
        

        } add_action('woocommerce_before_calculate_totals', 'tbk_woo_update_option_price', 1000, 1);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多