9

i need to know which hook is running after clicking the update cart button in the cart page .

That is in cart page we have 4 button , update cart , continue shopping, proceed to checkout , apply coupon .

So i want to know which hook is run after update cart button is clicked . When customer click the update cart button after changing the quantity then i have to run a special function that can change total price in the cart , If some condition met , i will change the total price in the cart , and this total price need to pass to the checkout page also /

Please help .

For example

add_filter('after_update_cart_function_finished', 'special_function');

function special_function(){
   $applied_coupons= WC()->cart->get_applied_coupons();
   if(!empty($applied_coupons)){

   $new_value=WC()->cart->get_cart_subtotal();
   $discounted=WC()->cart->coupon_discount_totals;
   $discounted_value=array_values($discounted)[0];
   $new_value=$new_value-$discounted_value+100;
    WC()->cart->set_total_price($new_value);
    After this update all post_meta value that regarding to order total
   }


}

Please see the following custom function that i write for to change value in cart

 function action_woocommerce_cart_totals_after_order_total(  ) {
          $applied_coupons= WC()->cart->get_applied_coupons();
          if(!empty($applied_coupons)){


        $new_value=WC()->cart->get_cart_subtotal();
        $discounted=WC()->cart->coupon_discount_totals;
        $discounted_value=array_values($discounted)[0];
        $new_value=$new_value-$discounted_value;
        if($new_value<100){
            $new_value=$new_value+5;
        }

         ?>
         <style>
         .new-price-new{
             color:black;
             font-size: 17px;
         }
         </style>
         <script>
         jQuery(function($){
              $(".order-total .woocommerce-Price-amount.amount").text("£<?php  echo $new_value;?>");
              $(".order-total .woocommerce-Price-amount.amount").hide();
              $(".new-price").remove();
              $('.order-total .woocommerce-Price-amount.amount').after('<div class="new-price-new">£<?php  echo $new_value;?></div>');;
              $(".new-price-new").show();


         });


         </script>

         <?php 
      }
    else{
        ?>
         <script>
         jQuery(function($){
        $(".new-price-new").remove();
         });
         </script>
    <?php }   

}; 

add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_cart_totals_after_order_total', 10, 0 ); 

And this function have many problems , i write this function because of some reason or some other function woocommerce is not calculating coupon price correctly , so i write this function for to manually update the product price in cart .Here if the cart value is more than 100 we provide free shipping other vise we will add 5 . Even this function is not working properly .

Woocommerce Create new discount functionality

Manik
  • 513
  • 1
  • 7
  • 23

1 Answers1

7

Updated

You should try the woocommerce_update_cart_action_cart_updated filter hook. I have revisited your code a bit. Try this:

add_filter('woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated');
function on_action_cart_updated( $cart_updated ) {
    // Get applied coupons
    $applied_coupons = WC()->cart->get_applied_coupons();
    
    if( count( $applied_coupons ) > 0 ){
        $new_value        = WC()->cart->get_cart_subtotal();
        $discounted       = WC()->cart->coupon_discount_totals;
        $discounted_value = array_values($discounted)[0];
        $new_value        = $new_value-$discounted_value + 100;

        // Recalculate totals before
        if ( $cart_updated ) {
            WC()->cart->calculate_totals();
        }
        
        WC()->cart->set_total( $new_value );
        
        $cart_updated = false;
    }
    return $cart_updated;
}

Code goes in functions.php file of your active child theme (or in a plugin). It should work.

Note 1: The WC_Cart set_total_price() method doesn't exist… I have replaced it by existing WC_Cart set_total()

Note 2: Returning true, will trigger calculate_totals(), suppressing all changes, including the notice "cart was updated".

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • i noticed that there is an error coming . Call to undefined method WC_Cart::set_total_price() . – Manik Mar 27 '18 at 04:44
  • also please help to clarify my doubts , 1)If we update the total price using this hook , it will pass to checkout page also ? 2) This total price is saved as post meta ? so that i can use the update post_meta function ? – Manik Mar 27 '18 at 04:51
  • then can you answer like this , if applied coupon equals to coupon number than if product cat =A price= price*90/100 . if product cat=b then price is price *80/100 .After this update cart price , checkout price with new price – Manik Mar 27 '18 at 09:39
  • @LoicTheAztec My apologies, but since the code used the hook in the wrong way, I thought it might be helpful to change it for future visitors. – Philip Jul 30 '23 at 14:12
  • @LoicTheAztec Should the `calculate_totals` call not go before the new discount is calculated? So at the beginning of the function. (Since there might be item added/removed from the cart.) – Philip Jul 30 '23 at 14:14
  • @Philip If you put it as it was before, when there is no applied coupons, calculate_totals will be triggered 2 times: one time in our function and another time again after the hook, as `$cart_updated` is not set to false in this case (look at the [source code](https://woocommerce.github.io/code-reference/files/woocommerce-includes-class-wc-form-handler.html#source-view.696))… – LoicTheAztec Jul 30 '23 at 16:16
  • Exactly, that's why I put the `= false` in there. – Philip Jul 30 '23 at 18:27
  • But with your code version, if there is no applied coupons `calculate_totals()` is triggered twice (which has not a real impact, it just uses more resources). With my updated version, it will run once in all cases. – LoicTheAztec Jul 30 '23 at 18:35