|
| 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 | +} |
0 commit comments