php - 使用过滤器或 Hook 修改购物车总金额

标签 php wordpress woocommerce

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

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

关于php - 使用过滤器或 Hook 修改购物车总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29054877/

相关文章:

php - Twitter Bootstrap 导航栏 - 表单输入之间的空间

css - 在 Divi Wordpress 主题的 slider 上移动按钮的 CSS 代码是什么?

php - 几秒钟后突出显示不同的链接

php - Woocommerce,根据运输类别隐藏运输方式

php - 在帐单地址中显示自定义字段

javascript - 数据更改时饼图数据不更新

php - 将 WordPress 中的下拉列表或文本字段中的值传递到functions.php 中的 MySQL 查询

javascript - Wordpress 中奇怪的滚动问题

php - 将多个产品添加到购物车(如果它们尚未在购物车中)

php - 将 Wordpress 菜单 css 类添加到 <span class ="">