<p>Found the answer:
in both discounts &quot;Don’t apply any subsequent discounts to an order if this discount is applied&quot; has to be active.</p>
<p>and the discount with the higher value (in my case &gt; 200$ -&gt; 10%) has to be above the other discount in the control panel.</p>
<p>and to check on the page itself, use the &quot;update cart&quot; button and not the browser refresh.</p>

CC BY-SA 4.0
2021-07-27T15:48:40.453Z
But still the question on how to apply discounts for the whole cart, rather than single item?
Henrik
CC BY-SA 4.0
2021-07-28T19:01:03.120Z
answered above.
RitterKnight
CC BY-SA 4.0
2021-07-27T15:42:01.240Z
2021-07-27T15:42:01.240Z
Henrik
<p>The Commerce GUI doesn't let you set a whole cart discount but you can do it as an adjuster in a module/plugin.</p>
<p>Setup the scaffolding for a module at <a href="https://pluginfactory.io" rel="nofollow noreferrer">pluginfactory</a> then add a folder inside called the <code>src</code> folder <code>adjusters</code> and name it <code>OrderAdjuster.php</code></p>
<p>Throw this code in that file (modified from a another project):</p>
<pre><code>&lt;?php

namespace modules\yourmodule\adjusters;


use Craft;
use craft\base\Component;
use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;
use craft\commerce\Plugin;
// use craft\commerce\elements\Product;
// use craft\commerce\elements\Variant;
// use craft\commerce\services\LineItems;


//if the amount in the cart is greater than 100$ a discount of 5% is applied.
//if the amount in the cart is greater than 200$ a discount of 10% is applied.

class OrderAdjuster extends Component implements AdjusterInterface 
{ 
        public function adjust(Order $order): array
    {
        
        $adjustments = [];
        
        if ($order-&gt;itemSubtotal &gt; 100) {
        
                $adjustment = new OrderAdjustment;
                $adjustment-&gt;type = 'discount';
                $adjustment-&gt;sourceSnapshot = [ 'data' =&gt; 'value' ];
    
           
            // if the amount in the cart is greater than 100$ a discount of 5% is applied.
            if ($order-&gt;itemSubtotal &gt;= 100 &amp;&amp; $order-&gt;itemSubtotal &lt; 200) {
                
                 $adjustment-&gt;amount = ($order-&gt;itemSubtotal * .05 * -1);
                 $adjustment-&gt;name = 'Order $100 or more';
                 $adjustment-&gt;description = '5% off';
                
            }
            
            // if the amount in the cart is greater than 200$ a discount of 10% is applied.
    
             if ($order-&gt;itemSubtotal &gt;= 200){
                   $adjustment-&gt;amount = ($order-&gt;itemSubtotal * .10 * -1);
                   $adjustment-&gt;name = 'Order $200 or more';
                   $adjustment-&gt;description = '10% off';
    
                
            }
                    $adjustment-&gt;setOrder($order);

            
              $adjustments[] = $adjustment;
          }
        return $adjustments;

        
    }
    
}
</code></pre>
<p>The <code>\Product</code>, <code>\Varaint</code> and <code>\LineItems</code> classes aren't needed in this particular example but it's handy to keep them around in case you need them.</p>
<p>You're also going to need to <a href="https://craftcms.com/docs/commerce/3.x/extend/adjusters.html#registering-a-new-adjuster" rel="nofollow noreferrer">register</a> the adjuster in your module before you can use it.</p>
<pre><code>use craft\commerce\services\OrderAdjustments;
use yii\base\Event;
use MyAdjuster;

Event::on(OrderAdjustments::class, OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, function(RegisterComponentTypesEvent $event) {

       $event-&gt;types[] = OrderAdjuster::class;

});
</code></pre>
<p><a href="https://craftcms.com/docs/commerce/3.x/extend/adjusters.html" rel="nofollow noreferrer">Adjusters</a> are useful for all sorts of things. You probably need to modify if you use coupon codes and don't want the discount to stack but that's the beauty of the adjuster, you can create complex conditions pretty quickly once you get the boilerplate setup.</p>

