Download the PHP package mothership-ec/cog-mothership-discount without Composer

On this page you can find all versions of the php package mothership-ec/cog-mothership-discount. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cog-mothership-discount

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.

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

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

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

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:

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:

$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:

$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).

ToDo


All versions of cog-mothership-discount with dependencies

PHP Build Version
Package Version
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mothership-ec/cog-mothership-discount contains the following files

Loading the files please wait ....