2014-11-27 2 views
1

장바구니에 제품 수에 따라 할인을 추가해야하며이 할인은 장바구니 총에 적용됩니다. 쿠폰을 사용하지 않고 다른 옵션이 있습니까?카트 총계에 할인을 추가하는 방법?

+0

이 참조 생각하는 당신의 CSS에 다음을 추가 : 1 - https://wordpress.org/plugins/woocommerce -direct-bulk-category-discount/2 - https://wordpress.org/plugins/woocommerce-bulk-discount/screenshots/ –

+0

이 플러그인은 제품과 현명하게 작동합니다. 장바구니에있는 제품의 총 개수에 따라 discount를 추가해야합니다 . – user3714488

+0

그럼 내가 생각하는 코드를 사용자 정의해야 할 것입니다 ... –

답변

1

이 코드는 작동합니다 :

add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart'); 
function discount_when_produts_in_cart() { 
    global $woocommerce; 
    if($woocommerce->cart->cart_contents_count > 3) { 
     $coupon_code = 'maryscode'; 
     if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
     echo '<div class="woocommerce_message"><strong>You have more than 3 items in your cart, a 10% discount has been added.'; 
    } 
} 

위의 고객의 장바구니에 4 개 이상의 제품이있는 경우 장바구니에 쿠폰 "maryscode"을 적용합니다.

편집 : 나는이 방법을 선호

.coupon 
{ 
    display: none !important; 
} 
+0

예.이 작업을 시도했지만 쿠폰 필드를 표시하고 카트 페이지에 버튼과 메시지를 적용합니다. – user3714488

+0

@ user3714488 Woocommerce 설정 -> Checkout으로 이동하면 "쿠폰"의 체크 표시가 해제되어 카트 페이지에 쿠폰 필드/적용 버튼이 표시되지 않습니다. – Howli

+0

위의 코드가 작동하지 않는 옵션을 해제하면 – user3714488

15

이 더 청소기 내가

// Hook before calculate fees 
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees'); 

/** 
* Add custom fee if more than three article 
* @param WC_Cart $cart 
*/ 
function add_custom_fees(WC_Cart $cart){ 
    if($cart->cart_contents_count < 3){ 
     return; 
    } 

    // Calculate the amount to reduce 
    $discount = $cart->subtotal * 0.1; 
    $cart->add_fee('You have more than 3 items in your cart, a 10% discount has been added.', -$discount); 
} 
+0

카트에 적용된 동적 할인을 사용할 때 백엔드를 혼란스럽게하기 위해 쿠폰 코드를 사용하는 대신 이것은 훨씬 더 나은 접근법입니다. –

+0

각 제품에 대한 WooCommerce 할인 카트 정보이 https://www.cloudways.com/blog/woocommerce-discount-cart/를 확인하십시오. –

관련 문제