0
votes

By default when woocommerce cart quantity is changed it updates the item price by multiplying it by the quantity. I would like to change the default behaviour and ensure that when in the cart when the quantity is changed it doesnt change the default price passed when add to cart is clicked

I have added this action hook but i cannot figure out how to stop price change when quantity is changed in a cart.

   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
  }
1
I am not aware of a hook that prevents the price from being adjusted. What you could do is adjust the hook where the price calculation is done so that it applies a different calculation than the default one, depending on your wishes? P.s. what do you mean by the 'final price'? the order total or the line item total?7uc1f3r
@7uc1f3r Its both on order total and line item total.Geoff
Your question is similar to this question In the meantime, we are a few versions of WooCommerce further, so there may be some things changed or simplified.7uc1f3r
The best thing should be to replace quantity fields on cart page by the quantity value as a displayed noot editable value, or to make that quantity fields read only... So customer can't edit cart item quantities in cart and is only be able to remove cart items... Otherwise is quiet complicated: see this related threadLoicTheAztec
@7uc1f3r thanks this works ill post an answer for this as well.Geoff

1 Answers

0
votes

Looking to "Disable Woocommerce cart line item quantity price calculation" answer thread, I found out I need to override an action hook to replace the final cost per line:

  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);
  }

Now on the totals as well I overriden the following

 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());
    }