Skip to content

Commit b652c7f

Browse files
author
Stanislav Idolov
authored
Merge branch '1.1-develop' into MC-35306
2 parents fcb5613 + cd363c5 commit b652c7f

File tree

25 files changed

+769
-6
lines changed

25 files changed

+769
-6
lines changed

InventoryAdminUi/Test/Mftf/Test/StorefrontCreateOrderAllQuantityConfigurableProductCustomStock.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@
9393
<actionGroup ref="GoToCheckoutFromMinicartActionGroup" stepKey="navigateToCheckoutPage"/>
9494
<click selector="{{CheckoutShippingMethodsSection.next}}" stepKey="clickOnNextButton"/>
9595
<actionGroup ref="ClickPlaceOrderActionGroup" stepKey="clickOnPlaceOrder"/>
96-
<magentoCLI command="cache:flush" stepKey="flushCache"/>
96+
<magentoCLI command="queue:consumers:start inventory.reservations.updateSalabilityStatus" stepKey="startSalabilityUpdate" />
97+
<wait time="60" stepKey="waitForSalabilityUpdate"/>
98+
<magentoCLI command="cache:flush" stepKey="cleanCache"/>
99+
<wait time="30" stepKey="waitAfterCacheFlushed"/>
97100
<!--Verify, configurable product has 'out of stock' status.-->
98101
<amOnPage url="{{StorefrontProductPage.url($configurableProduct.custom_attributes[url_key]$)}}" stepKey="navigateToConfigurablePDP"/>
99102
<see selector="{{StorefrontProductInfoMainSection.productStockStatus}}" userInput="{{SourceStatusOutOfStock.value}}" stepKey="verifyProductStatus"/>

InventoryAdminUi/Test/Mftf/Test/StorefrontLoggedInCustomerCreateOrderAllOptionQuantityConfigurableProductCustomStock.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@
117117
<actionGroup ref="GoToCheckoutFromMinicartActionGroup" stepKey="navigateToCheckoutPage"/>
118118
<click selector="{{CheckoutShippingMethodsSection.next}}" stepKey="clickOnNextButton"/>
119119
<actionGroup ref="ClickPlaceOrderActionGroup" stepKey="clickOnPlaceOrder"/>
120-
<magentoCLI command="cache:flush" stepKey="flushCache"/>
120+
121+
<magentoCLI command="queue:consumers:start inventory.reservations.updateSalabilityStatus" stepKey="startSalabilityUpdate" />
122+
<wait time="60" stepKey="waitForSalabilityUpdate"/>
123+
<magentoCLI command="cache:flush" stepKey="cleanCache"/>
124+
<wait time="30" stepKey="waitAfterCacheFlushed"/>
125+
121126
<!-- Assert out of stock option is absent on product page -->
122127
<amOnPage url="{{StorefrontProductPage.url($$configurableProduct.custom_attributes[url_key]$$)}}" stepKey="navigateToConfigurablePDP"/>
123128
<waitForPageLoad stepKey="waitForConfigurablePDP"/>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\InventoryCache\Plugin\InventoryIndexer\Queue\Reservation\UpdateSalabilityStatus;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\InventoryCache\Model\FlushCacheByProductIds;
12+
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
13+
use Magento\InventoryIndexer\Model\Queue\UpdateIndexSalabilityStatus;
14+
15+
/**
16+
* Clean cache for corresponding products after stock status update.
17+
*/
18+
class CacheFlush
19+
{
20+
/**
21+
* @var FlushCacheByProductIds
22+
*/
23+
private $flushCacheByIds;
24+
25+
/**
26+
* @var GetProductIdsBySkusInterface
27+
*/
28+
private $getProductIdsBySkus;
29+
30+
/**
31+
* @param FlushCacheByProductIds $flushCacheByIds
32+
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
33+
*/
34+
public function __construct(
35+
FlushCacheByProductIds $flushCacheByIds,
36+
GetProductIdsBySkusInterface $getProductIdsBySkus
37+
) {
38+
$this->flushCacheByIds = $flushCacheByIds;
39+
$this->getProductIdsBySkus = $getProductIdsBySkus;
40+
}
41+
42+
/**
43+
* Flush cache after reindex.
44+
*
45+
* @param UpdateIndexSalabilityStatus $subject
46+
* @param array $skusAffected
47+
*
48+
* @return array
49+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
50+
*/
51+
public function afterExecute(UpdateIndexSalabilityStatus $subject, array $skusAffected)
52+
{
53+
if ($skus = array_keys($skusAffected)) {
54+
try {
55+
$this->flushCacheByIds->execute($this->getProductIdsBySkus->execute($skus));
56+
} catch (NoSuchEntityException $e) { // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
57+
// Do nothing.
58+
}
59+
}
60+
61+
return $skusAffected;
62+
}
63+
}

