Woocommerce 根据数量为每件商品提供百分比折扣
Woocommerce percentage discount per item based on quantity
根据 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);
}
但我想管理每个产品的数量折扣。
你有什么建议吗?
以下将根据商品数量进行百分比折扣:
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 );
}
}
代码继续在您的活动子主题(或活动主题)的 functions.php 文件中。它应该有效。
根据 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);
}
但我想管理每个产品的数量折扣。
你有什么建议吗?
以下将根据商品数量进行百分比折扣:
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 );
}
}
代码继续在您的活动子主题(或活动主题)的 functions.php 文件中。它应该有效。