Which Hook is running after Woocommerce update cart button action

Manik picture Manik · Mar 26, 2018 · Viewed 13.5k times · Source

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

Answer

LoicTheAztec picture LoicTheAztec · Mar 26, 2018

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

add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){

    $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;

        WC()->cart->set_total( $new_value );

        if ( $cart_updated ) {
            // Recalc our totals
            WC()->cart->calculate_totals();
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Untested. It could work.

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