Skip to content

Commit 1d1fde9

Browse files
Merge remote-tracking branch 'origin/MC-34649' into 2.4-develop-pr34
2 parents b9762ed + a1cdf38 commit 1d1fde9

File tree

16 files changed

+565
-171
lines changed

16 files changed

+565
-171
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Model\Coupon\Quote;
9+
10+
use Magento\Quote\Api\Data\CartInterface;
11+
use Magento\SalesRule\Model\Coupon\Usage\Processor as CouponUsageProcessor;
12+
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo;
13+
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfoFactory;
14+
15+
/**
16+
* Updates the coupon usages from quote
17+
*/
18+
class UpdateCouponUsages
19+
{
20+
/**
21+
* @var CouponUsageProcessor
22+
*/
23+
private $couponUsageProcessor;
24+
25+
/**
26+
* @var UpdateInfoFactory
27+
*/
28+
private $updateInfoFactory;
29+
30+
/**
31+
* @param CouponUsageProcessor $couponUsageProcessor
32+
* @param UpdateInfoFactory $updateInfoFactory
33+
*/
34+
public function __construct(
35+
CouponUsageProcessor $couponUsageProcessor,
36+
UpdateInfoFactory $updateInfoFactory
37+
) {
38+
$this->couponUsageProcessor = $couponUsageProcessor;
39+
$this->updateInfoFactory = $updateInfoFactory;
40+
}
41+
42+
/**
43+
* Executes the current command
44+
*
45+
* @param CartInterface $quote
46+
* @param bool $increment
47+
* @return void
48+
*/
49+
public function execute(CartInterface $quote, bool $increment): void
50+
{
51+
if (!$quote->getAppliedRuleIds()) {
52+
return;
53+
}
54+
55+
/** @var UpdateInfo $updateInfo */
56+
$updateInfo = $this->updateInfoFactory->create();
57+
$updateInfo->setAppliedRuleIds(explode(',', $quote->getAppliedRuleIds()));
58+
$updateInfo->setCouponCode((string)$quote->getCouponCode());
59+
$updateInfo->setCustomerId((int)$quote->getCustomerId());
60+
$updateInfo->setIsIncrement($increment);
61+
62+
$this->couponUsageProcessor->process($updateInfo);
63+
}
64+
}

app/code/Magento/SalesRule/Model/Coupon/UpdateCouponUsages.php

Lines changed: 23 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,39 @@
88
namespace Magento\SalesRule\Model\Coupon;
99

1010
use Magento\Sales\Api\Data\OrderInterface;
11-
use Magento\SalesRule\Model\Coupon;
12-
use Magento\SalesRule\Model\ResourceModel\Coupon\Usage;
13-
use Magento\SalesRule\Model\Rule\CustomerFactory;
14-
use Magento\SalesRule\Model\RuleFactory;
11+
use Magento\SalesRule\Model\Coupon\Usage\Processor as CouponUsageProcessor;
12+
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo;
13+
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfoFactory;
1514

