Skip to content

Commit b6df4eb

Browse files
committed
Merge branch 'MC-42443' of https://github.com/magento-l3/magento2ce into L3-PR-20210830
2 parents e0da248 + a815c38 commit b6df4eb

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Model\Product\Plugin;
8+
9+
use Magento\Catalog\Api\Data\TierPriceInterface;
10+
use Magento\Catalog\Api\TierPriceStorageInterface;
11+
use Magento\Quote\Model\ResourceModel\Quote;
12+
use Magento\Catalog\Model\ProductIdLocatorInterface;
13+
14+
/**
15+
* UpdateQuote Plugin Class
16+
*/
17+
class UpdateQuote
18+
{
19+
20+
/**
21+
* Quote Resource
22+
*
23+
* @var Quote
24+
*/
25+
private $resource;
26+
27+
/**
28+
* Product ID locator.
29+
*
30+
* @var ProductIdLocatorInterface
31+
*/
32+
private $productIdLocator;
33+
34+
/**
35+
* Construct Method for updateQuote Plugin
36+
*
37+
* @param Quote $resource
38+
* @param ProductIdLocatorInterface $productIdLocator
39+
*/
40+
public function __construct(
41+
\Magento\Quote\Model\ResourceModel\Quote $resource,
42+
\Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator
43+
) {
44+
$this->resource = $resource;
45+
$this->productIdLocator = $productIdLocator;
46+
}
47+
48+
/**
49+
* Update the quote trigger_recollect column is 1 when product price is changed through API.
50+
*
51+
* @param TierPriceStorageInterface $subject
52+
* @param $result
53+
* @param $prices
54+
* @return array
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function afterUpdate(
58+
TierPriceStorageInterface $subject,
59+
$result,
60+
$prices
61+
): array {
62+
$this->resource->markQuotesRecollect($this->retrieveAffectedProductIdsForPrices($prices));
63+
return $result;
64+
65+
}
66+
67+
/**
68+
* Retrieve affected product IDs for prices.
69+
*
70+
* @param TierPriceInterface[] $prices
71+
* @return array
72+
*/
73+
private function retrieveAffectedProductIdsForPrices(array $prices): array
74+
{
75+
$skus = array_unique(
76+
array_map(
77+
function (TierPriceInterface $price) {
78+
return $price->getSku();
79+
},
80+
$prices
81+
)
82+
);
83+
84+
return $this->retrieveAffectedIds($skus);
85+
}
86+
87+
/**
88+
* Retrieve affected product IDs.
89+
*
90+
* @param array $skus
91+
* @return array
92+
*/
93+
private function retrieveAffectedIds(array $skus): array
94+
{
95+
$affectedIds = [];
96+
97+
foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $productId) {
98+
$affectedIds[] = array_keys($productId);
99+
}
100+
101+
return array_unique(array_merge([], ...$affectedIds));
102+
}
103+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<plugin name="clean_quote_items_after_product_delete" type="Magento\Quote\Model\Product\Plugin\RemoveQuoteItems"/>
100100
<plugin name="update_quote_items_after_product_save" type="Magento\Quote\Model\Product\Plugin\UpdateQuoteItems"/>
101101
</type>
102+
<type name="Magento\Catalog\Api\TierPriceStorageInterface">
103+
<plugin name="update_quote_items_after_tier_prices_update" type="Magento\Quote\Model\Product\Plugin\UpdateQuote"/>
104+
</type>
102105
<type name="Magento\Catalog\Model\Product\Action">
103106
<plugin name="quoteProductMassChange" type="Magento\Quote\Model\Product\Plugin\MarkQuotesRecollectMassDisabled"/>
104107
</type>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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\Quote\Model\Product\Plugin;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
14+
use Magento\Catalog\Api\TierPriceStorageInterface;
15+
use Magento\Catalog\Api\Data\TierPriceInterfaceFactory;
16+
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Tests for update quote plugin.
21+
*/
22+
class UpdateQuoteTest extends TestCase
23+
{
24+
/**
25+
* @var GetQuoteByReservedOrderId
26+
*/
27+
private $getQuoteByReservedOrderId;
28+
29+
/** @var ProductRepositoryInterface */
30+
private $productRepository;
31+
32+
/** @var CartItemInterfaceFactory */
33+
private $itemFactory;
34+
35+
/**
36+
* @var TierPriceStorageInterface
37+
*/
38+
private $tierPriceStorage;
39+
40+
/**
41+
* @var TierPriceInterfaceFactory
42+
*/
43+
private $tierPriceFactory;
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
protected function setUp(): void
49+
{
50+
parent::setUp();
51+
52+
$objectManager = Bootstrap::getObjectManager();
53+
$this->getQuoteByReservedOrderId = $objectManager->get(GetQuoteByReservedOrderId::class);
54+
$this->tierPriceStorage = $objectManager->get(TierPriceStorageInterface::class);
55+
$this->tierPriceFactory = $objectManager->get(TierPriceInterfaceFactory::class);
56+
$this->itemFactory = $objectManager->get(CartItemInterfaceFactory::class);
57+
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
58+
}
59+
60+
/**
61+
* Test to update the column trigger_recollect is 1 from quote table.
62+
*
63+
* @magentoDataFixture Magento/Sales/_files/quote.php
64+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
65+
* @return void
66+
*/
67+
public function testUpdateQuoteRecollectAfterChangeProductPrice(): void
68+
{
69+
$quoteId = 'test01';
70+
$productSku = 'simple';
71+
$quote = $this->getQuoteByReservedOrderId->execute($quoteId);
72+
73+
$quoteItem = $this->itemFactory->create();
74+
75+
$product = $this->productRepository->get($productSku);
76+
77+
$quoteItem->setProduct($product);
78+
$quoteItem->setProductId($product->getRowId());
79+
80+
$quote->addItem($quoteItem);
81+
$quote->setTriggerRecollect(0);
82+
$quote->save();
83+
84+
$this->assertNotNull($quote);
85+
$this->assertFalse((bool)$quote->getTriggerRecollect());
86+
87+
$tierPrice = $this->tierPriceFactory->create();
88+
$tierPrice->setPrice($product->getPrice());
89+
$tierPrice->setPriceType('fixed');
90+
$tierPrice->setWebsiteId(0);
91+
$tierPrice->setSku($product->getSku());
92+
$tierPrice->setCustomerGroup('ALL GROUPS');
93+
$tierPrice->setQuantity(1);
94+
95+
$this->tierPriceStorage->update([$tierPrice]);
96+
97+
$quote = $this->getQuoteByReservedOrderId->execute($quoteId);
98+
99+
$this->assertNotEmpty($quote->getTriggerRecollect());
100+
$this->assertTrue((bool)$quote->getTriggerRecollect());
101+
}
102+
}

0 commit comments

Comments
 (0)