[英]Woocommerce percentage discount per item based on quantity
Based on WooCommerce Cart Quantity Base Discount answer code, I apply a discount from xx products on shopping cart with the following:根据WooCommerce Cart Quantity Base Discount答案代码,我对购物车上的 xx 产品应用了以下折扣:
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## -------------- DEFINIG VARIABLES ------------- ##
$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();
$cart_total_excl_tax = $cart_object->subtotal_ex_tax;
## ----------- CONDITIONAL PERCENTAGE ----------- ##
if( $cart_item_count <= 17 )
$percent = 0;
elseif( $cart_item_count >= 18 && $cart_item_count <= 120 )
$percent = 10;
## ------------------ CALCULATION ---------------- ##
$discount -= ($cart_total_excl_tax / 100) * $percent;
## ---- APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
if( $percent > 0 )
$cart_object->add_fee( __( 'Remise sur la quantité 10%', 'woocommerce' ), $discount, true);
}
But I would like to manage the discount from quantity for each product.但我想管理每个产品的数量折扣。
Do you have any tips for that?你有什么建议吗?
The following will make a percentage discount based on item quantity:以下将根据商品数量进行百分比折扣:
add_action( 'woocommerce_cart_calculate_fees','cart_quantity_discount' );
function cart_quantity_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount = 0; // Initializing
$percent = 10; // Percentage
// Loop through cat items
foreach ( $cart->get_cart() as $cart_item ) {
$subtotal = $cart_item['line_total'];
// Calculation by Item based on quantity
if( $cart_item['quantity'] > 17 ) {
$discount += $cart_item['line_total'] * $percent / 100;
}
}
if( $discount > 0 ) {
$cart->add_fee( __( "Item quantity discount", "woocommerce" ), -$discount, true );
}
}
Code goes on functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 It should works.它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.