英文:
Add a discount on selected categories provided they are the second item in the cart
问题
这是您的翻译请求的部分:
"我在产品卡中提供特定类别的预售折扣。我在这里找到了这样的代码,但即使购物车中只有来自这些类别的商品,它也可以工作。
是否可以添加条件,只有在购物车中已经有至少1件不属于所选类别的产品时才能工作?
示例:
我添加了一个类别:椅子,如果用户进入该类别并只购买该类别的商品,折扣将不会应用。如果用户进入任何其他类别,例如桌子,将产品Table添加到购物车,然后再添加所需类别椅子的商品,折扣将会应用。我们想提供折扣,但只作为额外的产品。"
英文:
I am in the product cards to offer presales from specific categories with a discount.I found here such a code but it works even if there are only goods from these categories in the cart.
Is it possible to add a condition so that it works only when the cart already has at least 1 product not from the selected categories?
Example:
I added a category: Chairs, if the user goes to it and buys goods only from this category, the discount will not be applied, if he goes to any other category, for example, Tables, add the product Table to the cart and then when adding goods from the desired category Chairs, the discount will be applied. We want to give a discount, but only as an additional product.
/**
* Добавление скидок на товар в корзине
* @param WC_Cart $cart
*/
add_action( 'woocommerce_cart_calculate_fees', 'x_products_discount' );
function x_products_discount( $cart ) {
// Settings below
$categorise_ids = array(1795,1732,1725); // <== Your defined product Ids
$percentage = 10; // <== discount in percentage (10% here)
$found_ids = array();
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ) {
// Loop through defined product Ids
foreach( $product_ids as $product_id ) {
if( in_array( $product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found_ids[$product_id] = $product_id;
break;
}
}
}
// Discount part
if( count( $found_ids ) === count( $product_ids ) ) {
$cart->add_fee( ( 'Скидка', 'woocommerce' ), -( $cart->subtotal * $percentage / 100 ) );
}
}
Tried this code, it works, but we need it not to work if the cart only has the categories in the code
答案1
得分: 1
你的代码中有一些错误,请尝试使用以下修订后的代码:
/**
* 向购物车商品添加折扣
* @param WC_Cart $cart
*/
function products_category_based_discount( $cart ) {
// 以下是设置
$categories_ids = array(1795,1732,1725); // <== 你定义的产品类别ID
$percentage = 10; // <== 折扣百分比(这里是10%)
$found_category = $found_other = false;
// 遍历购物车商品
foreach ( $cart->get_cart() as $item ) {
if( has_term( $categories_ids, 'product_cat', $item['product_id'] ) ) {
$found_category = true;
} else {
$found_other = true;
}
}
// 折扣部分
if( $found_category && $found_other ) {
$cart->add_fee( __( '折扣', 'woocommerce' ), -( $cart->subtotal * $percentage / 100 ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'products_category_based_discount' );
它应该可以工作。
英文:
There are some mistakes in your code, try instead the following revisited code:
/**
* Adding discounts to cart items
* @param WC_Cart $cart
*/
function products_category_based_discount( $cart ) {
// Settings below
$categories_ids = array(1795,1732,1725); // <== Your defined product category Ids
$percentage = 10; // <== discount in percentage (10% here)
$found_category = $found_other = false;
// Loop through cart items
foreach ( $cart->get_cart() as $item ) {
if( has_term( $categories_ids, 'product_cat', $item['product_id'] ) ) {
$found_category = true;
} else {
$found_other = true;
}
}
// Discount part
if( $found_category && $found_other ) {
$cart->add_fee( __( 'Discount', 'woocommerce' ), -( $cart->subtotal * $percentage / 100 ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'products_category_based_discount' );
It should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论