2016-11-29 5 views
1

WooCommerceでは、サンプルと呼ばれるカテゴリの商品があります。各サンプルの費用は2.99ドルです。 しかし、サンプルを5カートに追加すると、サンプルのコストを$ 2.99から$ 1に自動的に変更する方法が必要です。商品の総数に基づく特定のカテゴリの割引

したがって、4つのサンプルがカートに追加された場合、合計は11.96ドルになりますが、5が追加された場合、合計は5ドルになります。

だから、すべての5つの製品について、製品の価格は$ 1 $ 2.99から変更するだろうが、6個のサンプルがカートに追加された場合、合計$ 7.99になると10を添加した場合の合計が$ 10などだろう...

どうすればこれを達成できますか?

ありがとうございました。

+1

をあなたが必要なものを私たちに語ってきましたが、あなたは、あなたが試したし、何が機能していないものを私たちに語っていません。お手伝いしましょうか? – byxor

答えて

3

- 更新 -

ここには、あなたの要件に便利なものがあります。
この機能は、カートに割引を追加します。

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

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

    // Define HERE your targeted product category (id, slug or name are accepted) 
    $category = 'posters'; 
    // Set the price for Five HERE 
    $price_x5 = 5; 

    // initializing variables 
    $calculated_qty = 0; 
    $calculated_total = 0; 
    $discount = 0; 

    // Iterating through each cart item 
    foreach($cart_object->get_cart() as $item_key => $item): 

     // Make this discount calculations only for products of your targeted category 
     if(has_term($category, 'product_cat', $item['product_id'])): 

      $item_price = $item["data"]->price; // The price for one (assuming that there is always 2.99) 
      $item_qty = $item["quantity"];// Quantity 
      $item_line_total = $item["line_total"]; // Item total price (price x quantity) 
      $calculated_qty += $item_qty; // ctotal number of items in cart 
      $calculated_total += $item_line_total; // calculated total items amount 
     endif; 
    endforeach; 

    // ## CALCULATIONS (updated) ## 
    if($calculated_qty >= 5):  
     for($j = 5, $k=0; $j <= $calculated_qty; $j+=5,$k++); // Update $k=0 (instead of $k=1) 
     $qty_modulo = $calculated_qty % 5; 
     $calculation = ($k * $price_x5) + ($qty_modulo * $item_price); 
     $discount -= $calculated_total - $calculation; 
    endif; 

    // Adding the discount 
    if ($discount != 0) 
     $cart_object->add_fee(__('Quantity discount', 'woocommerce'), $discount, false); 
     // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
} 
+0

上記のコードを使用すると、カート内に5つのサンプルがあり、各商品は$ 2.99で小計では14.95ドルになり、「 - $ 4.95」の「数量割引」が適用され、合計で$ 10になります。最終的な合計は$ 10ではなく$ 5になります。 –

+1

パーフェクトありがとう! –

関連する問題