1

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?

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.

2
  • But still the question on how to apply discounts for the whole cart, rather than single item?
    – Henrik
    Jul 27 at 15:48
  • answered above. Jul 28 at 19:01
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):

<?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->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.

7
  • 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
    Jul 29 at 20:43
  • 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 via Modules or Plugins. Adjusters are just code (class) that extend Commerce's AdjusterInterface. Jul 30 at 15:50
  • 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
    Aug 2 at 12:05
  • 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
    Aug 2 at 12:06
  • @Henrik: adjusters folder goes inside the src folder. Aug 2 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.