2016-12-01 68 views
0

我在管理購物車和結賬頁面上的同時存在的優惠券和自定義附加字段方面存在問題。在購物車/結帳時管理有條件的優惠券和額外費用Woocommerce

我創建了一項功能,只有當購物車中存在多於一種變化的商品時,纔會添加折扣,而折扣只適用於第二種(或第三種)變體,而不是第一種。你可以在這裏找到我的最終代碼Woocommerce conditional part cart discount on the same product present with > 1 variation

但是我不得不繼續,因爲我有一個特別棘手的情況。通常,當購物車中有其他折扣(即標準折扣優惠券)時,上述條件不適用。因此,我在添加購物車後添加了這兩條線

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

    if(!$has_coupons) { 
    //here goes the function that I did 
    } else return; 

一切都很好,它的工作原理。然而,我有一個特殊的情況,我必須在購物車上使用一張優惠券(而不是%),以減少整個購物車的數量。這個特殊的優惠券可以與我寫的函數中創建的折扣一起使用!

/* get coupon code*/ 
function get_coupon_code() { 
    global $woocommerce; 
    if (!empty($woocommerce->cart->applied_coupons)) { 
     $my_coupon_code = $woocommerce->cart->get_coupons() ; 
     foreach($my_coupon_code as $coupon){ 

      if ($post = get_post($coupon->id)) { 
        $coupon_code= $coupon->code; 
      } 
     } 
    } 
    return $coupon_code; 
} 
    /* get coupon amount*/ 
function get_coupon_amount() { 
    global $woocommerce; 
    if (! empty($woocommerce->cart->applied_coupons)) { 
     $my_coupon_amount = $woocommerce->cart->get_coupons() ; 
     foreach($my_coupon_amount as $coupon){ 
         $coupon_amount =$coupon->coupon_amount; 
     } 
    } 
    return $coupon_amount; 
} 

function cart_discount() { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 
    global $woocommerce; 
    //get the code and amount of the coupon applied 
    $coupon_code = get_coupon_code(); 
    $coupon_amount = get_coupon_amount(); 
    //check if the code corresponds to the coupon exception I want to accept 
    if ($coupon_code == 'wa-1'){ 
     //if it is the code I need to apply, then I remove it from the cart 
     WC()->cart->remove_coupon($coupon_code); 
      //I use the amount to add it as additional fee at negative 
      $coupon_discount = -($coupon_amount); 
      //I add the ex-coupon amount to an additional field 
      $discount_agency_text = __('Detracted agency fee', 'woocommerce'); 
      WC()->cart->add_fee($discount_agency_text, $coupon_discount, false); 
     } 
    //and following the function previously mentioned 

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

     $has_coupons = count(WC()->cart->applied_coupons)>0?true:false; 
     //check if there are other coupons or if the coupon applied are the case I want to pass through my function 
     if(!$has_coupons || $coupon == 'wa-1')   { 

     //here follows the strings to apply a 10% discount on the second (third) variation of the product and not on the first one 

      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"]; 

        //product id to be considered for this discount 
        //$product = 1394 = Spedizioni CSR eng 
        //$product = 1389 = Spedizioni IDP eng 
        //$product = 13888 = Spedizioni CSR ita 
        //$product = 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); //counts the number of items in teh cart 
       // percent is 5% 
       $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'); 

我現在的問題是零件關注管理的折扣優惠券不起作用。它獲取$ coupon_code和$ coupon_amount,但不會將其添加到附加字段部分。

有人可以幫我嗎?

回答

0

我仍然停留在argyument,但我現在嘗試使它更簡單。

我實際上可以簡單地使用與我的功能一起使用的優惠券折扣(如果我的產品具有>然後是變體,則它僅添加10%的折扣而不是第一個)。

問題是,如果我在購物車上使用優惠券(= 80€),它無論如何都會蔓延到avrious物品上,並且10%的折扣是以較低的價格(而不是正常價格)計算的。

我的問題將被解決,如果我可以將優惠券應用於最終購物車總額,而不是分散在單個項目上。那可能嗎?

只給你一個例子,在使用優惠券之前,我的$ cart_array是

Array 
(
    [0] => Array 
     (
      [0] => 1394 //product_id 
      [1] => 1851 //variation_id 
      [2] => 1 //quantity 
      [3] => 906 //regular price 
     ) 

    [1] => Array 
     (
      [0] => 1394 //product_id 
      [1] => 1852 //variation_id 
      [2] => 1 //quantity 
      [3] => 906 //regular price 
     ) 

) 

如果在這一點上我申請優惠券時,陣列中的變更:

Array 
(
    [0] => Array 
     (
      [0] => 1394 
      [1] => 1851 
      [2] => 1 
      [3] => 866 //price after coupon 
     ) 

    [1] => Array 
     (
      [0] => 1394 
      [1] => 1852 
      [2] => 1 
      [3] => 866 //price after coupon 
     ) 

) 

正如你看到的第二項不再是906而是866,因此10%的折扣更少。 有沒有辦法將優惠券只應用到數組的第一項?或者在最終總額(在計算額外費用後)應用它?

0

很好的呈現!

這個插件可以很容易地解決這個問題,它允許您在購物車中根據您在woocommerce商店中配置的多個條件規則的組合收取額外費用。

我希望它可以幫助!

WooCommerce Conditional Product Fees For Checkout

+0

鏈接在回答非常歡迎,但答案應該始終從鏈接呈現的基本信息,因爲鏈接可以改變。謝謝! – Alex

相關問題