Man*_*nik 7 php wordpress cart woocommerce hook-woocommerce
单击购物车页面中的更新购物车按钮后,我需要知道哪个挂钩正在运行。
也就是说,在购物车页面中,我们有 4 个按钮 , update cart, continue shopping, proceed to checkout, apply coupon。
所以我想知道点击更新购物车按钮后运行哪个钩子。当客户在更改数量后单击更新购物车按钮时,我必须运行一个可以更改购物车中总价的特殊功能,如果满足某些条件,我将更改购物车中的总价,并且该总价需要传递。也到结帐页面 /
请帮忙 。
例如
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
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅我编写的以下自定义函数以更改购物车中的值
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 );
Run Code Online (Sandbox Code Playgroud)
而且这个函数有很多问题,我写这个函数是因为某种原因或其他一些函数woocommerce没有正确计算优惠券价格,所以我写这个函数来手动更新购物车中的产品价格。这里如果购物车价值超过100 我们提供免费送货其他虎钳我们将添加 5。甚至这个功能也不能正常工作。
你应该试试woocommerce_update_cart_action_cart_updated动作钩子。我重新审视了你的代码。尝试这个:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。未经测试。它可以工作。
更新:该WC_Cart set_total_price()方法不存在......我已将其替换为现有WC_Cart set_total()
| 归档时间: |
|
| 查看次数: |
17222 次 |
| 最近记录: |