-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from adobe-commerce-tier-4/Tier4-Kings-PR-11-…
…22-2024 [Support Tier-4-Kings glo02433] 11.22.2024 Regular delivery of bugfixes and improvements
- Loading branch information
Showing
4 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...log/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Indexer\Product\Full as FullProductIndexer; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\CatalogInventory\Model\Stock\StockItemRepository; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku; | ||
use Magento\InventoryIndexer\Indexer\InventoryIndexer; | ||
|
||
class StockItemRepositoryPlugin | ||
{ | ||
|
||
/** | ||
* @param FullProductIndexer $fullProductIndexer | ||
* @param InventoryIndexer $inventoryIndexer | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param GetSourceItemsBySku $getSourceItemsBySku | ||
*/ | ||
public function __construct( | ||
private FullProductIndexer $fullProductIndexer, | ||
private InventoryIndexer $inventoryIndexer, | ||
private ProductRepositoryInterface $productRepository, | ||
private getSourceItemsBySku $getSourceItemsBySku | ||
) { | ||
} | ||
|
||
/** | ||
* Complex reindex after product stock item has been saved. | ||
* | ||
* @param StockItemRepository $subject | ||
* @param StockItemInterface $stockItem | ||
* @return StockItemInterface | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function afterSave(StockItemRepository $subject, StockItemInterface $stockItem): StockItemInterface | ||
{ | ||
$product = $this->productRepository->getById($stockItem->getProductId()); | ||
$this->fullProductIndexer->executeRow($product->getId()); | ||
$sourceItems = $this->getSourceItemsBySku->execute($product->getSku()); | ||
$sourceItemIds = []; | ||
|
||
foreach ($sourceItems as $sourceItem) { | ||
$sourceItemIds[] = $sourceItem->getId(); | ||
} | ||
$this->inventoryIndexer->executeList($sourceItemIds); | ||
return $stockItem; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Test\Unit\Plugin\CatalogInventory\Model\Stock\StockItemRepository; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Indexer\Product\Full as FullProductIndexer; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\CatalogInventory\Model\Stock\StockItemRepository; | ||
use Magento\Inventory\Model\SourceItem; | ||
use Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku; | ||
use Magento\InventoryIndexer\Indexer\InventoryIndexer; | ||
use Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class StockItemRepositoryPluginTest extends TestCase | ||
{ | ||
/** @var FullProductIndexer|MockObject */ | ||
private $fullProductIndexer; | ||
|
||
/** @var InventoryIndexer|MockObject */ | ||
private $inventoryIndexer; | ||
|
||
/** @var ProductRepositoryInterface|MockObject */ | ||
private $productRepository; | ||
|
||
/** @var GetSourceItemsBySku|MockObject */ | ||
private $getSourceItemsBySku; | ||
|
||
/** @var StockItemRepositoryPlugin */ | ||
private $plugin; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->fullProductIndexer = $this->createMock(FullProductIndexer::class); | ||
$this->inventoryIndexer = $this->createMock(InventoryIndexer::class); | ||
$this->productRepository = $this->createMock(ProductRepositoryInterface::class); | ||
$this->getSourceItemsBySku = $this->createMock(GetSourceItemsBySku::class); | ||
|
||
$this->plugin = new StockItemRepositoryPlugin( | ||
$this->fullProductIndexer, | ||
$this->inventoryIndexer, | ||
$this->productRepository, | ||
$this->getSourceItemsBySku | ||
); | ||
} | ||
|
||
public function testAfterSave(): void | ||
{ | ||
$productId = 123; | ||
$sku = 'test-sku'; | ||
$sourceItemId = 456; | ||
|
||
$stockItem = $this->createMock(StockItemInterface::class); | ||
$stockItem->method('getProductId')->willReturn($productId); | ||
|
||
$product = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class); | ||
$product->method('getId')->willReturn($productId); | ||
$product->method('getSku')->willReturn($sku); | ||
$this->productRepository->method('getById')->with($productId)->willReturn($product); | ||
|
||
$sourceItem = $this->createMock(SourceItem::class); | ||
$sourceItem->method('getId')->willReturn($sourceItemId); | ||
$this->getSourceItemsBySku->method('execute')->with($sku)->willReturn([$sourceItem]); | ||
|
||
$this->fullProductIndexer->expects($this->once())->method('executeRow')->with($productId); | ||
$this->inventoryIndexer->expects($this->once())->method('executeList')->with([$sourceItemId]); | ||
|
||
$result = $this->plugin->afterSave( | ||
$this->createMock(StockItemRepository::class), | ||
$stockItem | ||
); | ||
|
||
$this->assertSame($stockItem, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* Copyright 2020 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Catalog\Model\ResourceModel\Product"> | ||
<plugin name="create_source_items" type="Magento\InventoryCatalog\Plugin\Catalog\Model\ResourceModel\Product\CreateSourceItemsPlugin"/> | ||
</type> | ||
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository"> | ||
<plugin name="reindex_after_save_product_stock_item" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin"/> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* Copyright 2020 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Catalog\Model\ResourceModel\Product"> | ||
<plugin name="create_source_items" type="Magento\InventoryCatalog\Plugin\Catalog\Model\ResourceModel\Product\CreateSourceItemsPlugin"/> | ||
</type> | ||
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository"> | ||
<plugin name="reindex_after_save_product_stock_item" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Model\Stock\StockItemRepository\StockItemRepositoryPlugin"/> | ||
</type> | ||
</config> |