Skip to content

Discounts

Overview

// ...

Discounts

php
Lunar\Models\Discount
FieldDescriptionExample
id
nameThe given name for the discount
handleThe unique handle for the discount
typeThe type of discountLunar\DiscountTypes\BuyXGetY
dataJSONAny data to be used by the type class
starts_atThe datetime the discount starts (required)
ends_atThe datetime the discount expires, if NULL it won't expire
usesHow many uses the discount has had
max_usesThe maximum times this discount can be applied storewide
priorityThe order of priority
stopWhether this discount will stop others after propagating
created_at
updated_at

Creating a discount

php
Lunar\Models\Discount::create([
    'name' => '20% Coupon',
    'handle' => '20_coupon',
    'type' => 'Lunar\DiscountTypes\Coupon',
    'data' => [
        'coupon' => '20OFF',
        'min_prices' => [
            'USD' => 2000 // $20
        ],
    ],
    'starts_at' => '2022-06-17 13:30:55',
    'ends_at' => null,
    'max_uses' => null,
])

Fetching a discount

The following scopes are available:

php
/**
* Query for discounts using the `start_at` and `end_at` dates.
 */
Discount::active()->get();

/**
* Query for discounts where the `uses` column is less than the `max_uses` column or `max_uses` is null.
 */
Discount::usable()->get();

/**
* Query for discounts where the associated products are of a certain type, based on given product ids.
 */
Discount::products($productIds, $type = 'condition');

Resetting the discount cache

For performance reasons the applicable discounts are cached per request. If you need to reset this cache (for example after adding a discount code) you should call resetDiscounts():

php
Discount::resetDiscounts();

Discount Purchasable

You can relate a purchasable to a discount via this model. Each has a type for whether it's a condition or reward.

  • condition - If your discount requires these purchasable models to be in the cart to activate
  • reward - Once the conditions are met, discount one of more of these purchasable models.
php
Lunar\Models\DiscountPurchasable
FieldDescriptionExample
id
discount_id
purchasable_typeproduct_variant
typecondition or reward
created_at
updated_at

Relationships

  • Purchasables discount_purchasables
  • Users - customer_user

Adding your own Discount type

php
<?php

namespace App\DiscountTypes;

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

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

    /**
     * Called just before cart totals are calculated.
     *
     * @return Cart
     */
    public function apply(Cart $cart): Cart
    {
        // ...
        return $cart;
    }
}
php
use Lunar\Facades\Discounts;

Discounts::addType(MyCustomDiscountType::class);