2016-12-02 15 views
2

woocommerceに1つのカテゴリの12-23個の商品がカートに追加されたときに10%の割引を計算する機能を追加できます。数量計算に基づく商品カテゴリのカート割引

カテゴリの24〜47項目が追加された場合、15%の割引となります。

最後に、このカテゴリの48以上のアイテムが追加された場合、20%の割引となります。私は

+1

は、あなたが割引のこれらの種類を行うことができますプラグインを試してみましたか?たぶんhttps://wordpress.org/plugins-wp/pricing-deals-for-woocommerce/ –

+0

私は成功していないプラグインの数を試してみました。私は価格取引のためのwoocommerceを試した –

+0

@DustySatterleeわずかなエラーがあった私の答えを再更新しました...コードの2つの間違い...今は完璧に働いています。 – LoicTheAztec

答えて

0

更新 woocommerceに新しいですと

実際のコード例では、素晴らしいだろう - ここで出力された割引テキストで修正したコードの間違いや追加機能強化

を夢中機能ですwoocommerce_cart_calculate_feesフックでは、カートアイテムの数量計算に基づいてその特定のカテゴリ(またはサブカテゴリ)の割引を行う予定です。

これはコードです:

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

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

    // Set HERE your category (can be an ID, a slug or the name) 
    $category = 34; // or a slug: $category = 'wine'; 

    $category_count = 0; 
    $category_total = 0; 
    $discount = 0; 

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

     if(has_term($category, 'product_cat', $cart_item['product_id'])): 
      $category_count += $cart_item['quantity']; 
      $category_total += $cart_item["line_total"]; // calculated total items amount (quantity x price) 
     endif; 

    endforeach; 

    $discount_text = __('Quantity discount of ', 'woocommerce'); 

    // ## CALCULATIONS ## 
    if ($category_count >= 12 && $category_count < 24) { 
     $discount -= $category_total * 0.1; // Discount of 10% 
     $discount_text_output = $discount_text . '10%'; 
    } elseif ($category_count >= 24 && $category_count < 48) { 
     $discount -= $category_total * 0.15; // Discount of 15% 
     $discount_text_output = $discount_text . '15%'; 
    } elseif ($category_count >= 48) { 
     $discount -= $category_total * 0.2; // Discount of 20% 
     $discount_text_output = $discount_text . '20%'; 
    } 

    // Adding the discount 
    if ($discount != 0 && $category_count >= 12) 
     $cart_object->add_fee($discount_text_output, $discount, false); 

    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
} 

注:add_fee()方法で最後の引数が割引に税を適用するかどうかに関係している...

コードがテストされ、完全にされます機能的。

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


Discount for Certain Category Based on Total Number of Products

関連する問題