2017-02-09 6 views
3

私はWoocommerceショップを設立し、12の倍数に基づいてすべての商品に特定の割引を設定したいと考えています。私は多くのディスカウントプラグインを試しましたが、私が探しているものは見つかりませんでした。商品数量に基づいて条件付きでカートアイテムによる割引を追加する

たとえば、商品Xの12個を注文すると、10%の割引が適用されます。商品Xの15個を注文すると、最初の12個は10%の割引が、最後の3個は満価です。私は24を注文した場合、その10%の割引が

私が見つけた最も近いがこれです製品Xのすべての24に適用されます。Discount for Certain Category Based on Total Number of Products

をしかし、これは、割引(実際には負の料金)として適用されます通常の割引のように、商品の横にあるカートに割引を表示したいと考えています。

また、製品がすでに販売されている場合は、この割引を無効にする必要があります。

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

答えて

3

はい、これは各カートのアイテムのカスタム計算を行うと、個別に(あなたの条件と計算に合致する)その価格を交換し、woocommerce_before_calculate_totalsアクションフックに引っかけカスタム関数を使用することも可能です。

add_action('woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 10, 1); 
function custom_discounted_cart_item_price($cart_object) { 

    $discount_applied = false; 

    // Set Here your targeted quantity discount 
    $t_qty = 12; 

    // Iterating through each item in cart 
    foreach ($cart_object->get_cart() as $item_values) { 

     ## Get cart item data 
     $item_id = $item_values['data']->id; // Product ID 
     $item_qty = $item_values['quantity']; // Item quantity 
     $original_price = $item_values['data']->price; // Product original price 

     // Getting the object 
     $product = new WC_Product($item_id); 


     // CALCULATION FOR EACH ITEM 
     // when quantity is up to the targetted quantity and product is not on sale 
     if($item_qty >= $t_qty && !$product->is_on_sale()){ 
      for($j = $t_qty, $loops = 0; $j <= $item_qty; $j += $t_qty, $loops++); 

      $modulo_qty = $item_qty % $t_qty; // The remaining non discounted items 

      $item_discounted_price = $original_price * 0.9; // Discount of 10 percent 

      $total_discounted_items_price = $loops * $t_qty * $item_discounted_price; 

      $total_normal_items_price = $modulo_qty * $original_price; 

      // Calculating the new item price 
      $new_item_price = ($total_discounted_items_price + $total_normal_items_price)/$item_qty; 


      // Setting the new price item 
      $item_values['data']->price = $new_item_price; 

      $discount_applied = true; 
     } 
    } 
    // Optionally display a message for that discount 
    if ($discount_applied) 
     wc_add_notice(__('A quantity discount has been applied on some cart items.', 'my_theme_slug'), 'success'); 
} 

これは(それが量だに基づいて)販売にある項目についてとされていないあなたは、カート内の各項目について個別に期待している正確に割引を行います

これはコードです。しかし、カートの広告申込情報に割引を示すラベル(テキスト)は表示されません。割引は、一部のカートのアイテムに適用されたときに

は、必要に応じて私は

コードは、任意のプラグインファイルでも、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行くか...通知を表示します。

このコードはテスト済みであり、動作します。

+1

ありがとう、これは完璧です。 – Phovos

関連する問題