mothership-ec/cog-mothership-discount
最新稳定版本:2.1.4
Composer 安装命令:
composer require mothership-ec/cog-mothership-discount
包简介
Cog module for discount building blocks in Mothership
README 文档
README
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:
-
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
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
- 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
freeShippingis enabled on the discount. - Translations!
mothership-ec/cog-mothership-discount 适用场景与选型建议
mothership-ec/cog-mothership-discount 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.53k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「campaign」 「discount」 「offer」 「cog」 「mothership」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mothership-ec/cog-mothership-discount 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mothership-ec/cog-mothership-discount 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mothership-ec/cog-mothership-discount 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for interacting with the v2 MailChimp REST API
Adds Discount and Coupon support for SilverShop.
Symfony bundle for integrating the ZfrMailChimp library
Magento 2 Discount Amount Limiter
Zend Framework 2 module for interacting with the v2 MailChimp API, built on top of ZfrMailChimp
A simple mechanism for implementing tracked interactive elements
统计信息
- 总下载量: 1.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2015-02-25