首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce购物车折扣:一种基于项目数量的免费商品

问WooCommerce购物车折扣:一种基于项目数量的免费商品
EN

Stack Overflow用户
提问于 2020-08-04 06:04:29
回答 1查看 595关注 0票数 1

我正在尝试实现一种对购物车的自定义折扣规则。基本上是有WooCommerce和该网站出售t恤。目前的促销活动是,如果你买了3件t恤,你只需支付2件,而另一件则是你免费得到的最低价格。

我使用钩子woocommerce_cart_calculate_fees创建了一个自定义函数,到目前为止它还在工作。

这是我的代码:

代码语言:javascript
复制
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)
    }
}

显示并应用折扣。但是,它似乎只有当我有3种不同的产品在购物车。如果我有一个产品的数量2和1个产品的数量1,它是无效的。

如何调整功能,使其用于项目数量计数?

以下是购物车页面的截图:

​

​

编辑:

关于折扣产品类别的说明:

只有当同一类别的中有3项在购物车中时,才能使用折扣。

例如,分类是t恤和帽衫.如果我的手推车里有3件t恤衫,应该打折扣.如果我有2件t恤和1件连帽衫,就不应该打折.

EN

回答 1

Stack Overflow用户

发布于 2020-08-04 11:06:19

经过一些测试和其他的工作,我设法使它的工作,如所述。以下是代码:

代码语言:javascript
复制
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)
    }
}

我还在测试,但到目前为止还能用。可能之前的主要问题是,我没有考虑到产品有变化,类别解析失败了。当我添加'get_parent_id()‘时,我能够得到正确的类别。现在我只需要找出数量的问题。例如,如果类别中的一个产品的数量为2,而另一个产品的数量为1,或者该类别的一个产品的数量为3或更多。会非常感谢你在这方面的帮助或任何改进的想法。)(谢谢:)

编辑:如果我从正确的类别中选择了3种产品,从任何其他种类中选择了1种产品,就会得到折扣。但是,当我从正确的类别中删除其中一个项目时,折扣仍然被应用,而它不应该被应用。:(我如何解决这个问题?谢谢:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63241064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档