Skip to main content
Custom discount types can be registered to add additional discount logic beyond the built-in types.

Overview

If you want to add additional functionality to Discounts, you can register your own custom discount types.

Registering a discount type.

use Lunar\Facades\Discounts;

Discounts::addType(MyCustomDiscountType::class);
<?php

namespace App\DiscountTypes;

use Lunar\DiscountTypes\AbstractDiscountType;
use Lunar\Models\Contracts\Cart;

class MyCustomDiscountType extends AbstractDiscountType
{
    /**
     * Return the name of the discount.
     */
    public function getName(): string
    {
        return 'Custom Discount Type';
    }

    /**
     * Execute and apply the discount if conditions are met.
     */
    public function apply(Cart $cart): Cart
    {
        // ...
        return $cart;
    }
}

Adding form fields for your discount in the admin panel

If you require fields in the Lunar admin for your discount type, ensure your discount implements Lunar\Admin\Base\LunarPanelDiscountInterface. You will need to provide the lunarPanelSchema, lunarPanelOnFill and lunarPanelOnSave methods.
<?php

namespace App\DiscountTypes;

use Lunar\Admin\Base\LunarPanelDiscountInterface;
use Lunar\DiscountTypes\AbstractDiscountType;
use Filament\Forms;

class MyCustomDiscountType extends AbstractDiscountType implements LunarPanelDiscountInterface
{
    /**
     * Return the schema to use in the Lunar admin panel
     */
    public function lunarPanelSchema(): array
    {
        return [
            Forms\Components\TextInput::make('data.my_field')
               ->label('My label')
               ->required(),
        ];
    }

    /**
     * Mutate the model data before displaying it in the admin form.
     */
    public function lunarPanelOnFill(array $data): array
    {
        // optionally do something with $data
        return $data;
    }

    /**
     * Mutate the form data before saving it to the discount model.
     */
    public function lunarPanelOnSave(array $data): array
    {
        // optionally do something with $data
        return $data;
    }
}