CC BY-SA 4.0
2021-07-29T20:43:59.257Z
Thanks a lot! Own plugins and modules are still quite new to me. So this is a module, or adjusters are a kind of module? i made a setup via pluginfactory, with the new file, but dont know where to put it. maybe i just have to learn more about modules first...
Henrik
CC BY-SA 4.0
2021-07-30T15:50:06.293Z
Yeh you probably want to learn a bit about modules first but once you get the "scaffolding" down, it's just adding in your own code.  In Craft you [add on/extend](https://craftcms.com/docs/3.x/extend/) via Modules or Plugins.  Adjusters are just code (class) that extend Commerce's AdjusterInterface.
RitterKnight
CC BY-SA 4.0
2021-08-02T12:05:46.753Z
I made a plugin (working) and a module (also working) via craftquest.io. now i added your code into the module, but i get the following error: Failed to instantiate component or class "OrderAdjuster".↵ Caused by: ReflectionException, Class OrderAdjuster does not exist". i am aware of the correct names and spelling, i tried different options, but no luck. I changed your example code: "use MyAdjuster" to "use OrderAdjuster" and added "use craft\events\RegisterComponentTypesEvent;" in the main Module File (it was missing, was'nt it?). what could be the problem?
Henrik
CC BY-SA 4.0
2021-08-02T12:06:06.463Z
adjusters folder is not inside the src folder, right? for the module template via pluginfactory.io i did not added controller, services or models.
Henrik
CC BY-SA 4.0
2021-08-02T14:07:57.310Z
@Henrik: ``adjusters`` folder goes inside the ``src`` folder.
RitterKnight
CC BY-SA 4.0
2021-08-03T15:39:45.623Z
now it worked! thanks a lot. i tried the adjuster in the src folder, but did'nt work, apparently because of cookies, but this did'nt came to my mind. Would you edit your answer (see my answer above, the missing piece) for further reference? and one more question: i can have several adjusters in one module, right?
Henrik
CC BY-SA 4.0
2021-08-03T20:11:03.310Z
@Henrik - cookies shouldn't have any affect. And yeh you can should be able to have more one adjuster.
RitterKnight
CC BY-SA 4.0
2021-07-28T18:59:54.907Z
2021-08-02T14:09:16.117Z
2021-08-02T14:09:16.117Z
RitterKnight
<p>i added two discounts:</p>
<ul>
<li>if the amount in the cart is greater than 100$ a discount of 5% is applied.</li>
<li>if the amount in the cart is greater than 200$ a discount of 10% is applied.</li>
</ul>
<p>if i have a cart of more than 200$ only the first discount (5%) is applied, and not the second one.
Is there a way of having a amount range for discounts (100-200$ -&gt; 5%, ...) or how can i get the second discount to be active?</p>
<p>i tried 'Don’t apply any subsequent discounts to an order if this discount is applied'
but no luck.</p>
<p>i also noticed, that this discounts are applied to the single items. Can i also set it to the whole cart?</p>

<plugin-craftcommerce><discounts>
Applying several discounts for amount range (> 100$ -> 5%, > 200$ -> 10%)
# Applying several discounts for amount range (> 100$ -> 5%, > 200$ -> 10%)

(Asked by: username_0 on 2021-07-27)

i added two discounts:

* if the amount in the cart is greater than 100$ a discount of 5% is applied.
* if the amount in the cart is greater than 200$ a discount of 10% is applied.

if i have a cart of more than 200$ only the first discount (5%) is applied, and not the second one.
Is there a way of having a amount range for discounts (100-200$ -> 5%, ...) or how can i get the second discount to be active?

i tried 'Don’t apply any subsequent discounts to an order if this discount is applied'
but no luck.

i also noticed, that this discounts are applied to the single items. Can i also set it to the whole cart?

**Tags:** plugin-craftcommerce, discounts


---

## 2 answers

### Answer by username_0 on 2021-07-27. Score: 1

Found the answer:
in both discounts "Don’t apply any subsequent discounts to an order if this discount is applied" has to be active.

