2017-08-18 7 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

それはwoocommerce_cart_calculate_feesフックで使われているとき、あなたが直面している無限ロードスピン問題がwc_print_notice()によるものです。それはバグのようだ。

代わりに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 +

関連する問題