Woocommerce更新购物车按钮操作后哪个挂钩正在运行 [英] Which Hook is running after Woocommerce update cart button action

查看:64
本文介绍了Woocommerce更新购物车按钮操作后哪个挂钩正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在购物车页面中单击更新购物车按钮后,我需要知道哪个挂钩正在运行。

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

在购物车页面中,我们有4个按钮,更新购物车继续购物进行结帐 apply coupon

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 /

请帮助。

例如

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

此功能有很多问题,我由于某些原因或某些其他目的而写了此功能woocommerce是没有正确计算优惠券价格,所以我为手动更新购物车中的产品价格编写了此功能。如果购物车价值超过100,我们提供免费运送其他虎钳,我们将加5。甚至该功能也无法正常工作。

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创建新的折扣功能

推荐答案

您应该尝试 woocommerce_update_cart_action_cart_updated 动作挂钩。我已经重新审视了您的代码。尝试以下操作:

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

代码已输入您的活动子主题(或活动主题)的function.php文件。未测试。

更新 WC_Cart set_total_price ()方法不存在…我已将其替换为现有的 WC_Cart set_total()

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

这篇关于Woocommerce更新购物车按钮操作后哪个挂钩正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