InventoryCache/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<argument name="productCacheTag" xsi:type="const">Magento\Catalog\Model\Product::CACHE_TAG</argument>
2323
</arguments>
2424
</type>
25+
<type name="Magento\InventoryIndexer\Model\Queue\UpdateIndexSalabilityStatus">
26+
<plugin name="invalidate_products_cache" type="Magento\InventoryCache\Plugin\InventoryIndexer\Queue\Reservation\UpdateSalabilityStatus\CacheFlush" />
27+
</type>
2528
</config>

InventoryCatalog/Test/Mftf/Data/QueueConsumerData.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
<data key="consumerName">inventory.reservations.cleanup</data>
2121
<data key="messageLimit">10</data>
2222
</entity>
23+
<entity name="AdminInventoryReservationsSalableStatusData">
24+
<data key="consumerName">inventory.reservations.updateSalabilityStatus</data>
25+
<data key="messageLimit">10</data>
26+
</entity>
2327
</entities>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\InventoryIndexer\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
12+
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
13+
use Psr\Log\LoggerInterface;
14+
15+
/**
16+
* Lightweight implementation for Storefront application.
17+
*/
18+
class IsProductSalable implements IsProductSalableInterface
19+
{
20+
/**
21+
* @var GetStockItemDataInterface
22+
*/
23+
private $getStockItemData;
24+
/**
25+
* @var LoggerInterface
26+
*/
27+
private $logger;
28+
29+
/**
30+
* @param GetStockItemDataInterface $getStockItemData
31+
* @param LoggerInterface $logger
32+
*/
33+
public function __construct(
34+
GetStockItemDataInterface $getStockItemData,
35+
LoggerInterface $logger
36+
) {
37+
$this->getStockItemData = $getStockItemData;
38+
$this->logger = $logger;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function execute(string $sku, int $stockId): bool
45+
{
46+
try {
47+
$stockItem = $this->getStockItemData->execute($sku, $stockId);
48+
$isSalable = (bool)($stockItem[GetStockItemDataInterface::IS_SALABLE] ?? false);
49+
} catch (LocalizedException $exception) {
50+
$this->logger->warning(
51+
sprintf(
52+
'Unable to fetch stock #%s data for SKU %s. Reason: %s',
53+
$stockId,
54+
$sku,
55+
$exception->getMessage()
56+
)
57+
);
58+
$isSalable = false;
59+
}
60+
61+
return $isSalable;
62+
}
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\InventoryIndexer\Model\Queue;
9+
10+
/**
11+
* Data object for reservations queue request.
12+
*/
13+
class ReservationData
14+
{
15+
/**
16+
* @var string[]
17+
*/
18+
private $skus;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $stockId;
24+
25+
/**
26+
* @param string[] $skus
27+
* @param int $stock
28+
*/
29+
public function __construct(array $skus, int $stock)
30+
{
31+
$this->skus = $skus;
32+
$this->stockId = $stock;
33+
}
34+
35+
/**
36+
* Retrieve products SKUs to process.
37+
*
38+
* @return string[]
39+
*/
40+
public function getSkus(): array
41+
{
42+
return $this->skus;
43+
}
44+
45+
/**
46+
* Retrieve stock id.
47+
*
48+
* @return int
49+
*/
50+
public function getStock(): int
51+
{
52+
return $this->stockId;
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\InventoryIndexer\Model\Queue;
9+
10+
use Magento\Framework\Exception\StateException;
11+
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
12+
use Magento\InventoryIndexer\Model\Queue\UpdateIndexSalabilityStatus\IndexProcessor;
13+
14+
/**
15+
* Recalculates index items salability status.
16+
*/
17+
class UpdateIndexSalabilityStatus
18+
{
19+
/**
20+
* @var DefaultStockProviderInterface
21+
*/
22+
private $defaultStockProvider;
23+
24+
/**
25+
* @var IndexProcessor
26+
*/
27+
private $indexProcessor;
28+
29+
/**
30+
* @param DefaultStockProviderInterface $defaultStockProvider
31+
* @param IndexProcessor $indexProcessor
32+
*/
33+
public function __construct(
34+
DefaultStockProviderInterface $defaultStockProvider,
35+
IndexProcessor $indexProcessor
36+
) {
37+
$this->defaultStockProvider = $defaultStockProvider;
38+
$this->indexProcessor = $indexProcessor;
39+
}
40+
41+
/**
42+
* Reindex items salability statuses.
43+
*
44+
* @param ReservationData $reservationData
45+
*
46+
* @return bool[] - ['sku' => bool]: list of SKUs with salability status changed.
47+
* @throws StateException
48+
*/
49+
public function execute(ReservationData $reservationData): array
50+
{
51+
$stockId = $reservationData->getStock();
52+
$dataForUpdate = [];
53+
if ($stockId !== $this->defaultStockProvider->getId() && $reservationData->getSkus()) {
54+
$dataForUpdate = $this->indexProcessor->execute($reservationData, $stockId);
55+
}
56+
57+
return $dataForUpdate;
58+
}
59+
}

0 commit comments

Comments
 (0)