Skip to content

Commit 4745100

Browse files
Merge pull request #8965 from adobe-commerce-tier-4/Tier4-Kings-PR-05-27-2024
[Support Tier-4-Kings glo02433] 05.27.2024 Regular delivery of bugfixes and improvements
2 parents bb798fe + 03d7766 commit 4745100

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\SalesRule\Plugin;
20+
21+
use Closure;
22+
use Magento\Framework\Exception\NoSuchEntityException;
23+
use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages;
24+
use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault;
25+
use Throwable;
26+
use Magento\Quote\Api\CartRepositoryInterface;
27+
28+
/**
29+
* Increments number of coupon usages before placing order
30+
*/
31+
class CouponUsagesIncrementMultishipping
32+
{
33+
34+
/**
35+
* @var UpdateCouponUsages
36+
*/
37+
private UpdateCouponUsages $updateCouponUsages;
38+
39+
/**
40+
* @var CartRepositoryInterface
41+
*/
42+
private CartRepositoryInterface $cartRepositoryInterface;
43+
44+
/**
45+
* @param UpdateCouponUsages $updateCouponUsages
46+
* @param CartRepositoryInterface $cartRepositoryInterface
47+
*/
48+
public function __construct(
49+
UpdateCouponUsages $updateCouponUsages,
50+
CartRepositoryInterface $cartRepositoryInterface
51+
) {
52+
$this->updateCouponUsages = $updateCouponUsages;
53+
$this->cartRepositoryInterface = $cartRepositoryInterface;
54+
}
55+
56+
/**
57+
* Increments number of coupon usages before placing order
58+
*
59+
* @param PlaceOrderDefault $subject
60+
* @param Closure $proceed
61+
* @param array $order
62+
* @return array
63+
* @throws NoSuchEntityException|Throwable
64+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
65+
*/
66+
public function aroundPlace(PlaceOrderDefault $subject, Closure $proceed, array $order): array
67+
{
68+
$quoteId = $order[0]->getQuoteId();
69+
$quote = $this->cartRepositoryInterface->get($quoteId);
70+
/* if coupon code has been canceled then need to notify the customer */
71+
if (!$quote->getCouponCode() && $quote->dataHasChangedFor('coupon_code')) {
72+
throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
73+
}
74+
75+
$this->updateCouponUsages->execute($quote, true);
76+
try {
77+
return $proceed($order);
78+
} catch (Throwable $e) {
79+
$this->updateCouponUsages->execute($quote, false);
80+
throw $e;
81+
}
82+
}
83+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\SalesRule\Test\Unit\Plugin;
20+
21+
use Magento\Quote\Model\Quote;
22+
use Magento\Quote\Api\CartRepositoryInterface;
23+
use PHPUnit\Framework\MockObject\MockObject;
24+
use PHPUnit\Framework\TestCase;
25+
use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages;
26+
use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault;
27+
use Magento\Sales\Model\Order;
28+
use Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping;
29+
30+
class CouponUsageIncreamentForMultishippingTest extends TestCase
31+
{
32+
/**
33+
* @var PlaceOrderDefault|MockObject
34+
*/
35+
private $subjectMock;
36+
37+
/**
38+
* @var UpdateCouponUsages|MockObject
39+
*/
40+
private $updateCouponUsagesMock;
41+
42+
/**
43+
* @var CartRepositoryInterface|MockObject
44+
*/
45+
private $cartRepositoryInterfaceMock;
46+
47+
/**
48+
* @var Order[]|MockObject
49+
*/
50+
private $orderMock;
51+
52+
/**
53+
* @var CouponUsagesIncrementMultishipping
54+
*/
55+
private $plugin;
56+
57+
/**
58+
* Set Up
59+
*/
60+
protected function setUp(): void
61+
{
62+
$this->subjectMock = $this->getMockBuilder(PlaceOrderDefault::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$this->updateCouponUsagesMock = $this->getMockBuilder(UpdateCouponUsages::class)
66+
->disableOriginalConstructor()
67+
->onlyMethods(['execute'])
68+
->getMock();
69+
$this->cartRepositoryInterfaceMock = $this->getMockBuilder(CartRepositoryInterface::class)
70+
->disableOriginalConstructor()
71+
->getMock();
72+
$this->orderMock = $this->getMockBuilder(Order::class)
73+
->onlyMethods(['getQuoteId'])
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->plugin = new CouponUsagesIncrementMultishipping(
77+
$this->updateCouponUsagesMock,
78+
$this->cartRepositoryInterfaceMock
79+
);
80+
}
81+
/**
82+
* Testing Increments number of coupon usages before placing order
83+
*/
84+
public function testAroundPlace()
85+
{
86+
$couponCode = 'coupon code';
87+
$proceed = function ($orderMock) {
88+
return $orderMock;
89+
};
90+
/** @var Quote|MockObject $quote */
91+
$quoteMock = $this->getMockBuilder(Quote::class)
92+
->disableOriginalConstructor()
93+
->addMethods(['getCouponCode'])
94+
->onlyMethods(['dataHasChangedFor'])
95+
->getMock();
96+
$this->orderMock->expects($this->once())->method('getQuoteId')
97+
->willReturn(1);
98+
99+
$this->cartRepositoryInterfaceMock->expects($this->once())->method('get')->with(1)->willReturn($quoteMock);
100+
$quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode);
101+
$quoteMock->expects($this->any())->method('dataHasChangedFor')->with($couponCode)->willReturn(true);
102+
$this->updateCouponUsagesMock
103+
->expects($this->once())
104+
->method('execute');
105+
$this->assertSame(
106+
[$this->orderMock],
107+
$this->plugin->aroundPlace($this->subjectMock, $proceed, [$this->orderMock])
108+
);
109+
}
110+
}

app/code/Magento/SalesRule/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"magento/module-captcha": "*",
2828
"magento/module-checkout": "*",
2929
"magento/module-authorization": "*",
30-
"magento/module-asynchronous-operations": "*"
30+
"magento/module-asynchronous-operations": "*",
31+
"magento/module-multishipping": "*"
3132
},
3233
"suggest": {
3334
"magento/module-sales-rule-sample-data": "*"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
<type name="\Magento\Quote\Model\QuoteManagement">
196196
<plugin name="coupon_uses_increment_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrement" sortOrder="20"/>
197197
</type>
198+
<type name="\Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault">
199+
<plugin name="coupon_uses_increment_multishipping_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping" sortOrder="30"/>
200+
</type>
198201
<preference
199202
for="Magento\SalesRule\Model\Spi\CodeLimitManagerInterface"
200203
type="Magento\SalesRule\Model\Coupon\CodeLimitManager" />

0 commit comments

Comments
 (0)