Skip to content

Commit eee42cf

Browse files
committed
MC-42443: Row Sub Total is not updated with new price when Price is updated through API
- Added the updateQuote plugin to update the quote items when product tier price is updated from rest API.
1 parent 39e1300 commit eee42cf

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
* @param TierPriceStorageInterface $subject
50+
* @param $result
51+
* @param $prices
52+
* @return array
53+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
54+
*/
55+
public function afterUpdate(
56+
TierPriceStorageInterface $subject,
57+
$result,
58+
$prices
59+
): array {
60+
$this->resource->markQuotesRecollect($this->retrieveAffectedProductIdsForPrices($prices));
61+
return $result;
62+
63+
}
64+
65+
/**
66+
* Retrieve affected product IDs for prices.
67+
*
68+
* @param TierPriceInterface[] $prices
69+
* @return array
70+
*/
71+
private function retrieveAffectedProductIdsForPrices(array $prices): array
72+
{
73+
$skus = array_unique(
74+
array_map(
75+
function (TierPriceInterface $price) {
76+
return $price->getSku();
77+
},
78+
$prices
79+
)
80+
);
81+
82+
return $this->retrieveAffectedIds($skus);
83+
}
84+
85+
/**
86+
* Retrieve affected product IDs.
87+
*
88+
* @param array $skus
89+
* @return array
90+
*/
91+
private function retrieveAffectedIds(array $skus): array
92+
{
93+
$affectedIds = [];
94+
95+
foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $productId) {
96+
$affectedIds[] = array_keys($productId);
97+
}
98+
99+
return array_unique(array_merge([], ...$affectedIds));
100+
}
101+
}

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>

0 commit comments

Comments
 (0)