1615
/**
17-
* Updates the coupon usages.
16+
* Updates the coupon usages
1817
*/
1918
class UpdateCouponUsages
2019
{
2120
/**
22-
* @var RuleFactory
21+
* @var CouponUsageProcessor
2322
*/
24-
private $ruleFactory;
23+
private $couponUsageProcessor;
2524

2625
/**
27-
* @var RuleFactory
26+
* @var UpdateInfoFactory
2827
*/
29-
private $ruleCustomerFactory;
28+
private $updateInfoFactory;
3029

3130
/**
32-
* @var Coupon
33-
*/
34-
private $coupon;
35-
36-
/**
37-
* @var Usage
38-
*/
39-
private $couponUsage;
40-
41-
/**
42-
* @param RuleFactory $ruleFactory
43-
* @param CustomerFactory $ruleCustomerFactory
44-
* @param Coupon $coupon
45-
* @param Usage $couponUsage
31+
* @param CouponUsageProcessor $couponUsageProcessor
32+
* @param UpdateInfoFactory $updateInfoFactory
4633
*/
4734
public function __construct(
48-
RuleFactory $ruleFactory,
49-
CustomerFactory $ruleCustomerFactory,
50-
Coupon $coupon,
51-
Usage $couponUsage
35+
CouponUsageProcessor $couponUsageProcessor,
36+
UpdateInfoFactory $updateInfoFactory
5237
) {
53-
$this->ruleFactory = $ruleFactory;
54-
$this->ruleCustomerFactory = $ruleCustomerFactory;
55-
$this->coupon = $coupon;
56-
$this->couponUsage = $couponUsage;
38+
$this->couponUsageProcessor = $couponUsageProcessor;
39+
$this->updateInfoFactory = $updateInfoFactory;
5740
}
5841

5942
/**
60-
* Executes the current command.
43+
* Executes the current command
6144
*
6245
* @param OrderInterface $subject
6346
* @param bool $increment
@@ -68,86 +51,16 @@ public function execute(OrderInterface $subject, bool $increment): OrderInterfac
6851
if (!$subject || !$subject->getAppliedRuleIds()) {
6952
return $subject;
7053
}
71-
// lookup rule ids
72-
$ruleIds = explode(',', $subject->getAppliedRuleIds());
73-
$ruleIds = array_unique($ruleIds);
74-
$customerId = (int)$subject->getCustomerId();
75-
// use each rule (and apply to customer, if applicable)
76-
foreach ($ruleIds as $ruleId) {
77-
if (!$ruleId) {
78-
continue;
79-
}
80-
$this->updateRuleUsages($increment, (int)$ruleId, $customerId);
81-
}
82-
$this->updateCouponUsages($subject, $increment, $customerId);
83-
84-
return $subject;
85-
}
8654

87-
/**
88-
* Update the number of rule usages.
89-
*
90-
* @param bool $increment
91-
* @param int $ruleId
92-
* @param int $customerId
93-
*/
94-
private function updateRuleUsages(bool $increment, int $ruleId, int $customerId)
95-
{
96-
/** @var \Magento\SalesRule\Model\Rule $rule */
97-
$rule = $this->ruleFactory->create();
98-
$rule->load($ruleId);
99-
if ($rule->getId()) {
100-
$rule->loadCouponCode();
101-
if ($increment || $rule->getTimesUsed() > 0) {
102-
$rule->setTimesUsed($rule->getTimesUsed() + ($increment ? 1 : -1));
103-
$rule->save();
104-
}
105-
if ($customerId) {
106-
$this->updateCustomerRuleUsages($increment, $ruleId, $customerId);
107-
}
108-
}
109-
}
55+
/** @var UpdateInfo $updateInfo */
56+
$updateInfo = $this->updateInfoFactory->create();
57+
$updateInfo->setAppliedRuleIds(explode(',', $subject->getAppliedRuleIds()));
58+
$updateInfo->setCouponCode((string)$subject->getCouponCode());
59+
$updateInfo->setCustomerId((int)$subject->getCustomerId());
60+
$updateInfo->setIsIncrement($increment);
11061

111-
/**
112-
* Update the number of rule usages per customer.
113-
*
114-
* @param bool $increment
115-
* @param int $ruleId
116-
* @param int $customerId
117-
*/
118-
private function updateCustomerRuleUsages(bool $increment, int $ruleId, int $customerId): void
119-
{
120-
/** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
121-
$ruleCustomer = $this->ruleCustomerFactory->create();
122-
$ruleCustomer->loadByCustomerRule($customerId, $ruleId);
123-
if ($ruleCustomer->getId()) {
124-
if ($increment || $ruleCustomer->getTimesUsed() > 0) {
125-
$ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + ($increment ? 1 : -1));
126-
}
127-
} elseif ($increment) {
128-
$ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
129-
}
130-
$ruleCustomer->save();
131-
}
62+
$this->couponUsageProcessor->process($updateInfo);
13263

133-
/**
134-
* Update the number of coupon usages.
135-
*
136-
* @param OrderInterface $subject
137-
* @param bool $increment
138-
* @param int $customerId
139-
*/
140-
private function updateCouponUsages(OrderInterface $subject, bool $increment, int $customerId): void
141-
{
142-
$this->coupon->load($subject->getCouponCode(), 'code');
143-
if ($this->coupon->getId()) {
144-
if ($increment || $this->coupon->getTimesUsed() > 0) {
145-
$this->coupon->setTimesUsed($this->coupon->getTimesUsed() + ($increment ? 1 : -1));
146-
$this->coupon->save();
147-
}
148-
if ($customerId) {
149-
$this->couponUsage->updateCustomerCouponTimesUsed($customerId, $this->coupon->getId(), $increment);
150-
}
151-
}
64+
return $subject;
15265
}
15366
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Model\Coupon\Usage;
9+
10+
use Magento\SalesRule\Model\Coupon;
11+
use Magento\SalesRule\Model\ResourceModel\Coupon\Usage;
12+
use Magento\SalesRule\Model\Rule\CustomerFactory;
13+
use Magento\SalesRule\Model\RuleFactory;
14+
15+
/**
16+
* Processor to update coupon usage
17+
*/
18+
class Processor
19+
{
20+
/**
21+
* @var RuleFactory
22+
*/
23+
private $ruleFactory;
24+
25+
/**
26+
* @var RuleFactory
27+
*/
28+
private $ruleCustomerFactory;
29+
30+
/**
31+
* @var Coupon
32+
*/
33+
private $coupon;
34+
35+
/**
36+
* @var Usage
37+
*/
38+
private $couponUsage;
39+
40+
/**
41+
* @param RuleFactory $ruleFactory
42+
* @param CustomerFactory $ruleCustomerFactory
43+
* @param Coupon $coupon
44+
* @param Usage $couponUsage
45+
*/
46+
public function __construct(
47+
RuleFactory $ruleFactory,
48+
CustomerFactory $ruleCustomerFactory,
49+
Coupon $coupon,
50+
Usage $couponUsage
51+
) {
52+
$this->ruleFactory = $ruleFactory;
53+
$this->ruleCustomerFactory = $ruleCustomerFactory;
54+
$this->coupon = $coupon;
55+
$this->couponUsage = $couponUsage;
56+
}
57+
58+
/**
59+
* Update coupon usage
60+
*
61+
* @param UpdateInfo $updateInfo
62+
*/
63+
public function process(UpdateInfo $updateInfo): void
64+
{
65+
if (empty($updateInfo->getAppliedRuleIds())) {
66+
return;
67+
}
68+
69+
if (!empty($updateInfo->getCouponCode())) {
70+
$this->updateCouponUsages($updateInfo);
71+
}
72+
$isIncrement = $updateInfo->isIncrement();
73+
$customerId = $updateInfo->getCustomerId();
74+
// use each rule (and apply to customer, if applicable)
75+
foreach (array_unique($updateInfo->getAppliedRuleIds()) as $ruleId) {
76+
if (!(int)$ruleId) {
77+
continue;
78+
}
79+
$this->updateRuleUsages($isIncrement, (int)$ruleId);
80+
if ($customerId) {
81+
$this->updateCustomerRuleUsages($isIncrement, (int)$ruleId, $customerId);
82+
}
83+
}
84+
}
85+
86+
/**
87+
* Update the number of coupon usages
88+
*
89+
* @param UpdateInfo $updateInfo
90+
*/
91+
private function updateCouponUsages(UpdateInfo $updateInfo): void
92+
{
93+
$isIncrement = $updateInfo->isIncrement();
94+
$this->coupon->load($updateInfo->getCouponCode(), 'code');
95+
if ($this->coupon->getId()) {
96+
if ($updateInfo->isIncrement() || $this->coupon->getTimesUsed() > 0) {
97+
$this->coupon->setTimesUsed($this->coupon->getTimesUsed() + ($isIncrement ? 1 : -1));
98+
$this->coupon->save();
99+
}
100+
if ($updateInfo->getCustomerId()) {
101+
$this->couponUsage->updateCustomerCouponTimesUsed(
102+
$updateInfo->getCustomerId(),
103+
$this->coupon->getId(),
104+
$isIncrement
105+
);
106+
}
107+
}
108+
}
109+
110+
/**
111+
* Update the number of rule usages
112+
*
113+
* @param bool $isIncrement
114+
* @param int $ruleId
115+
*/
116+
private function updateRuleUsages(bool $isIncrement, int $ruleId): void
117+
{
118+
$rule = $this->ruleFactory->create();
119+
$rule->load($ruleId);
120+
if ($rule->getId()) {
121+
$rule->loadCouponCode();
122+
if ($isIncrement || $rule->getTimesUsed() > 0) {
123+
$rule->setTimesUsed($rule->getTimesUsed() + ($isIncrement ? 1 : -1));
124+
$rule->save();
125+
}
126+
}
127+
}
128+
129+
/**
130+
* Update the number of rule usages per customer
131+
*
132+
* @param bool $isIncrement
133+
* @param int $ruleId
134+
* @param int $customerId
135+
*/
136+
private function updateCustomerRuleUsages(bool $isIncrement, int $ruleId, int $customerId): void
137+
{
138+
$ruleCustomer = $this->ruleCustomerFactory->create();
139+
$ruleCustomer->loadByCustomerRule($customerId, $ruleId);
140+
if ($ruleCustomer->getId()) {
141+
if ($isIncrement || $ruleCustomer->getTimesUsed() > 0) {
142+
$ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + ($isIncrement ? 1 : -1));
143+
}
144+
} elseif ($isIncrement) {
145+
$ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
146+
}
147+
$ruleCustomer->save();
148+
}
149+
}

0 commit comments

Comments
 (0)