<p>Based on some research, I believe a custom <a href="https://craftcommerce.com/docs/adjusters" rel="nofollow noreferrer">Adjuster</a> is the way to solve this problem.</p>

<p>I created an adjuster with the following:</p>

<p><code>craft/plugins/myplugin/MypluginPlugin.php</code></p>

<pre class="lang-php prettyprint-override"><code>public function commerce_registerOrderAdjusters()
{
  return [
    401 =&gt; new \Commerce\Adjusters\Myplugin_TenpercentDiscount
  ];
}
</code></pre>

<p><code>craft/plugins/myplugin/Adjusters/Myplugin_TenPercentDiscount.php</code></p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace Commerce\Adjusters;
use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class Myplugin_TenPercentDiscount implements Commerce_AdjusterInterface {

public function adjust(Commerce_OrderModel &amp;$order, array $lineItems = []){

    if (empty($lineItems))
    {
        return [];
    }

    if ($order-&gt;totalQty &gt;= 10)
    {
        $adjuster = new Commerce_OrderAdjustmentModel();

        $adjuster-&gt;type        = 'Item Discount';
        $adjuster-&gt;name        = '10% Off Items';
        $adjuster-&gt;description = 'Your order qualifies for a 10% discount on all non sale items because you are purchasing at least 10 products.';
        $adjuster-&gt;orderId     = $order-&gt;id;

        $amountOff = 0;
        foreach ($lineItems as $item)
        {
            if (empty($item-&gt;onSale))
            {
                $amountOff += ($item-&gt;subtotal + $item-&gt;discount) * .1;
            }
        }
        $amountOff = $amountOff * -1;

        $adjuster-&gt;amount      = $amountOff;
        $adjuster-&gt;optionsJson = ['lineItemsAffected' =&gt; null];
        $order-&gt;baseDiscount  += $amountOff;

        $adjuster-&gt;included = false;

        return [$adjuster];
    }
    return[];
}

}
</code></pre>

<p>Does anyone see any flaws with this approach?</p>

CC BY-SA 3.0
2018-03-03T15:42:01.907Z
Hi peter, I am struggling onto where I'd have to put this code. I've tried to put it under craft->plugins->Commerce->Adjusters and named it Myplugin_TenPercentDiscount.php. But that does not seem to work. Sorry, I am a bit lost in all of this.
outline4
CC BY-SA 3.0
2018-03-23T21:09:58.033Z
Hi @outline4 -- You're close. You'll need to create your own plugin. I suggest using Plugin Factory for the scaffolding: https://pluginfactory.io/ and then you'll want the adjuster in the path: `craft/plugins/yourplugin/Adjusters/Yourplugin_Adjustername.php`
Peter Tell
CC BY-SA 3.0
2018-03-23T21:10:38.963Z
Forgot to add, you also need to register the adjuster, I updated my code above to show that
Peter Tell
CC BY-SA 3.0
2017-05-09T19:02:03.390Z
2018-03-23T21:13:15.190Z
2018-03-23T21:13:15.190Z
Peter Tell
<p>What I would like is for a 10% discount to be applied to non sale items after the total cart quantity reaches 10 total items. I would ONLY like this discount on non sale items. Is that possible?</p>

<p>So, say I have 2 Products in my cart:</p>

<ul>
<li>Item A - $20 on sale for $10, qty 9</li>
<li>Item B - $20, qty 1</li>
</ul>

<p>This makes my total in cart quantity 10 line items even though 9 of them are sale items.</p>

<p>What I would like is for <code>Item B</code> to be discounted 10% making my cart total <code>$108</code></p>

<pre><code>( (10*9) + (20 * .9) ) = 108
</code></pre>

<p>As it currently stands, if I toggle <code>Exclude this discount for products that are already on sale</code> it doesn't count the sales items towards the total.</p>

CC BY-SA 3.0
2017-05-08T16:37:33.120Z
So 1) cart needs to reach 10 items. 2) Then 10% off on any item not already on sale.
RitterKnight
CC BY-SA 3.0
2017-05-08T17:28:57.877Z
Correct. You're much more succinct than I ;-)
Peter Tell
<plugin-craftcommerce>
Craft Commerce - For a discount, can we include all products in the product count but not include the sale items that receive the discount?
# Craft Commerce - For a discount, can we include all products in the product count but not include the sale items that receive the discount?

(Asked by: username_0 on 2017-05-03)

What I would like is for a 10% discount to be applied to non sale items after the total cart quantity reaches 10 total items. I would ONLY like this discount on non sale items. Is that possible?
So, say I have 2 Products in my cart:

Item A - $20 on sale for $10, qty 9
Item B - $20, qty 1

This makes my total in cart quantity 10 line items even though 9 of them are sale items.
What I would like is for Item B to be discounted 10% making my cart total $108
( (10*9) + (20 * .9) ) = 108

As it currently stands, if I toggle Exclude this discount for products that are already on sale it doesn't count the sales items towards the total.

**Tags:** plugin-craftcommerce
> **Comments:**
> **username_1**: So 1) cart needs to reach 10 items. 2) Then 10% off on any item not already on sale.  
> **username_0**: Correct. You're much more succinct than I ;-)  

---

## 1 answers

### Answer by username_0 on 2017-05-09. Score: 3 | Accepted answer

Based on some research, I believe a custom Adjuster is the way to solve this problem.
I created an adjuster with the following:
craft/plugins/myplugin/MypluginPlugin.php
public function commerce_registerOrderAdjusters()
{
  return [
    401 => new \Commerce\Adjusters\Myplugin_TenpercentDiscount
  ];
}

craft/plugins/myplugin/Adjusters/Myplugin_TenPercentDiscount.php
totalQty >= 10)
    {
        $adjuster = new Commerce_OrderAdjustmentModel();

        $adjuster->type        = 'Item Discount';
        $adjuster->name        = '10% Off Items';
        $adjuster->description = 'Your order qualifies for a 10% discount on all non sale items because you are purchasing at least 10 products.';
        $adjuster->orderId     = $order->id;

        $amountOff = 0;
        foreach ($lineItems as $item)
        {
            if (empty($item->onSale))
            {
                $amountOff += ($item->subtotal + $item->discount) * .1;
            }
        }
        $amountOff = $amountOff * -1;

        $adjuster->amount      = $amountOff;
        $adjuster->optionsJson = ['lineItemsAffected' => null];
        $order->baseDiscount  += $amountOff;

        $adjuster->included = false;

        return [$adjuster];
    }
    return[];
}

}

Does anyone see any flaws with this approach?

> **Comments:**
> **username_2**: Hi peter, I am struggling onto where I'd have to put this code. I've tried to put it under craft->plugins->Commerce->Adjusters and named it Myplugin_TenPercentDiscount.php. But that does not seem to work. Sorry, I am a bit lost in all of this.  
> **username_0**: Hi @outline4 -- You're close. You'll need to create your own plugin. I suggest using Plugin Factory for the scaffolding: https://pluginfactory.io/ and then you'll want the adjuster in the path: `craft/plugins/yourplugin/Adjusters/Yourplugin_Adjustername.php`  
> **username_0**: Forgot to add, you also need to register the adjuster, I updated my code above to show that  

---

