2017-03-06 16 views
1

私はこのコードを別の投稿から取りましたが、基本的に私の理解では、このコードはカート価格を40ドルの固定金額に変更して予約として請求しようとしています費用。預金総額の割合に基づいて預金

私がしたいことは、カート内のすべての商品を合計することに基づいて合計金額の20%を強制することです。私のサイトはの予約ですですので、は預金を請求し、予約時に支払うことができます。 Woocommerce cart deposit

add_action('woocommerce_cart_calculate_fees', 'booking_fee'); 
function booking_fee() { 
    global $woocommerce; 

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

    $bookingfee_array = array('2434'); 

    $fixed = 40.00; 

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 

     if(in_array($values['product_id'], $bookingfee_array)) { 
      $surcharge = $fixed; 
      $woocommerce->cart->add_fee('Broneeringutasu', $surcharge, true, ''); 
     } 

    } 

} 

そして、ここでは、私はそれを変更する方法を次のとおりです:ここで

は、この記事からのコードです

add_action('woocommerce_cart_calculate_fees', 'booking_fee'); 
function booking_fee() { 
    global $woocommerce; 

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

    $bookingfee_array = array('2434'); 

    $percent = .20; 

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 

     if(in_array($values['product_id'], $bookingfee_array)) { 
      $surcharge = $percent; 
      $woocommerce->cart->add_fee('Booking Fee', $surcharge, true, ''); 
     } 

    } 

} 

しかし、予想通り、これは動作しません。

これに関する助力?

おかげ

答えて

1

あなたのコードは本当にあなたが総カート量にo.2の料金を追加しているとして、あなたが期待する何をするつもりはありません。カートの量が100であれば、総合計は、100.2になるだろう...

あなたの代わりに何をしたいのか、20%のカートの量を取得するために、総カートの金額の80%を削除することです。これは、総カート量から80%のマイナス料金を使用して可能です。オリジナルのコードのようにターゲットとする商品IDを設定する必要がない場合は、以下の2番目の機能を使用します。ここで

は、カートのアイテムマッチ機能で設定した目標と製品IDと総カートの金額の80%を削除します最初の関数である:

add_action('woocommerce_cart_calculate_fees', 'booking_deposit_calculation'); 
function booking_deposit_calculation($cart_object) { 

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

    ## Set HERE your targeted products IDs 
    $target_product_ids = array(2434); 

    ## Set HERE your negative percentage (to remove an amount from cart total) 
    $percent = -.80; // 80% off (negative) 

    $matching = false; 

    // Iterating through each cart items 
    foreach ($cart_object->get_cart() as $item_values) 
    { 
     if(in_array($item_values['product_id'], $target_product_ids)) 
     { // If a cart item match with a targeted product ID 

      // Get cart subtotal excluding taxes 
      $cart_subtotal = $cart_object->subtotal_ex_tax; 
      // or for subtotal including taxes use instead: 
      // $cart_subtotal = $cart_object->subtotal; 

      ## ## CALCULATION ## ## 
      $calculated_amount = $cart_subtotal * $percent; 

      // Adding a negative fee to cart amount (Including taxes) 
      $cart_object->add_fee(__('Deposit calculation', 'woocommerce'), $calculated_amount, true); 

      break; // We stop the loop 
     } 
    } 
} 

コードは、あなたのアクティブなのfunction.phpファイルに行きます子供のテーマ(またはテーマ)、またはすべてのプラグインファイルに保存されます。


しかし、あなたは、元のコードのように、いくつかの対象製品を持っている必要はありませんかもしれません。

それは、このコードを簡素化するケースの場合:

add_action('woocommerce_cart_calculate_fees', 'booking_deposit_calculation'); 
function booking_deposit_calculation($cart_object) { 

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

    ## Set HERE your negative percentage (to remove an amount from cart total) 
    $percent = -.80; // 80% off (negative) 

    // Get cart subtotal excluding taxes 
    $cart_subtotal = $cart_object->subtotal_ex_tax; 
    // or for subtotal including taxes use instead: 
    // $cart_subtotal = $cart_object->subtotal; 

    ## ## CALCULATION ## ## 
    $calculated_amount = $cart_subtotal * $percent; 

    // Adding a negative fee to cart amount (Including taxes) 
    $cart_object->add_fee(__('Deposit calculation', 'woocommerce'), $calculated_amount, true); 

} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

両方の機能がテストされ、動作します。

+0

これは私が探していたものです。どうもありがとうございました。 –

+0

@ Stewsgarage.comようこそ、これは私にとってはとても簡単でした...無料のプラグインもいくつかあります:https://wordpress.org/plugins/search.php?q=woocommerce+deposit – LoicTheAztec

関連する問題