-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
app/code/Magento/SalesRule/Plugin/CouponUsagesIncrementMultishipping.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SalesRule\Plugin; | ||
|
||
use Closure; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages; | ||
use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault; | ||
use Throwable; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
|
||
/** | ||
* Increments number of coupon usages before placing order | ||
*/ | ||
class CouponUsagesIncrementMultishipping | ||
{ | ||
|
||
/** | ||
* @var UpdateCouponUsages | ||
*/ | ||
private UpdateCouponUsages $updateCouponUsages; | ||
|
||
/** | ||
* @var CartRepositoryInterface | ||
*/ | ||
private CartRepositoryInterface $cartRepositoryInterface; | ||
|
||
/** | ||
* @param UpdateCouponUsages $updateCouponUsages | ||
* @param CartRepositoryInterface $cartRepositoryInterface | ||
*/ | ||
public function __construct( | ||
UpdateCouponUsages $updateCouponUsages, | ||
CartRepositoryInterface $cartRepositoryInterface | ||
) { | ||
$this->updateCouponUsages = $updateCouponUsages; | ||
$this->cartRepositoryInterface = $cartRepositoryInterface; | ||
} | ||
|
||
/** | ||
* Increments number of coupon usages before placing order | ||
* | ||
* @param PlaceOrderDefault $subject | ||
* @param Closure $proceed | ||
* @param array $order | ||
* @return array | ||
* @throws NoSuchEntityException|Throwable | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function aroundPlace(PlaceOrderDefault $subject, Closure $proceed, array $order): array | ||
{ | ||
$quoteId = $order[0]->getQuoteId(); | ||
$quote = $this->cartRepositoryInterface->get($quoteId); | ||
/* if coupon code has been canceled then need to notify the customer */ | ||
if (!$quote->getCouponCode() && $quote->dataHasChangedFor('coupon_code')) { | ||
throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again.")); | ||
} | ||
|
||
$this->updateCouponUsages->execute($quote, true); | ||
try { | ||
return $proceed($order); | ||
} catch (Throwable $e) { | ||
$this->updateCouponUsages->execute($quote, false); | ||
throw $e; | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
app/code/Magento/SalesRule/Test/Unit/Plugin/CouponUsageIncreamentForMultishippingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SalesRule\Test\Unit\Plugin; | ||
|
||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages; | ||
use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault; | ||
use Magento\Sales\Model\Order; | ||
use Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping; | ||
|
||
class CouponUsageIncreamentForMultishippingTest extends TestCase | ||
{ | ||
/** | ||
* @var PlaceOrderDefault|MockObject | ||
*/ | ||
private $subjectMock; | ||
|
||
/** | ||
* @var UpdateCouponUsages|MockObject | ||
*/ | ||
private $updateCouponUsagesMock; | ||
|
||
/** | ||
* @var CartRepositoryInterface|MockObject | ||
*/ | ||
private $cartRepositoryInterfaceMock; | ||
|
||
/** | ||
* @var Order[]|MockObject | ||
*/ | ||
private $orderMock; | ||
|
||
/** | ||
* @var CouponUsagesIncrementMultishipping | ||
*/ | ||
private $plugin; | ||
|
||
/** | ||
* Set Up | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->subjectMock = $this->getMockBuilder(PlaceOrderDefault::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->updateCouponUsagesMock = $this->getMockBuilder(UpdateCouponUsages::class) | ||
->disableOriginalConstructor() | ||
->onlyMethods(['execute']) | ||
->getMock(); | ||
$this->cartRepositoryInterfaceMock = $this->getMockBuilder(CartRepositoryInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->orderMock = $this->getMockBuilder(Order::class) | ||
->onlyMethods(['getQuoteId']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->plugin = new CouponUsagesIncrementMultishipping( | ||
$this->updateCouponUsagesMock, | ||
$this->cartRepositoryInterfaceMock | ||
); | ||
} | ||
/** | ||
* Testing Increments number of coupon usages before placing order | ||
*/ | ||
public function testAroundPlace() | ||
{ | ||
$couponCode = 'coupon code'; | ||
$proceed = function ($orderMock) { | ||
return $orderMock; | ||
}; | ||
/** @var Quote|MockObject $quote */ | ||
$quoteMock = $this->getMockBuilder(Quote::class) | ||
->disableOriginalConstructor() | ||
->addMethods(['getCouponCode']) | ||
->onlyMethods(['dataHasChangedFor']) | ||
->getMock(); | ||
$this->orderMock->expects($this->once())->method('getQuoteId') | ||
->willReturn(1); | ||
|
||
$this->cartRepositoryInterfaceMock->expects($this->once())->method('get')->with(1)->willReturn($quoteMock); | ||
$quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); | ||
$quoteMock->expects($this->any())->method('dataHasChangedFor')->with($couponCode)->willReturn(true); | ||
$this->updateCouponUsagesMock | ||
->expects($this->once()) | ||
->method('execute'); | ||
$this->assertSame( | ||
[$this->orderMock], | ||
$this->plugin->aroundPlace($this->subjectMock, $proceed, [$this->orderMock]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters