【问题标题】:Woocommerce cart quantity change stop changing the final priceWoocommerce 购物车数量更改停止更改最终价格
【发布时间】:2020-08-16 18:37:27
【问题描述】:

默认情况下,当 woocommerce 购物车数量发生变化时,它会通过将商品价格乘以数量来更新商品价格。我想更改默认行为并确保在购物车中更改数量时不会更改单击添加到购物车时传递的默认价格

我已添加此操作挂钩,但我不知道如何在购物车中的数量发生变化时停止价格变化。

   add_action( 'woocommerce_after_cart_item_quantity_update', 'on_quantity_changed_in_cart', 20, 4 );

   function on_quantity_changed_in_cart( $cart_item_key, $quantity, $old_quantity, $cart){
     if( ! is_cart() ) return; // Only on cart page

     //here stop price from been changed by default
  }

【问题讨论】:

  • 我不知道有一个钩子会阻止价格调整。您可以做的是调整完成价格计算的挂钩,以便它应用与默认计算不同的计算,取决于您的意愿? P.s. “最终价格”是什么意思?订单总额还是订单项总额?
  • @7uc1f3r 订单总数和订单项总数。
  • 您的问题与question类似同时,我们是WooCommerce的几个版本,所以可能会有一些变化或简化。
  • 最好的办法是用数量值替换购物车页面上的数量字段作为显示的不可编辑值,或者使该数量字段只读...所以客户无法编辑购物车中的购物车商品数量,只能删除购物车商品...否则很复杂:see this related thread
  • @7uc1f3r 谢谢这工作我也为这个问题发布一个答案。

标签: php wordpress woocommerce


【解决方案1】:

寻找“Disable Woocommerce cart line item quantity price calculation”回答线程,我发现我需要重写一个动作挂钩来替换每行的最终成本:

  add_filter('woocommerce_cart_product_subtotal', [$this, 'filter_woocommerce_cart_product_subtotal'], 10, 4);

  public function filter_woocommerce_cart_product_subtotal($product_subtotal, $product, $quantity, $cart){
        $sub_value = 0;
        foreach ($cart->cart_contents as $hash => $value) {
            if ($value["product_id"] === wc_get_product($product)->get_id()) {
                $sub_value =  $value["wcform-custom_price"];

                 //wcform-custom_price is passed from when adding to the cart.
            }
        };

        return wc_price($sub_value);
  }

现在在总数上我也覆盖了以下内容

 add_action( 'woocommerce_calculate_totals', [$this,'custom_item_price'],20,1);

 add_filter( 'woocommerce_calculated_total', [$this,"calculateFinalTotal"], 20, 2 );


   public function custom_item_price( $wc_cart ) {
        $cart_contents_total = 0;

        foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ){
            $cart_contents_total += $cart_item["wcform-custom_price"];
        }

        $wc_cart->subtotal = $cart_contents_total;
    }



   public function calculateFinalTotal($total,$cart){
        $cart_contents_total = 0;
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
            $cart_contents_total += $cart_item["wcform-custom_price"];
        }
        return   $cart_contents_total;;///print_r($cart->get_cart());
    }

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 2017-09-10
    • 2014-09-04
    • 2021-04-19
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多