and the discount with the higher value (in my case > 200$ -> 10%) has to be above the other discount in the control panel.

and to check on the page itself, use the "update cart" button and not the browser refresh.

> **Comments:**
> **username_0**: But still the question on how to apply discounts for the whole cart, rather than single item?  
> **username_1**: answered above.  

---

### Answer by username_1 on 2021-07-28. Score: 1

The Commerce GUI doesn't let you set a whole cart discount but you can do it as an adjuster in a module/plugin.
Setup the scaffolding for a module at pluginfactory then add a folder inside called the src folder adjusters and name it OrderAdjuster.php
Throw this code in that file (modified from a another project):
itemSubtotal > 100) {
        
                $adjustment = new OrderAdjustment;
                $adjustment->type = 'discount';
                $adjustment->sourceSnapshot = [ 'data' => 'value' ];
    
           
            // if the amount in the cart is greater than 100$ a discount of 5% is applied.
            if ($order->itemSubtotal >= 100 && $order->itemSubtotal < 200) {
                
                 $adjustment->amount = ($order->itemSubtotal * .05 * -1);
                 $adjustment->name = 'Order $100 or more';
                 $adjustment->description = '5% off';
                
            }
            
            // if the amount in the cart is greater than 200$ a discount of 10% is applied.
    
             if ($order->itemSubtotal >= 200){
                   $adjustment->amount = ($order->itemSubtotal * .10 * -1);
                   $adjustment->name = 'Order $200 or more';
                   $adjustment->description = '10% off';
    
                
            }
                    $adjustment->setOrder($order);

            
              $adjustments[] = $adjustment;
          }
        return $adjustments;

        
    }
    
}

The \Product, \Varaint and \LineItems classes aren't needed in this particular example but it's handy to keep them around in case you need them.
You're also going to need to register the adjuster in your module before you can use it.
use craft\commerce\services\OrderAdjustments;
use yii\base\Event;
use MyAdjuster;

Event::on(OrderAdjustments::class, OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, function(RegisterComponentTypesEvent $event) {

       $event->types[] = OrderAdjuster::class;

});

Adjusters are useful for all sorts of things. You probably need to modify if you use coupon codes and don't want the discount to stack but that's the beauty of the adjuster, you can create complex conditions pretty quickly once you get the boilerplate setup.

> **Comments:**
> **username_0**: Thanks a lot! Own plugins and modules are still quite new to me. So this is a module, or adjusters are a kind of module? i made a setup via pluginfactory, with the new file, but dont know where to put it. maybe i just have to learn more about modules first...  
> **username_1**: Yeh you probably want to learn a bit about modules first but once you get the "scaffolding" down, it's just adding in your own code.  In Craft you [add on/extend](https://craftcms.com/docs/3.x/extend/) via Modules or Plugins.  Adjusters are just code (class) that extend Commerce's AdjusterInterface.  
> **username_0**: I made a plugin (working) and a module (also working) via craftquest.io. now i added your code into the module, but i get the following error: Failed to instantiate component or class "OrderAdjuster".↵ Caused by: ReflectionException, Class OrderAdjuster does not exist". i am aware of the correct names and spelling, i tried different options, but no luck. I changed your example code: "use MyAdjuster" to "use OrderAdjuster" and added "use craft\events\RegisterComponentTypesEvent;" in the main Module File (it was missing, was'nt it?). what could be the problem?  
> **username_0**: adjusters folder is not inside the src folder, right? for the module template via pluginfactory.io i did not added controller, services or models.  
> **username_1**: @username_0: ``adjusters`` folder goes inside the ``src`` folder.  
> **username_0**: now it worked! thanks a lot. i tried the adjuster in the src folder, but did'nt work, apparently because of cookies, but this did'nt came to my mind. Would you edit your answer (see my answer above, the missing piece) for further reference? and one more question: i can have several adjusters in one module, right?  
> **username_1**: @username_0 - cookies shouldn't have any affect. And yeh you can should be able to have more one adjuster.  

---

