Skip to content

Commit 7c98cc2

Browse files
committed
magento#12817: Coupon code with canceled order.
1 parent 29e5957 commit 7c98cc2

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Observer;
7+
8+
use Magento\Framework\Event\Observer as EventObserver;
9+
use Magento\Framework\Event\ObserverInterface;
10+
11+
class SalesOrderAfterPlaceObserver implements ObserverInterface
12+
{
13+
/**
14+
* @var \Magento\SalesRule\Model\RuleFactory
15+
*/
16+
protected $_ruleFactory;
17+
18+
/**
19+
* @var \Magento\SalesRule\Model\RuleFactory
20+
*/
21+
protected $_ruleCustomerFactory;
22+
23+
/**
24+
* @var \Magento\SalesRule\Model\Coupon
25+
*/
26+
protected $_coupon;
27+
28+
/**
29+
* @var \Magento\SalesRule\Model\ResourceModel\Coupon\Usage
30+
*/
31+
protected $_couponUsage;
32+
33+
/**
34+
* @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
35+
* @param \Magento\SalesRule\Model\Rule\CustomerFactory $ruleCustomerFactory
36+
* @param \Magento\SalesRule\Model\Coupon $coupon
37+
* @param \Magento\SalesRule\Model\ResourceModel\Coupon\Usage $couponUsage
38+
*/
39+
public function __construct(
40+
\Magento\SalesRule\Model\RuleFactory $ruleFactory,
41+
\Magento\SalesRule\Model\Rule\CustomerFactory $ruleCustomerFactory,
42+
\Magento\SalesRule\Model\Coupon $coupon,
43+
\Magento\SalesRule\Model\ResourceModel\Coupon\Usage $couponUsage
44+
) {
45+
$this->_ruleFactory = $ruleFactory;
46+
$this->_ruleCustomerFactory = $ruleCustomerFactory;
47+
$this->_coupon = $coupon;
48+
$this->_couponUsage = $couponUsage;
49+
}
50+
51+
/**
52+
* @param EventObserver $observer
53+
* @return $this
54+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
55+
*/
56+
public function execute(EventObserver $observer)
57+
{
58+
$order = $observer->getEvent()->getOrder();
59+
60+
if (!$order || !$order->getAppliedRuleIds()) {
61+
return $this;
62+
}
63+
64+
// lookup rule ids
65+
$ruleIds = explode(',', $order->getAppliedRuleIds());
66+
$ruleIds = array_unique($ruleIds);
67+
68+
$ruleCustomer = null;
69+
$customerId = $order->getCustomerId();
70+
71+
// use each rule (and apply to customer, if applicable)
72+
foreach ($ruleIds as $ruleId) {
73+
if (!$ruleId) {
74+
continue;
75+
}
76+
/** @var \Magento\SalesRule\Model\Rule $rule */
77+
$rule = $this->_ruleFactory->create();
78+
$rule->load($ruleId);
79+
if ($rule->getId()) {
80+
$rule->loadCouponCode();
81+
$rule->setTimesUsed($rule->getTimesUsed() + 1);
82+
$rule->save();
83+
84+
if ($customerId) {
85+
/** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
86+
$ruleCustomer = $this->_ruleCustomerFactory->create();
87+
$ruleCustomer->loadByCustomerRule($customerId, $ruleId);
88+
89+
if ($ruleCustomer->getId()) {
90+
$ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + 1);
91+
} else {
92+
$ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
93+
}
94+
$ruleCustomer->save();
95+
}
96+
}
97+
}
98+
99+
$this->_coupon->load($order->getCouponCode(), 'code');
100+
if ($this->_coupon->getId()) {
101+
$this->_coupon->setTimesUsed($this->_coupon->getTimesUsed() + 1);
102+
$this->_coupon->save();
103+
if ($customerId) {
104+
$this->_couponUsage->updateCustomerCouponTimesUsed($customerId, $this->_coupon->getId());
105+
}
106+
}
107+
108+
return $this;
109+
}
110+
}

app/code/Magento/SalesRule/Plugin/CouponUsesIncrement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class CouponUsesIncrement extends AbstractCouponUses
2020
*/
2121
public function afterPlace(Order $subject, Order $result)
2222
{
23-
$this->updateCouponUses($subject, true);
23+
$this->updateCouponUses($subject, true);
2424

25-
return $subject;
25+
return $subject;
2626
}
2727
}

dev/tests/integration/testsuite/Magento/SalesRule/Plugin/CouponUsesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\SalesRule\Observer;
7+
namespace Magento\SalesRule\Plugin;
88

99
use Magento\Framework\DataObject;
1010
use Magento\Framework\ObjectManagerInterface;

0 commit comments

Comments
 (0)