mothership-ec / cog-mothership-discount
Mothership中的折扣构建块Cog模块
Requires
- php: >=5.4.0
- mothership-ec/cog: ~4.0
- mothership-ec/cog-mothership-commerce: ~5.8
- mothership-ec/cog-mothership-cp: ~3.0
- mothership-ec/cog-mothership-file-manager: ~3.1
- mothership-ec/cog-mothership-reports: ~2.0
- mothership-ec/cog-user: ~2.0
- dev-develop
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-master
- dev-feature/ux/admin-rebuild
- dev-feature/renable-usage-check
- dev-compatibility/stoneham
- dev-feature/lazy-loading
- dev-feature/feature/discount-image-issue
- dev-feature/discount-code-validation
- dev-ux/improvements
This package is not auto-updated.
Last update: 2021-03-22 11:07:53 UTC
README
Discount\Discount
Discount类是折扣-cogule的核心实体。折扣至少包括一个id、一个代码和一个名称。
优惠
折扣包含关于其优惠的信息。它可以是固定金额或百分比折扣,此外还可以应用免费送货。
$discount->percentage = 40;
$discount->freeshipping = true;
固定折扣可以添加如下:
$amount = new DiscountAmount();
$amount->currencyID = 'GBP';
$amount->locale = 'en_GB';
$amount->amount = 20.5;
$discount->addDiscountAmount($amount);
您可以通过这种方式获取特定货币ID的折扣金额:
$discount->getDiscountAmountForCurrencyID('GBP'); // returns $amount
$discount->getDiscountAmountForCurrencyID('EUR'); // returns null
活动
为了定义折扣是否活跃,它有一个开始日期和结束日期。如果开始日期和结束日期都为空,则表示折扣始终活跃;如果只设置其中一个,则折扣在开始/结束日期之后始终活跃。开始日期和结束日期都是DateTime对象。
您可以使用以下方式确定折扣当前是否活跃:
$discount->isActive(); // returns boolean
标准
折扣可以为特定地区和货币定义阈值。如果未达到阈值,则订单无法使用折扣。
可以使用 addThreshold($threshold)
添加阈值。要获取阈值,最好使用 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
此外,折扣可以应用于整个订单或仅应用于特定产品。当 products
数组为空时,这意味着折扣适用于整个订单。要测试此功能,您可以使用:
$discount->appliesToOrder() // true if $products is empty
折扣装饰器
可以创建、编辑、删除和加载折扣
$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);
加载器可以使用如下:
$discount = $this->get('discount.loader')->getByCode("UNIQUECODE");
您可以使用以下方式加载折扣:
-
将仅返回一个折扣或false
getByID($id)
(将返回一个折扣或false)getByCode($id)
(将返回一个折扣或false)
-
将返回折扣数组
getAll()
getActive()
getInactive()
(包括即将到来和已过期)getByProduct($product)
getByDateRange($from, $to)
(返回在$from和$to之间活跃的折扣)
Discount\OrderDiscountFactory
要从Discount\Discount
和Commerce\Order\Order
创建一个Commerce\Order\Entity\Discount\Discount
(以下称为OrderDiscount
),可以使用Discount\OrderDiscountFactory
类。使用方法相当直接,因此不需要过多解释。
$orderDiscountFactory = $this->get('discount.order-discount-factory')
->setOrder($order)
->setDiscount($discount);
$orderDiscount = $orderDiscountFactory->createOrderDiscount();
Discount\Validator
校验器是一个组件,它尝试将折扣应用到特定的订单上。如果折扣不适用于该订单,将抛出OrderValidityException
异常。否则,校验器将返回给定折扣码和订单的订单-折扣对象(使用订单折扣工厂)。在控制器内部,您可以使用discount.validator
服务来使用校验器。这可能看起来是这样的
$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');
}
事件监听器与Commerce\Order的集成
为了将折扣组件与Commerce中的购物篮集成,有一个Discount\EventListener
,它会监听CREATE_START
和ASSEMBLER_UPDATE
订单事件以验证和更新折扣。这是很重要的,因为我们希望在任何时候都可以编辑折扣和购物篮。实际在订单上设置折扣总额和项目折扣的工作是在Commerce
中完成的,并且只使用订单折扣。此事件监听器验证在更改(例如,删除项目)后订单是否还可以拥有特定的折扣,如果不可以,则删除该折扣。此外,它通过加载最新的折扣版本(使用折扣码)来更新订单折扣。
待办事项
- 创建视图时,需要货币集合来遍历所有货币!
- 对开始/结束日期和百分比/固定金额进行验证,而不是在控制器中检查
- 如果折扣上启用了
freeShipping
,添加一个方法来添加折扣金额。 - 翻译!