2017-10-18 2 views
2

私は、(各商品ラインアイテムにデポジットを追加するのではなく)ウーコムスサイトのTOTALカート額にデポジットを追加する方法を探していました。例外付きの総カート数のパーセントに対するデフォルトデポジット

私はここで、この独創的なスレッドへの答えを見つけた:Deposit based on a percentage of total cart amount

は、ここに私が使用して終了コードです:

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 = -.50; // 50% 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 (excluding taxes) 
    $cart_object->add_fee(__('Deposit calculation', 'woocommerce'), $calculated_amount, false); 

} 

これは、カートとチェックアウトのすべての製品の50%の保証金のデフォルトを作成しますページ。ブリリアント! (CSSを使用して、私はフロントエンドの説明をスタイルすることができました)

しかし、私はこのデポジットを強制したくないいくつかの製品(1つの製品カテゴリー)を持っています。

だから、ここに私の質問です:

私は全体のカテゴリを除外することができない場合、デフォルトの預金を強制し続けるが、1つの製品カテゴリ(またはこのカテゴリーの製品から預金を除外するために、コードを微調整することができますどのように)?

答えて

0

以下のフック機能では、商品カテゴリID番号または(および)の商品カテゴリを除外する必要があります。あなたがそれらのいずれかを使用しない場合には、例えば、$product_categories = array();のためのような空白の配列を設定することができます...

ここではコードです:

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

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

    // Define the product IDs to exclude 
    $product_ids = array(37, 25, 50); 
    // Define the product categories to exclude (can be IDs, slugs or names) 
    $product_categories = array('clothing'); 
    $amount_to_exclude_with_tax = 0; 

    // Iterating through cart items 
    foreach ($cart_object->get_cart() as $cart_item){ 
     // If condition match we get the sum of the line item total (excl. tax) 
     if(in_array($cart_item['product_id'], $product_ids) || has_term($product_categories, 'product_cat', $cart_item['product_id'])) 
      $amount_to_exclude_with_tax += $cart_item['line_total']; 
      // OR replace by (for tax inclusion) 
      // $amount_to_exclude_with_tax += $cart_item['line_tax'] + $cart_item['line_total']; 
    } 

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

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

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

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

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

WooCommerce 3で動作確認済みです。

関連する問題