2017-08-18 31 views
2

我需要在購物車中設置最低訂單費用,因此如果購物車中的產品總價格不超過10英鎊,則需要支付額外費用才能將價格提高到10英鎊。WooCommerce基於動態最低訂單金額的費用

這裏是我目前在購物車階段運作良好的代碼,但是當您到達結賬處時,定價部分因某種原因不會停止加載,您無法結帳,任何人都可以幫忙嗎?從functions.php的

代碼:

你正面臨
function woocommerce_custom_surcharge() { 
    global $woocommerce; 
    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 
    $minimumprice = 10; 
    $currentprice = $woocommerce->cart->cart_contents_total; 
    $additionalfee = $minimumprice - $currentprice; 
    if ($additionalfee >= 0) { 
     wc_print_notice(
      sprintf('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied at checkout.' , 
       wc_price($minimumprice), 
       wc_price($currentprice) 
      ), 'error' 
     ); 
     $woocommerce->cart->add_fee('Minimum Order Adjustment', $additionalfee, true, ''); 
    } 
} 
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge'); 

回答

1

無限加載旋轉的問題是由於wc_print_notice(),當它在woocommerce_cart_calculate_fees胡克的使用。這看起來像一個錯誤。

如果使用wc_add_notice(),問題不存在但通知顯示2次。

此外,我已經重新審視你的code.The 唯一的解決辦法是它在2個獨立的功能劃分:

// NOTICE ONLY IN CART PAGE 
add_action('woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1); 
function add_custom_surcharge($cart_object) { 

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

    $minimumprice = 100; 
    $currentprice = $cart_object->cart_contents_total; 
    $additionalfee = $minimumprice - $currentprice; 

    if ($additionalfee >= 0) { 
     $cart_object->add_fee('Minimum Order Adjustment', $additionalfee, true); 

     if(! is_checkout()){ 
      $message = sprintf(__('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied.', 'woocommerce'), wc_price($minimumprice), wc_price($currentprice)); 
      wc_print_notice($message, 'error'); 
     } 
    } 
} 

// NOTICE ONLY IN CHECKOUT PAGE 
add_action('woocommerce_before_checkout_form', 'custom_surcharge_message', 10, 0); 
function custom_surcharge_message() { 
    $cart_object = WC()->cart; 
    $minimumprice = 100; 
    $currentprice = $cart_object->cart_contents_total; 
    $additionalfee = $minimumprice - $currentprice; 
    if ($additionalfee >= 0) { 
     $message = sprintf(
      __('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied.', 'woocommerce'), 
      wc_price($minimumprice), wc_price($currentprice) 
     ); 
     wc_print_notice($message, 'error'); 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或者也在任何插件文件中。

經過測試,完美適用於WooCommerce 3+

相關問題