Skip to content

Commerce Foundations: the B2B ecommerce platform for manufacturers and distributors. See what it includes

Craft CMS plugins

Advanced Discounts

A Craft CMS plugin that builds multi-tier Craft Commerce discounts from flexible cart rules.

Adding a discount type

Build a discount type of your own, alongside the Advanced and Buy X, Get Y types the plugin ships. A discount type determines which action rules a group offers and how those rules become order adjustments.

The contract#

Extend fostercommerce\advanceddiscounts\base\DiscountType, which implements DiscountTypeInterface and supplies the group rendering, adjustment building, and message resolution. Five static methods are yours to define:

Method Returns
handle() Stored on the discount row. Must be stable, because existing discounts reference it.
displayName() Label in the Type dropdown.
actionConditionClass() The ElementCondition subclass listing the action rules a group offers.
actionLabel() Field label above the action condition builder.
actionInstructions() Instructions under that label.

Override getAdjustments(Order $order, Discount $discount): array when your rules need adjustment logic the base class does not already cover. The base implementation skips disabled groups and tests each group’s cart condition. It then builds adjustments for the OrderCartActionRule, ShippingMethodCartActionRule, LineItemCartActionRule, and BogoCartActionRule rules it finds.

Minimal example#

A type offering only a shipping action:

<?php

declare(strict_types=1);

namespace mymodule\discounttypes;

use Craft;
use fostercommerce\advanceddiscounts\base\DiscountType;

class FreightDiscountType extends DiscountType
{
    public static function handle(): string
    {
        return 'freight';
    }

    public static function displayName(): string
    {
        return Craft::t('my-module', 'Freight');
    }

    public static function actionConditionClass(): string
    {
        return FreightActionCondition::class;
    }

    public static function actionLabel(): string
    {
        return Craft::t('my-module', 'Freight Actions');
    }

    public static function actionInstructions(): string
    {
        return Craft::t('my-module', 'Applied to shipping when the rules above match.');
    }
}

The condition class listing the rules the group offers:

<?php

declare(strict_types=1);

namespace mymodule\discounttypes;

use craft\elements\conditions\ElementCondition;
use fostercommerce\advanceddiscounts\elements\conditions\ShippingMethodCartActionRule;

class FreightActionCondition extends ElementCondition
{
    /**
     * @return array<int, class-string>
     */
    protected function selectableConditionRules(): array
    {
        return [
            ShippingMethodCartActionRule::class,
        ];
    }
}

Register it#

Listen for EVENT_REGISTER_DISCOUNT_TYPES in your module’s init():

use craft\events\RegisterComponentTypesEvent;
use fostercommerce\advanceddiscounts\services\DiscountTypes;
use yii\base\Event;

Event::on(
    DiscountTypes::class,
    DiscountTypes::EVENT_REGISTER_DISCOUNT_TYPES,
    static function (RegisterComponentTypesEvent $event): void {
        $event->types[] = FreightDiscountType::class;
    }
);

The type appears in the Type dropdown on the discount edit screen. See events for the event payload.

Message placeholders#

DiscountType::MESSAGE_PLACEHOLDERS lists every token a message can use. messagePlaceholders() sets the tokens listed in the Message placeholders pane. The token chips under each message, and save validation, offer the Buy X, Get Y tokens only when actionConditionClass() is BundleCondition, whatever messagePlaceholders() returns.

Removing a type#

The plugin skips a discount whose stored type handle no longer resolves. The discount is missing from the Discounts index, does not apply to orders, and logs a warning naming the handle. The row stays in the database, so registering the type again brings the discount back.

Before you stop registering a type, migrate existing rows to another handle. Otherwise the discount stops applying, and the log entry is the only record of why.

We can take it from here.

We take on the Craft and Craft Commerce sites you built, so you can get back to building. Introduce a client who signs a management contract and you get a $3K partnership fee.

How the Dev Partnership Program works
Book a call with Stephen