使用过滤器或挂钩修改购物车总金额 [英] Modify cart total amount with filter or hook

查看:27
本文介绍了使用过滤器或挂钩修改购物车总金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在尝试编写一个过滤器或挂钩,它会根据购物车中商品的总量来修改购物车的总量.例如,如果购物车中有三件商品,我想从总订单中扣除二十美元.以下是我到目前为止的代码,非常感谢任何帮助!

Okay so I'm trying to write a filter or hook that will modify the total amount of the shopping cart depending on the total overall amount of items in the shopping cart. For example, if there are three items in the shopping cart, I want to deduct twenty dollars from the total order. Below is the code I have so far, any help is greatly appreciated!

add_filter('woocommerce_cart_contents_total', 'bundle_deals');

function bundle_deals( $cart_contents_total, $cart_contents_count) {
   global $woocommerce;

   if ($woocommerce->cart->get_cart()->cart_contents_count <= 3) {
     $cart_contents_total = $woocommerce->cart->get_cart()->cart_contents_total - 20.00;
   }

   return $cart_contents_total;
}

推荐答案

function woocommerce_cart_subtotal(  $cart_subtotal, $compound, $obj  ){

$t = 0;
foreach ( $obj->cart_contents as $key => $product ) : 

    $product_price = $product['line_total'];    

    foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : 

            $product_price  = 19;//wpq_9522_discount( $product['line_total'], $coupon->discount_type, $coupon->amount ); 

    endforeach; 

    $t += $product_price;

endforeach; 

return $cart_subtotal;//( $t > 0 ) ? sprintf( '<s>%s</s> %s',      
$cart_subtotal, wc_price( $t ) ) : $cart_subtotal ;
}

add_filter( 'woocommerce_cart_subtotal', 'woocommerce_cart_subtotal',      99, 3 );

这篇关于使用过滤器或挂钩修改购物车总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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