简体   繁体   English

WooCommerce 购物车折扣:根据商品数量免费赠送一件商品

[英]WooCommerce cart discount: one item free based on item quantity

I'm trying to implement a custom discount rule to the cart.我正在尝试对购物车实施自定义折扣规则。 Basically there is WooCommerce and the site is selling t-shirts.基本上有 WooCommerce,该网站正在销售 T 恤。 There is a current promotion that if you buy 3 t-shirts, you have to pay only for 2 and the one with the lowest price you get for free.目前有一项促销活动,如果您购买 3 件 T 恤,您只需支付 2 件的费用,并且免费获得价格最低的一件。

I created a custom function using the hook woocommerce_cart_calculate_fees and so far it's working.我使用钩子woocommerce_cart_calculate_fees创建了一个自定义 function ,到目前为止它正在运行。

Here is my code:这是我的代码:

add_action( 'woocommerce_cart_calculate_fees', 'iom_add_custom_discount', 10, 1 );
function iom_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $item_prices = array();
    $in_cart = true;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        if ( has_term( 'detski-bodita', 'product_cat', $cart_product->get_id() ) ) {
            $in_cart = true;
        }else {
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price();
        }

    }

    if( $in_cart ) {
        $count_ids = count($product_ids);
        asort( $item_prices ); //Sort the prices from lowest to highest
        
        $count = 0;
        if( $count_ids == 3 ) { 
           foreach( $item_prices as $id => $price ) {
                if( $count >= 1 ) {
                    break;
                }
                //$product = wc_get_product( $id );
                //$price = $product->get_price();
                $discount -= ($price * 100) / 100;
                $count++;
           }
       }

    } 

    if( $discount != 0 ){
        $wc_cart->add_fee( 'Отстъпка', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

The discount is displayed and applied.显示并应用折扣。 But, it appears that it only works if I have 3 different products in the cart.但是,它似乎只有在我的购物车中有 3 种不同的产品时才有效。 If I have 1 product with quantity 2 and 1 product with quantity 1 it's not working.如果我有 1 件数量为 2 的产品和 1 件数量为 1 的产品,则无法使用。

How to tweak the function to make it work for item quantity count instead?如何调整 function 使其适用于项目数量计数?

Here is the screenshot of the cart page:这是购物车页面的屏幕截图:

在此处输入图像描述


Edit:编辑:

Explanation about product categories in the discount:折扣商品类别说明:

The discount should only be applied if 3 items from the same category are in the cart.只有当购物车中有 3 件来自同一类别的商品时,才应应用折扣。

For example, the categories are t-shirts and hoodies.例如,类别是 T 恤和连帽衫。 If I have 3 t-shirts in my cart, the discount should be applied.如果我的购物车中有 3 件 T 恤,则应享受折扣。 If I have 2 t-shirts and 1 hoodie the discount should not be applied.如果我有 2 件 T 恤和 1 件连帽衫,则不应享受折扣。

After some testing and stuff, I managed to make it work as described.经过一些测试和东西,我设法让它按照描述的那样工作。 Here is the code:这是代码:

add_action( 'woocommerce_cart_calculate_fees', 'iom_add_custom_discount', 10, 1 );
function iom_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $item_prices = array();
    $in_cart = false;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        // var_dump($cart_product->get_parent_id());
        // var_dump(has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() ));

        // here we check if the products in the cart are in category 'detski-bodita'.
        // since these are variation products, we check if their parents have the category.
        // if they do we add them the $in_cart boolean to true.
        if ( has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() ) ) {
            $in_cart = true;
            // var_dump($cart_product);
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price(); 
        } else {
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price(); 
        }

    }

    // here we chek if we have products with $in_cart boolean to true 
    // and if they are in category 'detski-bodita'
    if( $in_cart && has_term( 'detski-bodita', 'product_cat', $cart_product->get_parent_id() )) {
        // var_dump($item_prices);
        $count_ids = count($product_ids); // We count the items we have
        asort( $item_prices ); //Sort the prices from lowest to highest
        

        $count = 0; // we add another counter for the products that will be discounted.
        // here we check if the minimum amount of products is met for the discount.
        if( $count_ids >= 3 ) { 
            foreach( $item_prices as $id => $price ) {
                if( $count >= 1 ) {
                // var_dump($price)
                    break;
                }
                //$product = wc_get_product( $id );
                //$price = $product->get_price();
                $discount -= ($price * 100) / 100; // this is the discount that we apply - 100%
                $count++; // increase the counter in order to stop after the max amount of discounted products
           }
       }

    }

    if( $discount != 0 ){
        $wc_cart->add_fee( 'Отстъпка', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

I'm still testing it, but so far it's working.我仍在测试它,但到目前为止它正在工作。 Maybe the main problem before was that I didn't considered that the products are with variations and the category parsing was failing.可能之前的主要问题是我没有考虑到产品有变体,类别解析失败。 When I added 'get_parent_id()' I was able to get the correct category.当我添加“get_parent_id()”时,我能够获得正确的类别。 Now I just have to figure out the problem with the quantity.现在我只需要弄清楚数量的问题。 For example if one product from the category have quantity of 2 and one have quantity of 1 or if one product from the category have quantity of 3 or more.例如,如果类别中的一种产品的数量为 2,一种产品的数量为 1,或者类别中的一种产品的数量为 3 或更多。 Would really appreciate some help on it or any ideas for improvement.真的很感激一些帮助或任何改进的想法。 :) Thanks:) :) 谢谢:)

EDIT: It appears that if I select 3 products from the right category and 1 product from any other, the discount is applied.编辑:看来,如果我 select 3 件产品来自正确的类别和 1 件产品来自任何其他产品,则应用折扣。 But when I remove one of the items that are from the right category, the discount is still applied, when it shouldn't.但是,当我从正确的类别中删除其中一件商品时,折扣仍然适用,但不应该。 :( How could I fix this? Thanks:) :( 我该如何解决这个问题?谢谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 购物商品折扣基于Woocommerce 3中的数量 - Cart item discount based on quantity in Woocommerce 3 折扣基于 WooCommerce 购物车中某个类别的商品数量计数 - Discount based on item quantity count for a category in WooCommerce cart Woocommerce 3中的购物车项目数量累进百分比折扣 - Cart item quantity progressive percentage discount in Woocommerce 3 Woocommerce 基于数量的每件商品百分比折扣 - Woocommerce percentage discount per item based on quantity 根据商品数量有条件地按购物车商品添加折扣 - Adding a discount by cart items conditionally based on the item quantity Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 如何将特定商品的 WooCommerce 购物车商品数量限制为一个 - How to restrict WooCommerce cart item quantity to one for specific item 基于 Woocommerce 中购物车项目数的有条件累进百分比折扣 - Conditional progressive percentage discount based on cart item count in Woocommerce 基于Woocommerce中特定产品变化的购物车项目折扣 - Cart item discount based on specific product variation in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM