带有>的同一商品的Woocommerce有条件条件部分购物车折扣1个变化 [英] Woocommerce conditional part cart discount on the same product present with > 1 variation

查看:90
本文介绍了带有>的同一商品的Woocommerce有条件条件部分购物车折扣1个变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的woocommerce产品实际上是邮轮探险(两种产品=两种探险类型).对于每种产品,变化都包括巡游发生的几周(=日期). 所以我有20个不同星期的利古里亚海探险和其他20个星期的希腊探险.幸运的是,我只有2种产品可以处理(非常简单的情况)

My woocommerce products are actually cruise expeditions (two products = two expedition types). For each product, the variations consists of the weeks in which cruises take place (= dates). So I have Ligurian Sea Expeditions with 20 different weeks and Greece Expeditions with other 20 weeks. Fortunately I have just 2 products like that to deal with (a very simple situation)

客户通常选择一个星期的探险.但是,如果客户决定申请> 1周,我需要在第二(或第三)周提供10%的折扣.因此,第一周的价格为全价,但是第二周和第三周(如果有的话)的折扣为10%.

The customer usually chooses one week expedition. However I need to apply a 10% discount on the second (or third) week in case a customer decides to apply for > 1 week. Hence the first week is paid full price, but the second and (in case there is) the third week will be discounted 10%.

我提供了一个功能,该功能可以在两个星期内应用折扣.

I have come out with a function that enables to apply the discount in the case of two weeks.

function cart_discount() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $woocommerce; $cat_count = 0; 

    $cart = WC()->cart->get_cart();

    foreach($cart as $cart_item_key => $values) { 
        $product_id = $values['product_id']; // product ID (= cruise type)
        $variation_id = $values['variation_id']; // variation (=week)
        $cart_lines_total = $values["line_total"]; //variation total price
        $cart_lines_quantity = $values["quantity"];// variation quantity

            //$product = 1394 = Expedition Ligurian eng
            //$product = 1389 = Expedition Greece eng
            //$product = 13888 = Expedition Ligurian ita
            //$product = 13910 = Expedition Greece ita

        //I hereby add a condition as we do have the same cruises with students prices which are not eligible to this discount (and are stored with different product_id)
        if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
        {
            //put in a new array only the terms I need for the calculation later
            $cart_array []= array( $product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
        }
    }


        // discount percent is 10%
        $percent = -0.10;

    if ($cart_array[0][0] == $cart_array[1][0]) //if in the cart the same product is present two times
    {
        $discount = $percent * $cart_array[1][2] * $cart_array[1][3];
        $discount_text = __( 'Quantity discount', 'woocommerce' );
        WC()->cart->add_fee( $discount_text, $discount, false );
    }


}
add_action( 'woocommerce_cart_calculate_fees','cart_discount' );

此代码有很多限制,正如我之前说的那样,因为它不能解决某些情况,例如:

This code has many limitations as I said before, as it doesn't take in acccount some scenarios such as:

1)仅在购物车中只有同一产品的两种变体时交易:如果客户决定购买x周,我应该能够检查同一产品是否存在> 2种变体;

1) it deals only if in the cart tere are only two variations of the same product: in case a customer decides for x weeks to be purchased I should be able to check if the same product is present with > 2 variations;

2)它没有考虑到两种产品具有两种或两种以上矿石的可能性(例如,一个人在利古里亚海购买了两周,在希腊购买了两周)

2) It doesn't take into account the possibility having the two products with 2 ore more variations (ie.a person buying let's say two weeks in Ligurian Sea and two weeks in Greece)

如果有人可以帮助我改善我编写的代码,我将非常高兴!!

If somebody can help me in improving the code I wrote I would be very happy!!

推荐答案

这是我编写的最终代码(有效,我已经对其进行了测试)

Here is the final code I wrote (it works, I have tested it)

function cart_discount() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    global $woocommerce; 


            $cart = WC()->cart->get_cart();

            $has_coupons = count(WC()->cart->applied_coupons)>0?true:false;

            if(!$has_coupons) //verifies that other discounts are not added
            {
                  foreach($cart as $cart_item_key => $values) { 
                      $product_id = $values['product_id']; // product ID
                      $variation_id = $values['variation_id']; // product quantity
                      $cart_lines_total = $values["line_total"];
                      $cart_lines_quantity = $values["quantity"];

                          //products for which this kind of discount must be applicable:
                          //$product_id = 1394 = Spedizioni CSR eng
                          //$product_id = 1389 = Spedizioni IDP eng
                          //$product_id = 13888 = Spedizioni CSR ita
                          //$product_id = 13910 = Spedizioni IDP ita

                      if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
                      {
                          $cart_array []= array( $product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
                      }
                  }

                      $conteggio = count($cart_array); //conta il numero di prodotti nel carrello 
                      // percent is 10%
                      $percent = -0.10;

                  if ($conteggio < 3 && $cart_array[0][0] == $cart_array[1][0])
                  { 
                      $discount = $percent *  $cart_array[1][3];
                      $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                      WC()->cart->add_fee( $discount_text, $discount, false );
                  }
                  if ($conteggio < 4 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0])
                  {
                      $discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]);
                      $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                      WC()->cart->add_fee( $discount_text, $discount, false );
                  }
                  if ($conteggio < 5 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0] && $cart_array[0][0] == $cart_array[3][0])
                  {
                      $discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]) + ($percent * $cart_array[3][3]);
                      $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                      WC()->cart->add_fee( $discount_text, $discount, false );
                  }
            } else return;


}
add_action( 'woocommerce_cart_calculate_fees','cart_discount' );

如您所见,我必须指定对这种特殊折扣感兴趣的产品的product_id,这对我来说很好,但是如果我们可以将其设置为类别(我没有发展这种状况的时间) 其次,我不喜欢的部分如下

As you see, I had to specify the product_id of the products that are interested in this particular kind of discount, which is fine for me, although it would be nicier if we could set it to categories (I didn't have the time to develop that condition) Secondly, the part I don't like is the following

if ($conteggio < 3 && $cart_array[0][0] == $cart_array[1][0])

以及以下所有条件:我正在寻找一个可能会通过$cart_array的函数,并找到在每个if()条件下手动设置的关系.

and all the following conditions: I was looking for a function that may go through the $cart_array and find the relations thta I set manually in each if() condition.

谢谢

这篇关于带有&gt;的同一商品的Woocommerce有条件条件部分购物车折扣1个变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