2016-12-01 4 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

아직 나는 수수께끼에 붙어 있지만 지금은 더 간단하게하려고합니다.

사실 내 함수와 함께 작동하는 쿠폰 할인을 사용할 수 있습니다 (제품에> 변형이있는 경우 다음 변형 만 10 % 할인되며 첫 번째 변형은 추가하지 않음).

카트에 적용된 쿠폰 (= 80 €)을 사용하면 문제가되는 것이므로 어쨌든 사용 가능한 항목에 적용되며 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