php商品折扣,php – 基于产品总数的某些类别的折扣

– 更新 –

这是应该方便您的要求的东西.

此功能将为购物车添加折扣:

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

function custom_cart_discount( $cart_object ) {

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

return;

// Define HERE your targeted product category (id, slug or name are accepted)

$category = 'posters';

// Set the price for Five HERE

$price_x5 = 5;

// initializing variables

$calculated_qty = 0;

$calculated_total = 0;

$discount = 0;

// Iterating through each cart item

foreach($cart_object->get_cart() as $item_key => $item):

// Make this discount calculations only for products of your targeted category

if(has_term($category, 'product_cat', $item['product_id'])):

$item_price = $item["data"]->price; // The price for one (assuming that there is always 2.99)

$item_qty = $item["quantity"];// Quantity

$item_line_total = $item["line_total"]; // Item total price (price x quantity)

$calculated_qty += $item_qty; // ctotal number of items in cart

$calculated_total += $item_line_total; // calculated total items amount

endif;

endforeach;

// ## CALCULATIONS (updated) ##

if($calculated_qty >= 5):

for($j = 5, $k=0; $j <= $calculated_qty; $j+=5,$k++); // Update $k=0 (instead of $k=1)

$qty_modulo = $calculated_qty % 5;

$calculation = ( $k * $price_x5 ) + ($qty_modulo * $item_price);

$discount -= $calculated_total - $calculation;

endif;

// Adding the discount

if ($discount != 0)

$cart_object->add_fee( __( 'Quantity discount', 'woocommerce' ), $discount, false );

// Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值