2017 © Pedro Peláez
 

library cog-mothership-discount

Cog module for discount building blocks in Mothership

image

mothership-ec/cog-mothership-discount

Cog module for discount building blocks in Mothership

  • Monday, March 14, 2016
  • by thomasjthomasj
  • Repository
  • 4 Watchers
  • 0 Stars
  • 1,510 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 24 Open issues
  • 33 Versions
  • 0 % Grown

The README.md

Mothership Discount

Discount\Discount

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., (*1)

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:, (*2)

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

Fixed discounts can be added like this:, (*3)

$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:, (*4)

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

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., (*5)

You can find out whether a discount is currently active or not by using:, (*6)

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

Criteria

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

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

$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:, (*9)

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

Discount Decorators

Discounts can be created, edited, deleted and loaded:, (*10)

$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:, (*11)

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

You can load discounts using:, (*12)

  • Will only return one discount or false, (*13)

    • getByID($id) (will return one discount or false)
    • getByCode($id) (will return one discount or false)
  • Will return array of discounts:, (*14)

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

Discount\OrderDiscountFactory

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:, (*15)

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

$orderDiscount = $orderDiscountFactory->createOrderDiscount();

Discount\Validator

The Validator is a component which tries to apply a discount to a certain order. If the discount is not applicable to the order, OrderValidityExceptions 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:, (*16)

$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

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)., (*17)

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!

The Versions

14/03 2016

dev-develop

dev-develop http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

14/03 2016

dev-master

9999999-dev http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

14/03 2016

2.1.4

2.1.4.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

16/11 2015

dev-feature/ux/admin-rebuild

dev-feature/ux/admin-rebuild http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

18/09 2015

2.1.3

2.1.3.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

05/08 2015

2.1.2

2.1.2.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

04/08 2015

2.1.1

2.1.1.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

28/07 2015

2.1.0

2.1.0.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

05/03 2015

dev-feature/renable-usage-check

dev-feature/renable-usage-check http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

25/02 2015

2.0.0

2.0.0.0 http://mothership.ec

Cog module for discount building blocks in Mothership

  Sources   Download

GPL-3.0+

The Requires

 

by The Mothership Development Team

offer discount campaign cog mothership

08/01 2015

dev-compatibility/stoneham

dev-compatibility/stoneham http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~2.0
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

08/01 2015

1.5.2

1.5.2.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~2.0
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

08/01 2015

1.5.1

1.5.1.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.9
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

23/12 2014

1.5.0

1.5.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

23/12 2014

1.4.1

1.4.1.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

08/12 2014

1.4.0

1.4.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

25/09 2014

dev-feature/lazy-loading

dev-feature/lazy-loading http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

19/09 2014

1.3.2

1.3.2.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0 | ~4.0

 

by The Message Development Team

offer discount campaign cog mothership

14/08 2014

1.3.1

1.3.1.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0

 

by The Message Development Team

offer discount campaign cog mothership

24/06 2014

1.3.0

1.3.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-cp ~1.2
  • message/cog-mothership-commerce ~2.0 | ~3.0

 

by The Message Development Team

offer discount campaign cog mothership

17/06 2014

dev-feature/feature/discount-image-issue

dev-feature/feature/discount-image-issue http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0 | ~3.0

 

by The Message Development Team

offer discount campaign cog mothership

17/06 2014

1.2.3

1.2.3.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0 | ~3.0

 

by The Message Development Team

offer discount campaign cog mothership

11/06 2014

1.2.2

1.2.2.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0 | ~3.0

 

by The Message Development Team

offer discount campaign cog mothership

02/05 2014

1.2.1

1.2.1.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0

 

by The Message Development Team

offer discount campaign cog mothership

02/05 2014

1.2.0

1.2.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0

 

by The Message Development Team

offer discount campaign cog mothership

02/05 2014

dev-feature/discount-code-validation

dev-feature/discount-code-validation http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0

 

by The Message Development Team

offer discount campaign cog mothership

18/03 2014

1.1.0

1.1.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~2.0

 

by The Message Development Team

offer discount campaign cog mothership

10/01 2014

1.0.4

1.0.4.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~2.0
  • message/cog-user ~1.0
  • message/cog-mothership-commerce ~1.0

 

by The Message Development Team

offer discount campaign cog mothership

04/12 2013

1.0.3

1.0.3.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.3.0
  • message/cog-user 1.*
  • message/cog-mothership-commerce 1.*

 

by The Message Development Team

offer discount campaign cog mothership

22/11 2013

1.0.2

1.0.2.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.3.0
  • message/cog-user 1.*
  • message/cog-mothership-commerce 1.*

 

by The Message Development Team

offer discount campaign cog mothership

20/11 2013

1.0.1

1.0.1.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

proprietary

The Requires

  • php >=5.3.0
  • message/cog-user 1.*
  • message/cog-mothership-commerce 1.*

 

by The Message Development Team

offer discount campaign cog mothership

18/11 2013

1.0.0

1.0.0.0 http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

The Requires

  • php >=5.3.0
  • message/cog-user 1.0.*
  • message/cog-mothership-commerce 1.0.*

 

by The Message Development Team

offer discount campaign cog mothership

12/11 2013

dev-ux/improvements

dev-ux/improvements http://message.co.uk

Cog module for discount building blocks in Mothership

  Sources   Download

The Requires

  • php >=5.3.0
  • message/cog-user dev-master
  • message/cog-mothership-commerce dev-master

 

by The Message Development Team

offer discount campaign cog mothership