PHPackages                             mothership-ec/cog-mothership-discount - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mothership-ec/cog-mothership-discount

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

mothership-ec/cog-mothership-discount
=====================================

Cog module for discount building blocks in Mothership

2.1.4(10y ago)01.5k1[23 issues](https://github.com/mothership-ec/cog-mothership-discount/issues)[1 PRs](https://github.com/mothership-ec/cog-mothership-discount/pulls)2GPL-3.0+PHPPHP &gt;=5.4.0

Since Nov 12Pushed 10y ago4 watchersCompare

[ Source](https://github.com/mothership-ec/cog-mothership-discount)[ Packagist](https://packagist.org/packages/mothership-ec/cog-mothership-discount)[ Docs](http://mothership.ec)[ RSS](/packages/mothership-ec-cog-mothership-discount/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (6)Versions (33)Used By (2)

Mothership Discount
===================

[](#mothership-discount)

Discount\\Discount
------------------

[](#discountdiscount)

The Discount-class is the entity in the heart of the discount-cogule. Discounts consist of (at least) an id, a code and a name.

### Benefit

[](#benefit)

A discount has information about it's benefit. It either has a fixed amount or a percentage discount, moreover free shipping can be applied:

```
$discount->percentage = 40;
$discount->freeshipping = true;

```

Fixed discounts can be added like this:

```
$amount = new DiscountAmount();
$amount->currencyID = 'GBP';
$amount->locale = 'en_GB';
$amount->amount = 20.5;

$discount->addDiscountAmount($amount);

```

And you can get a discount amount for a certain currency ID like this:

```
$discount->getDiscountAmountForCurrencyID('GBP'); // returns $amount
$discount->getDiscountAmountForCurrencyID('EUR'); // returns null

```

### Activity

[](#activity)

To define whether a discount is active, it has a start and end date. If the start AND end-date are null, it means the discount is always active, if only one of them is set, the discount is always active after the start/before the end-date. Both start and end-date are DateTime-Objects.

You can find out whether a discount is currently active or not by using:

```
$discount->isActive(); // returns boolean

```

### Criteria

[](#criteria)

A discount can define thresholds for certain locales and currencies. If the threshold is not reached, the order can not use the discount.

Thresholds can be added using `addThreshold($threshold)`. To get a threshold, you best use `getThresholdForCurrencyID($currencyID)`:

```
$threshold = new Threshold();
$threshold->currencyID = 'GBP';
$threshold->locale = 'en_GB';
$threshold->threshold = 20.5;

$discount->addThreshold($threshold);
$discount->getThresholdForCurrencyID('GBP'); // returns $threshold
$discount->getThresholdForCurrencyID('EUR'); // returns null

```

Moreover discounts can either apply to a whole order or to specific products only. When the `products`-Array is empty, this means that the discount applies to the whole order. To test this, you can use:

```
$discount->appliesToOrder() // true if $products is empty

```

Discount Decorators
-------------------

[](#discount-decorators)

Discounts can be created, edited, deleted and loaded:

```
$discount = new Discount();
$discount->name = "Test Discount";
$discount->code = "UNIQUECODE";

$discount = $this->get('discount.create')->create($discount);
$discount->name = 'Test Change';

$discount = $this->get('discount.edit')->save($discount);

$discount = $this->get('discount.delete')->delete($discount);
$discount = $this->get('discount.delete')->restore($discount);

```

The loader can be used like this:

```
$discount = $this->get('discount.loader')->getByCode("UNIQUECODE");

```

You can load discounts using:

- Will only return one discount or false

    - `getByID($id)` (will return one discount or false)
    - `getByCode($id)` (will return one discount or false)
- Will return array of discounts:

    - `getAll()`
    - `getActive()`
    - `getInactive()` (both upcoming and expired)
    - `getByProduct($product)`
    - `getByDateRange($from, $to)` (returns discounts active between $from and $to)

Discount\\OrderDiscountFactory
------------------------------

[](#discountorderdiscountfactory)

To create a `Commerce\Order\Entity\Discount\Discount` (further referred to as `OrderDiscount`) from a `Discount\Discount` and a `Commerce\Order\Order`, you can use the `Discount\OrderDiscountFactory`-class. The use is pretty straight forward and therefore doesn't really require a lot of explanation:

```
$orderDiscountFactory = $this->get('discount.order-discount-factory')
	->setOrder($order)
	->setDiscount($discount);

$orderDiscount = $orderDiscountFactory->createOrderDiscount();

```

Discount\\Validator
-------------------

[](#discountvalidator)

The Validator is a component which tries to apply a discount to a certain order. If the discount is not applicable to the order, `OrderValidityException`s will be thrown. Otherwise the validator returns the order-discount-object(using the OrderDiscountFactory) for the given discount-code and order. From inside a controller you can use the validator using the `discount.validator`-Service. This could look like this:

```
$discountValidator = $this->get('discount.validator')
	->setOrder($order);

try {
	$orderDiscount = $discountValidator->validate($code);
} catch (Discount\OrderValidityException $e) {
	$this->addFlash('error', $e->getMessage());
}

if($orderDiscount) { // validator returns orderDiscount-object on success
	$order->discounts->append($orderDiscount);
	$this->addFlash('success', 'You successfully added a discount');
}

```

EventListeners and integration with Commerce\\Order
---------------------------------------------------

[](#eventlisteners-and-integration-with-commerceorder)

To integrate the Discount-cogule with the Basket in Commerce, there is an `Discount\EventListener`, which listens to the `CREATE_START` and `ASSEMBLER_UPDATE` Order-Events to validate and update discounts. This is important because we want to be able to edit both discounts and the basket at any time. The actual work on setting discount-totals and item-discounts on the order is happening in `Commerce` and only uses OrderDiscounts. This EventListener validates whether an order can still have a certain discount after a change(e.g. removing an item) and removes the discount if not. Furthermore it updates the OrderDiscount by loading the latest verion of the discount(using the discount code).

ToDo
----

[](#todo)

- Currency Collection necessary for iterating over all currencies in create-view!
- Validation for Start/End-Date and Percentage/Fixed Amount instead of checking it in the controller
- Add a method to add discount-amount if `freeShipping` is enabled on the discount.
- Translations!

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 58.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~36 days

Recently: every ~57 days

Total

24

Last Release

3711d ago

Major Versions

1.5.2 → 2.0.02015-02-25

PHP version history (2 changes)1.0.0PHP &gt;=5.3.0

1.0.4PHP &gt;=5.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c80abec1ec847de90ada27e2239c31f4374ed1d91912493f5415a688c269a78?d=identicon)[thomasjthomasj](/maintainers/thomasjthomasj)

---

Top Contributors

[![thomasjthomasj](https://avatars.githubusercontent.com/u/4520924?v=4)](https://github.com/thomasjthomasj "thomasjthomasj (149 commits)")[![irisSchaffer](https://avatars.githubusercontent.com/u/4313088?v=4)](https://github.com/irisSchaffer "irisSchaffer (53 commits)")[![lsjroberts](https://avatars.githubusercontent.com/u/3817697?v=4)](https://github.com/lsjroberts "lsjroberts (19 commits)")[![eleanorshakeshaft](https://avatars.githubusercontent.com/u/5039349?v=4)](https://github.com/eleanorshakeshaft "eleanorshakeshaft (15 commits)")[![kuiche](https://avatars.githubusercontent.com/u/5089510?v=4)](https://github.com/kuiche "kuiche (9 commits)")[![joeholdcroft](https://avatars.githubusercontent.com/u/2511043?v=4)](https://github.com/joeholdcroft "joeholdcroft (6 commits)")[![aislingbrock](https://avatars.githubusercontent.com/u/3073759?v=4)](https://github.com/aislingbrock "aislingbrock (3 commits)")[![dannyhannah](https://avatars.githubusercontent.com/u/1499449?v=4)](https://github.com/dannyhannah "dannyhannah (2 commits)")

---

Tags

campaigncogmothershipofferdiscount

### Embed Badge

![Health badge](/badges/mothership-ec-cog-mothership-discount/health.svg)

```
[![Health](https://phpackages.com/badges/mothership-ec-cog-mothership-discount/health.svg)](https://phpackages.com/packages/mothership-ec-cog-mothership-discount)
```

###  Alternatives

[cybercog/laravel-optimus

An Optimus bridge for Laravel. Id obfuscation based on Knuth's multiplicative hashing method.

192564.1k](/packages/cybercog-laravel-optimus)[frittenkeez/laravel-vouchers

Voucher system for Laravel 9+

5819.9k2](/packages/frittenkeez-laravel-vouchers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
