2017-09-06 1 views
1

예술 총액에 특정 금액을 기준으로 수수료를 추가하려고합니다. 카트 총계가 총 "$$$"금액과 같거나 더 큰지를 보여주고 싶습니다. 수수료를 추가하십시오. 그렇지 않으면 추가하지 마십시오.WooCommerce의 특정 장바구니 합계를 기준으로 수수료를 추가하십시오.

나는 총액을 더하는 방법을 알고 있지만, 달러 금액 미만인지 확인하고 있다고 생각하지 않는다.

function woo_add_custom_fees(){ 

    $cart_total = 0; 

    // Set here your percentage 
    $percentage = 0.15; 

    foreach(WC()->cart->get_cart() as $item){ 
     $cart_total += $item["line_total"]; 
    } 
    $fee = $cart_total * $percentage; 

    if ( WC()->cart->total >= 25) { 

    WC()->cart->add_fee("Gratuity", $fee, false, ''); 

    } 

    else { 

     return WC()->cart->total; 
    } 
} 
add_action('woocommerce_cart_calculate_fees' , 'woo_add_custom_fees'); 
add_action('woocommerce_after_cart_item_quantity_update', 'woo_add_custom_fees'); 

내가 뭘 잘못하고 있니? 이 훅은 카트 총 계산하기 전에 해고로 WC()->cart->total 항상 0을 반환 woocommerce_cart_calculate_fees 액션 후크에서

+0

'else'부분이 '아래'입니다. – Reigel

답변

1

, ...

당신은 더 나은 WC()->cart->cart_contents_total 대신 사용해야합니다.

또한 카트 개체가 이미이 후크에 포함되어 있으므로이 개체를 후크 기능의 인수로 추가 할 수 있습니다.
또한이 고리를 사용할 필요가 없습니다. woocommerce_after_cart_item_quantity_update. 어떤 플러그인 파일도

add_action('woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1); 
function custom_fee_based_on_cart_total($cart_object) { 

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

    // The percetage 
    $percent = 15; // 15% 
    // The cart total 
    $cart_total = $cart_object->cart_contents_total; 

    // The conditional Calculation 
    $fee = $cart_total >= 25 ? $cart_total * $percent/100 : 0; 

    if ($fee != 0) 
     $cart_object->add_fee(__("Gratuity", "woocommerce"), $fee, false); 
} 

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

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

이 코드는 테스트되었으며 작동합니다.

+0

고마워요! 이것은 분명히 도움이된다. – nholloway4

관련 문제