2017-01-24 5 views
3

제품 ID 및 수량 조건에 따라 WooCommerce 저장소에 쿠폰을 자동으로 적용하려고합니다. 최종 목표는 원하는 제품이 카트에 2 개 추가되면 특정 쿠폰이 자동으로 적용되고, 원하는 제품이 3 개 추가 될 때마다 자동으로 다른 쿠폰이 적용됩니다. 제품의 단일 수량에는 할인이 없어야합니다. 지금 작동 코드의 수정 된 버전입니다 :특정 제품 ID 및 수량에 대해 조건부로 쿠폰을 자동 적용합니다.

add_action('woocommerce_before_cart', 'conditional_auto_add_coupons'); 
function conditional_auto_add_coupons() { 

if (!WC()->cart->is_empty()){ 

    // Define HERE your Targeted Product ID and coupons codes 
    $target_pid = 103; 
    $coupon1 = 'soccer-sibling-2'; 
    $coupon2 = 'soccer-sibling-3'; 

    // First cart loop: Counting number of subactegory items in cart 
    foreach (WC()->cart->get_cart() as $cart_item){ 
     if($target_pid == $cart_item['data']->id){ 
      // Removes any coupons in the cart already 
      WC()->cart->remove_coupons(); 
      if(2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon1)){ 
       WC()->cart->remove_coupons(); 
       WC()->cart->add_discount($coupon1); 
       wc_add_notice(__('The multiple sibling discount has been applied.', 'theme_domain'), 'success'); 
      } elseif(3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon2)){ 
       WC()->cart->remove_coupons(); 
       WC()->cart->add_discount($coupon2); 
       wc_add_notice(__('The multiple sibling discount has been applied.', 'theme_domain'), 'success'); 
      } 
      // Recalculates Cart Totals to show correct price 
      WC()->cart->calculate_totals(); 
     } 
    } 
    } 
} 
+0

어떤 오류 메시지가 표시됩니까? –

+0

글쎄, 내 붙여 넣은 코드에 구문 오류가 있음을 발견했습니다. 그것을 바로 잡았습니다. 내가 디버깅 할 때 얻는 에러는 "Notice : 333 행의 /home/...functions.php에있는 non-object의 속성을 얻으려고하는 것"입니다. 해당 라인은 조건부 'if ($ _product-> id == $ product_id)'입니다. – GDailey

답변

2

Theres는 코드에서 오류가 많이있다가 그것이 내가 당신의 기능에 everithing 다시 작성해야 ... 너무 조금 오래된의 및 그것을 다른 갈고리에 걸어 놓았다. 어떤 플러그인 파일도

add_action('woocommerce_before_cart', 'conditional_auto_add_coupons'); 
function conditional_auto_add_coupons() { 

    if (!WC()->cart->is_empty()){ 

     // Define HERE your Targeted Product ID and coupons codes 
     $target_pid = 103; 
     $coupon1 = 'soccer-sibling-2'; 
     $coupon2 = 'soccer-sibling-3'; 

     // First cart loop: Counting number of subactegory items in cart 
     foreach (WC()->cart->get_cart() as $cart_item){ 
      if($target_pid == $cart_item['data']->id){ 
       if(2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon1)){ 
        WC()->cart->add_discount($coupon1); 
        wc_add_notice(__('A quantity discount of <strong>5%</strong> has been added.', 'theme_domain'), 'success'); 
       } elseif(3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon2)){ 
        WC()->cart->add_discount($coupon2); 
        wc_add_notice(__('A quantity discount of <strong>10%</strong> has been added.', 'theme_domain'), 'success'); 
       } 
      } 
     } 
    } 
} 

이 코드는 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 :

는 여기에 귀하의 재검토 코드입니다.

웹 사이트가 작동하지 않아 웹 사이트가 중단되지는 않지만 실제로 테스트하지는 않았습니다.

+0

코드가 잘 작동합니다. 한 가지 문제를 해결하기 위해 두 개의 작은 줄을 추가했습니다. 카트에서 수량을 변경할 때마다 두 번째 할인이 적용되지만 첫 번째는 제거되지 않습니다. 수량을 1로 줄이면 적용된 할인을 그대로 유지합니다. 코드에 줄을 추가하여 원하는 자동 쿠폰을 적용하기 전에 추가 된 할인을 제거한 다음 다른 하나가 합계를 다시 계산하여 카트 페이지에 표시된 정확한 금액을 표시합니다. 원래 게시물에 추가 한 코드를 추가했습니다. 감사! – GDailey