Skip to content

Commit 679ca72

Browse files
committed
Merge branch 'ACP2E-2627' of https://github.com/adobe-commerce-tier-4/magento2ce into T4-PR-03-18-2024
2 parents 8c664a2 + d513852 commit 679ca72

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ public function execute(CartInterface $quote, bool $increment): void
7272

7373
$this->couponUsagePublisher->publish($updateInfo);
7474
$this->processor->updateCustomerRulesUsages($updateInfo);
75+
$this->processor->updateCouponUsages($updateInfo);
7576
}
7677
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\SalesRule\Model\Coupon\Usage\Processor as CouponUsageProcessor;
1313
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo;
1414
use Magento\SalesRule\Model\Coupon\Usage\UpdateInfoFactory;
15+
use Magento\SalesRule\Model\Service\CouponUsagePublisher;
1516

1617
/**
1718
* Updates the coupon usages
@@ -28,16 +29,25 @@ class UpdateCouponUsages
2829
*/
2930
private $updateInfoFactory;
3031

32+
/**
33+
* @var CouponUsagePublisher
34+
*/
35+
private $couponUsagePublisher;
36+
3137
/**
3238
* @param CouponUsageProcessor $couponUsageProcessor
3339
* @param UpdateInfoFactory $updateInfoFactory
40+
* @param ?CouponUsagePublisher $couponUsagePublisher
3441
*/
3542
public function __construct(
3643
CouponUsageProcessor $couponUsageProcessor,
37-
UpdateInfoFactory $updateInfoFactory
44+
UpdateInfoFactory $updateInfoFactory,
45+
?CouponUsagePublisher $couponUsagePublisher = null
3846
) {
3947
$this->couponUsageProcessor = $couponUsageProcessor;
4048
$this->updateInfoFactory = $updateInfoFactory;
49+
$this->couponUsagePublisher = $couponUsagePublisher
50+
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CouponUsagePublisher::class);
4151
}
4252

4353
/**
@@ -66,7 +76,9 @@ public function execute(OrderInterface $subject, bool $increment): OrderInterfac
6676
$updateInfo->setCouponAlreadyApplied(true);
6777
}
6878

69-
$this->couponUsageProcessor->process($updateInfo);
79+
$this->couponUsagePublisher->publish($updateInfo);
80+
$this->couponUsageProcessor->updateCustomerRulesUsages($updateInfo);
81+
$this->couponUsageProcessor->updateCouponUsages($updateInfo);
7082

7183
return $subject;
7284
}

app/code/Magento/SalesRule/Model/Coupon/Usage/Processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function updateRuleUsages(UpdateInfo $updateInfo): void
8989
}
9090

9191
$rule->loadCouponCode();
92-
if ($isIncrement || $rule->getTimesUsed() > 0) {
92+
if ((!$updateInfo->isCouponAlreadyApplied() && $isIncrement) || !$isIncrement) {
9393
$rule->setTimesUsed($rule->getTimesUsed() + ($isIncrement ? 1 : -1));
9494
$rule->save();
9595
}

app/code/Magento/SalesRule/Model/CouponUsageConsumer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public function process(OperationInterface $operation): void
8080
$data = $this->serializer->unserialize($serializedData);
8181
$updateInfo = $this->updateInfoFactory->create();
8282
$updateInfo->setData($data);
83-
$this->processor->updateCouponUsages($updateInfo);
8483
$this->processor->updateRuleUsages($updateInfo);
8584
} catch (NotFoundException $e) {
8685
$this->logger->critical($e->getMessage());

app/code/Magento/SalesRule/etc/db_schema.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
default="0" comment="Discount Step"/>
3737
<column xsi:type="smallint" name="apply_to_shipping" unsigned="true" nullable="false"
3838
identity="false" default="0" comment="Apply To Shipping"/>
39-
<column xsi:type="int" name="times_used" unsigned="true" nullable="false" identity="false"
39+
<column xsi:type="int" name="times_used" unsigned="false" nullable="false" identity="false"
4040
default="0" comment="Times Used"/>
4141
<column xsi:type="smallint" name="is_rss" unsigned="false" nullable="false" identity="false"
4242
default="0" comment="Is Rss"/>

dev/tests/integration/testsuite/Magento/SalesRule/Model/Coupon/UpdateCouponUsagesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,44 @@ public function testCancelOrderBeforeUsageConsumerExecution(): void
124124
$this->cartManagement->placeOrder($cart->getId());
125125
$consumer->process(1);
126126
}
127+
128+
#[
129+
DataFixture(ProductFixture::class, as: 'p1'),
130+
DataFixture(
131+
SalesRuleFixture::class,
132+
['coupon_code' => 'once', 'uses_per_coupon' => 1, 'discount_amount' => 10]
133+
),
134+
DataFixture(Customer::class, as: 'customer'),
135+
136+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], 'cart1'),
137+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart1.id$', 'product_id' => '$p1.id$']),
138+
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart1.id$']),
139+
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart1.id$']),
140+
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart1.id$']),
141+
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart1.id$']),
142+
143+
DataFixture(GuestCart::class, as: 'cart2'),
144+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart2.id$', 'product_id' => '$p1.id$']),
145+
DataFixture(SetBillingAddress::class, ['cart_id' => '$cart2.id$']),
146+
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart2.id$']),
147+
DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart2.id$']),
148+
DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart2.id$']),
149+
]
150+
public function testCancelOrderBeforeConsumerAndRuleTimesUsed(): void
151+
{
152+
$cart = $this->fixtures->get('cart1');
153+
$this->couponManagement->set($cart->getId(), 'once');
154+
$orderId = $this->cartManagement->placeOrder($cart->getId());
155+
$this->orderManagement->cancel($orderId);
156+
$consumer = $this->consumerFactory->get('sales.rule.update.coupon.usage');
157+
$consumer->process(2);
158+
159+
$cart = $this->fixtures->get('cart2');
160+
$customer = $this->fixtures->get('customer');
161+
$this->cartManagement->assignCustomer($cart->getId(), $customer->getId(), 1);
162+
$cart = $this->cartRepository->get($cart->getId());
163+
$this->couponManagement->set($cart->getId(), 'once');
164+
$this->cartManagement->placeOrder($cart->getId());
165+
$consumer->process(1);
166+
}
127167
}

0 commit comments

Comments
 (0)