基于PHP的购物车代码,php – 基于购物车项目计数的购物车折扣,仅适用于未售出的商品...

这是一个自定义挂钩功能,如果购物车中有5件或更多商品且没有销售产品,将适用于购物车折扣:

add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);

function custom_discount( $cart ){

if ( is_admin() && ! defined( 'DOING_AJAX' ) )

return;

// Only when there is 5 or more items in cart

if( $cart->get_cart_contents_count() >= 5):

// Initialising variable

$is_on_sale = false;

// Iterating through each item in cart

foreach( $cart->get_cart() as $cart_item ){

// Getting an instance of the product object

$product = $cart_item['data'];

// If a cart item is on sale, $is_on_sale is true and we stop the loop

if($product->is_on_sale()){

$is_on_sale = true;

break;

}

}

## Discount calculation ##

$discount = $cart->subtotal * -0.1;

## Applied discount (no products on sale) ##

if(!$is_on_sale )

$cart->add_fee( '10% discount', $discount);

endif;

}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.

此代码经过测试并完美运行.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值