Skip to content

Commit

Permalink
Merge pull request #8965 from adobe-commerce-tier-4/Tier4-Kings-PR-05…
Browse files Browse the repository at this point in the history
…-27-2024

[Support Tier-4-Kings glo02433] 05.27.2024 Regular delivery of bugfixes and improvements
  • Loading branch information
2 parents bb798fe + 03d7766 commit 4745100
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 1 deletion.
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;
}
}
}
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])
);
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/SalesRule/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"magento/module-captcha": "*",
"magento/module-checkout": "*",
"magento/module-authorization": "*",
"magento/module-asynchronous-operations": "*"
"magento/module-asynchronous-operations": "*",
"magento/module-multishipping": "*"
},
"suggest": {
"magento/module-sales-rule-sample-data": "*"
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/SalesRule/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
<type name="\Magento\Quote\Model\QuoteManagement">
<plugin name="coupon_uses_increment_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrement" sortOrder="20"/>
</type>
<type name="\Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault">
<plugin name="coupon_uses_increment_multishipping_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping" sortOrder="30"/>
</type>
<preference
for="Magento\SalesRule\Model\Spi\CodeLimitManagerInterface"
type="Magento\SalesRule\Model\Coupon\CodeLimitManager" />
Expand Down

0 comments on commit 4745100

Please sign in to comment.