英文:
Condition for Quantity and Price for Woocommerce
问题
我有代码来检查购物车中商品的数量,然后根据数量进行折扣。我如何对价格低于500的商品进行额外检查(对于这些商品,折扣应为0)。
以下代码基于 https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080 的答案:
英文:
Ii've code for checking quantity of goods in cart, and then it makes a discount based on quantity. How can I make additional check on price for goods below 500 (for these goods, discount should be 0).
The following code is based on https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080 answer:
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;
/*foreach( $cart_object->get_cart() as $cart_item_key => $cart_item ) {
$cart_item = $cart_object->get_cart_contents_count();
}*/
// Assign each product's price to its cart item key (to be used again later)
## ----------- CONDITIONAL PERCENTAGE ----------- ##
if( $cart_item_count <= 1 )
$percent = 5;
elseif( $cart_item_count <= 2 )
$percent = 10;
elseif( $cart_item_count <= 3 )
$percent = 15;
elseif( $cart_item_count <= 4 )
$percent = 20;
elseif( $cart_item_count >= 5 )
$percent = 25;
else {
$percent = 0;
}
## ------------------ CALCULATION ---------------- ##
$discount -= ($cart_total_excl_tax / 100) * $percent;
## ---- APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
if( $percent > 0 )
$cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}
Should be checking for quantity and price below, for example, 500 (the discount should be 0). For another goods with condition: price is higher 500 and the corresponding quantity, its own discount:
1 - 5%
2 - 10%
3 - 15%
etc.
答案1
得分: 1
以下是您提供的代码的翻译部分:
*已更新*
以下代码仅会计算超过500的项目,以根据该项目数量应用百分比折扣:
```php
add_action( 'woocommerce_cart_calculate_fees','wc_cart_intem_quantity_discount' );
function wc_cart_intem_quantity_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// 初始化变量
$min_item_amount = 500;
$discount = $items_count = $percent = $items_subtotal = 0;
// 循环遍历购物车项目
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
// 仅计算超过500的项目
if( $cart_item['data']->get_price() >= $min_item_amount ) {
$items_count += $cart_item['quantity'];
$items_subtotal += $cart_item['line_subtotal'];
}
}
// 条件百分比
if ($items_count == 1) {
$percent = 5;
} elseif ($items_count == 2) {
$percent = 10;
} elseif ($items_count == 3) {
$percent = 15;
} elseif ($items_count == 4) {
$percent = 20;
} elseif ($items_count >= 5) {
$percent = 25;
}
// 折扣(可税)
if( $items_count > 0 ) {
// 计算
$discount -= ($items_subtotal / 100) * $percent;
$cart->add_fee( __( "数量折扣 $percent%", "woocommerce" ), $discount, true);
}
}
基于以下网址的代码:https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080
<details>
<summary>英文:</summary>
*Updated*
The following code will only count items that are above 500, to apply a percentage discount based on that item count:
```php
add_action( 'woocommerce_cart_calculate_fees','wc_cart_intem_quantity_discount' );
function wc_cart_intem_quantity_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// INITIALIZING VARIABLES
$min_item_amount = 500;
$discount = $items_count = $percent = $items_subtotal = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Only counting items that are above 500
if( $cart_item['data']->get_price() >= $min_item_amount ) {
$items_count += $cart_item['quantity'];
$items_subtotal += $cart_item['line_subtotal'];
}
}
// CONDITIONAL PERCENTAGE
if ($items_count == 1) {
$percent = 5;
} elseif ($items_count == 2) {
$percent = 10;
} elseif ($items_count == 3) {
$percent = 15;
} elseif ($items_count == 4) {
$percent = 20;
} elseif ($items_count >= 5) {
$percent = 25;
}
// DISCOUNT (TAXABLE)
if( $items_count > 0 ) {
// Calculation
$discount -= ($items_subtotal / 100) * $percent;
$cart->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}
}
Code based on: https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论