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.