Add a discount on selected categories provided they are the second item in the cart

huangapple go评论65阅读模式
英文:

Add a discount on selected categories provided they are the second item in the cart

问题

我在产品卡中提供特定类别的预售折扣。我在这里找到了一段代码,但是即使购物车中只有这些类别的商品,它也会起作用。

是否可以添加一个条件,只有在购物车中已经有至少1个不属于所选类别的产品时才起作用?

示例:
我添加了一个类别:椅子,如果用户进入该类别并只购买该类别的商品,折扣将不会应用;如果他进入任何其他类别,例如桌子,将桌子添加到购物车中,然后再添加所需类别(椅子)的商品,折扣将应用。我们想要提供折扣,但只作为额外的产品。

/**
 * 添加购物车中的产品折扣
 * @param WC_Cart $cart
 */
add_action( 'woocommerce_cart_calculate_fees', 'x_products_discount' );
function x_products_discount( $cart ) {
    // 下面是设置
    $category_ids = array(1795,1732,1725); // <== 您定义的产品ID
    $percentage  = 10; // <== 折扣百分比(这里是10%)

    $found_ids   = array();
    
    // 循环遍历购物车中的商品
    foreach (WC()->cart->get_cart() as $cart_item ) {
        // 循环遍历定义的产品ID
        foreach( $category_ids as $category_id ) {
            if( in_array( $category_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                $found_ids[$category_id] = $category_id;
                break;
            }
        }
    }
    
    // 折扣部分
    if( count( $found_ids ) === count( $category_ids ) ) {
        $cart->add_fee( '折扣', -( $cart->subtotal * $percentage / 100 ) );
    }
}

尝试了这段代码,它可以工作,但是我们需要在购物车中只有代码中的类别时它不起作用。

英文:

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( &#39;woocommerce_cart_calculate_fees&#39;, &#39;x_products_discount&#39; );
function x_products_discount( $cart ) {
    // Settings below
    $categorise_ids = array(1795,1732,1725); // &lt;== Your defined product Ids
    $percentage  = 10; // &lt;== discount in percentage (10% here)

    $found_ids   = array();
    
    // Loop through cart items
    foreach (WC()-&gt;cart-&gt;get_cart() as $cart_item ) {
        // Loop through defined product Ids
        foreach( $product_ids as $product_id ) {
            if( in_array( $product_id, array( $cart_item[&#39;product_id&#39;], $cart_item[&#39;variation_id&#39;] ) ) ) {
                $found_ids[$product_id] = $product_id;
                break;
            }
        }
    }
    
    // Discount part
    if( count( $found_ids ) === count( $product_ids ) ) {
        $cart-&gt;add_fee( ( &#39;Скидка&#39;, &#39;woocommerce&#39; ), -( $cart-&gt;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( __( 'Discount', '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); // &lt;== Your defined product category Ids
    $percentage  = 10; // &lt;== discount in percentage (10% here)
    $found_category = $found_other = false;
    
    // Loop through cart items
    foreach ( $cart-&gt;get_cart() as $item ) {
        if( has_term( $categories_ids, &#39;product_cat&#39;, $item[&#39;product_id&#39;] ) ) {
            $found_category = true;
        } else {
            $found_other = true;
        }
    }
    
    // Discount part
    if( $found_category &amp;&amp; $found_other ) {
        $cart-&gt;add_fee( __( &#39;Discount&#39;, &#39;woocommerce&#39; ), -( $cart-&gt;subtotal * $percentage / 100 ) );
    }
}

add_action( &#39;woocommerce_cart_calculate_fees&#39;, &#39;products_category_based_discount&#39; );

It should work.

huangapple
  • 本文由 发表于 2023年8月9日 15:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定