Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acsd 47527 #170

Open
wants to merge 3 commits into
base: v1.2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Helper\Stock;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
Expand Down Expand Up @@ -40,22 +42,30 @@ class AdaptAssignStatusToProductPlugin
*/
private $stockResolver;

/**
* @var GetStockItemDataInterface
*/
private $getStockItemData;

/**
* @param Configurable $configurable
* @param AreProductsSalableInterface $areProductsSalable
* @param StoreManagerInterface $storeManager
* @param StockResolverInterface $stockResolver
* @param GetStockItemDataInterface $getStockItemData
*/
public function __construct(
Configurable $configurable,
AreProductsSalableInterface $areProductsSalable,
StoreManagerInterface $storeManager,
StockResolverInterface $stockResolver
StockResolverInterface $stockResolver,
GetStockItemDataInterface $getStockItemData
) {
$this->configurable = $configurable;
$this->areProductsSalable = $areProductsSalable;
$this->storeManager = $storeManager;
$this->stockResolver = $stockResolver;
$this->getStockItemData = $getStockItemData;
}

/**
Expand All @@ -76,6 +86,17 @@ public function beforeAssignStatusToProduct(
if ($product->getTypeId() === Configurable::TYPE_CODE) {
$website = $this->storeManager->getWebsite();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $website->getCode());
$stockId = $stock->getStockId();
try {
$stockItemData = $this->getStockItemData->execute($product->getSku(), $stockId);
} catch (NoSuchEntityException $exception) {
$stockItemData = null;
}
if (null !== $stockItemData) {
if (!((bool) $stockItemData[GetStockItemDataInterface::IS_SALABLE])) {
return [$product, $status];
}
}
$options = $this->configurable->getConfigurableOptions($product);
$status = 0;
$skus = [[]];
Expand Down
25 changes: 22 additions & 3 deletions InventoryConfigurableProductIndexer/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
namespace Magento\InventoryConfigurableProductIndexer\Indexer;

use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryIndexer\Indexer\IndexStructure;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/magento-sparta/inventory/pull/168/files#diff-46d1eab5b946d8db6bc8da2f9f205df1b929013e6df65c0edd95b1af0f44de3bL16: you are missing the part that remove some "use".. because of this, some "use" is included twice:

  • use Magento\InventoryIndexer\Indexer\IndexStructure;
  • use Magento\InventoryIndexer\Indexer\InventoryIndexer;
  • use Magento\Framework\EntityManager\MetadataPool;
  • use Magento\Catalog\Api\Data\ProductInterface;

use Magento\InventoryMultiDimensionalIndexerApi\Model\Alias;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameBuilder;
use Magento\InventoryIndexer\Indexer\SelectBuilderInterface;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameResolverInterface;
use Magento\InventoryIndexer\Indexer\IndexStructure;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryIndexer\Indexer\SelectBuilderInterface;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already included at row 20

use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Api\Data\ProductInterface;

class SelectBuilder
class SelectBuilder implements SelectBuilderInterface
{
/**
* @var ResourceConnection
Expand All @@ -39,23 +46,30 @@ class SelectBuilder
* @var MetadataPool
*/
private $metadataPool;
/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* @param ResourceConnection $resourceConnection
* @param IndexNameBuilder $indexNameBuilder
* @param IndexNameResolverInterface $indexNameResolver
* @param MetadataPool $metadataPool
* @param DefaultStockProviderInterface $defaultStockProvider
*/
public function __construct(
ResourceConnection $resourceConnection,
IndexNameBuilder $indexNameBuilder,
IndexNameResolverInterface $indexNameResolver,
MetadataPool $metadataPool
MetadataPool $metadataPool,
DefaultStockProviderInterface $defaultStockProvider
) {
$this->resourceConnection = $resourceConnection;
$this->indexNameBuilder = $indexNameBuilder;
$this->indexNameResolver = $indexNameResolver;
$this->metadataPool = $metadataPool;
$this->defaultStockProvider = $defaultStockProvider;
}

/**
Expand Down Expand Up @@ -86,7 +100,7 @@ public function execute(int $stockId): Select
[
IndexStructure::SKU => 'parent_product_entity.sku',
IndexStructure::QUANTITY => 'SUM(stock.quantity)',
IndexStructure::IS_SALABLE => 'MAX(stock.is_salable)',
IndexStructure::IS_SALABLE => 'IF(inventory_stock_item.is_in_stock = 0, 0, MAX(stock.is_salable))',
]
)->joinInner(
['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
Expand All @@ -100,6 +114,11 @@ public function execute(int $stockId): Select
['parent_product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
'parent_product_entity.' . $linkField . ' = parent_link.parent_id',
[]
)->joinLeft(
['inventory_stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item')],
'inventory_stock_item.product_id = parent_product_entity.entity_id'
. ' AND inventory_stock_item.stock_id = ' . $this->defaultStockProvider->getId(),
[]
)
->group(['parent_product_entity.sku']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@

namespace Magento\InventoryConfigurableProductIndexer\Indexer\SourceItem;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\InventoryMultiDimensionalIndexerApi\Model\Alias;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexHandlerInterface;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameBuilder;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexStructureInterface;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use ArrayIterator;

/**
* Configurable product source item indexer
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) Will be removed after deleting DefaultStockProviderInterface
*/
class SourceItemIndexer
{
/**
* Default batch size
*/
private const BATCH_SIZE = 100;

/**
* @var ResourceConnection
*/
Expand Down Expand Up @@ -52,6 +65,16 @@ class SourceItemIndexer
*/
private $defaultStockProvider;

/**
* @var int
*/
private $batchSize;

/**
* @var Batch
*/
private $batch;

/**
* @param ResourceConnection $resourceConnection
* @param IndexNameBuilder $indexNameBuilder
Expand All @@ -60,6 +83,8 @@ class SourceItemIndexer
* @param IndexDataBySkuListProvider $indexDataBySkuListProvider
* @param SiblingSkuListInStockProvider $siblingSkuListInStockProvider
* @param DefaultStockProviderInterface $defaultStockProvider
* @param Batch|null $batch
* @param int|null $batchSize
*/
public function __construct(
ResourceConnection $resourceConnection,
Expand All @@ -68,7 +93,9 @@ public function __construct(
IndexStructureInterface $indexStructure,
IndexDataBySkuListProvider $indexDataBySkuListProvider,
SiblingSkuListInStockProvider $siblingSkuListInStockProvider,
DefaultStockProviderInterface $defaultStockProvider
DefaultStockProviderInterface $defaultStockProvider,
?Batch $batch = null,
?int $batchSize = null
) {
$this->resourceConnection = $resourceConnection;
$this->indexNameBuilder = $indexNameBuilder;
Expand All @@ -77,9 +104,13 @@ public function __construct(
$this->indexStructure = $indexStructure;
$this->siblingSkuListInStockProvider = $siblingSkuListInStockProvider;
$this->defaultStockProvider = $defaultStockProvider;
$this->batch = $batch ?: ObjectManager::getInstance()->get(Batch::class);
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
}

/**
* Executes index by list of stock ids
*
* @param array $sourceItemIds
*/
public function executeList(array $sourceItemIds)
Expand All @@ -106,17 +137,20 @@ public function executeList(array $sourceItemIds)

$indexData = $this->indexDataBySkuListProvider->execute($stockId, $skuList);

$this->indexHandler->cleanIndex(
$mainIndexName,
$indexData,
ResourceConnection::DEFAULT_CONNECTION
);

$this->indexHandler->saveIndex(
$mainIndexName,
$indexData,
ResourceConnection::DEFAULT_CONNECTION
);
foreach ($this->batch->getItems($indexData, $this->batchSize) as $batchData) {
$batchIndexData = new ArrayIterator($batchData);
$this->indexHandler->cleanIndex(
$mainIndexName,
$batchIndexData,
ResourceConnection::DEFAULT_CONNECTION
);

$this->indexHandler->saveIndex(
$mainIndexName,
$batchIndexData,
ResourceConnection::DEFAULT_CONNECTION
);
}
}
}
}
51 changes: 39 additions & 12 deletions InventoryConfigurableProductIndexer/Indexer/Stock/StockIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryIndexer\Indexer\Stock\GetAllStockIds;
Expand All @@ -19,6 +20,7 @@
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameBuilder;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexStructureInterface;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexTableSwitcherInterface;
use ArrayIterator;

/**
* Configurable product stock indexer class
Expand All @@ -27,6 +29,11 @@
*/
class StockIndexer
{
/**
* Default batch size
*/
private const BATCH_SIZE = 100;

/**
* @var GetAllStockIds
*/
Expand Down Expand Up @@ -67,6 +74,16 @@ class StockIndexer
*/
private $prepareIndexDataForClearingIndex;

/**
* @var int
*/
private $batchSize;

/**
* @var Batch
*/
private $batch;

/**
* $indexStructure is reserved name for construct variable in index internal mechanism
*
Expand All @@ -78,6 +95,9 @@ class StockIndexer
* @param IndexTableSwitcherInterface $indexTableSwitcher
* @param DefaultStockProviderInterface $defaultStockProvider
* @param PrepareIndexDataForClearingIndex|null $prepareIndexDataForClearingIndex
* @param Batch|null $batch
* @param int|null $batchSize
* @SuppressWarnings(PHPMD.ExcessiveParameterList) All parameters are needed for backward compatibility
*/
public function __construct(
GetAllStockIds $getAllStockIds,
Expand All @@ -87,7 +107,9 @@ public function __construct(
IndexDataByStockIdProvider $indexDataByStockIdProvider,
IndexTableSwitcherInterface $indexTableSwitcher,
DefaultStockProviderInterface $defaultStockProvider,
PrepareIndexDataForClearingIndex $prepareIndexDataForClearingIndex = null
PrepareIndexDataForClearingIndex $prepareIndexDataForClearingIndex = null,
?Batch $batch = null,
?int $batchSize = null
) {
$this->getAllStockIds = $getAllStockIds;
$this->indexStructure = $indexStructure;
Expand All @@ -98,6 +120,8 @@ public function __construct(
$this->defaultStockProvider = $defaultStockProvider;
$this->prepareIndexDataForClearingIndex = $prepareIndexDataForClearingIndex ?: ObjectManager::getInstance()
->get(PrepareIndexDataForClearingIndex::class);
$this->batch = $batch ?: ObjectManager::getInstance()->get(Batch::class);
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
}

/**
Expand Down Expand Up @@ -150,17 +174,20 @@ public function executeList(array $stockIds): void

$indexData = $this->indexDataByStockIdProvider->execute((int)$stockId);

$this->indexHandler->cleanIndex(
$mainIndexName,
$this->prepareIndexDataForClearingIndex->execute($indexData),
ResourceConnection::DEFAULT_CONNECTION
);

$this->indexHandler->saveIndex(
$mainIndexName,
$indexData,
ResourceConnection::DEFAULT_CONNECTION
);
foreach ($this->batch->getItems($indexData, $this->batchSize) as $batchData) {
$batchIndexData = new ArrayIterator($batchData);
$this->indexHandler->cleanIndex(
$mainIndexName,
$this->prepareIndexDataForClearingIndex->execute($batchIndexData),
ResourceConnection::DEFAULT_CONNECTION
);

$this->indexHandler->saveIndex(
$mainIndexName,
$batchIndexData,
ResourceConnection::DEFAULT_CONNECTION
);
}
}
}
}
9 changes: 9 additions & 0 deletions InventoryConfigurableProductIndexer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@
<plugin name="configurable_product_full_index" type="Magento\InventoryConfigurableProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexFullPlugin"/>
<plugin name="configurable_product_index_list" type="Magento\InventoryConfigurableProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexListPlugin"/>
</type>
<type name="Magento\InventoryIndexer\Indexer\Stock\IndexDataProviderByStockId">
<arguments>
<argument name="selectBuilders" xsi:type="array">
<item name="configurable" xsi:type="object">Magento\InventoryConfigurableProductIndexer\Indexer\SelectBuilder</item>
</argument>
</arguments>
</type>
<type name="Magento\InventoryConfigurableProductIndexer\Indexer\SourceItem\SourceItemIndexer">
<arguments>
<argument name="indexHandler" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexHandler</argument>
<argument name="indexStructure" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexStructure</argument>
<argument name="batchSize" xsi:type="string">100</argument>
</arguments>
</type>
<type name="Magento\InventoryConfigurableProductIndexer\Indexer\Stock\StockIndexer">
<arguments>
<argument name="indexHandler" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexHandler</argument>
<argument name="indexStructure" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexStructure</argument>
<argument name="batchSize" xsi:type="string">100</argument>
</arguments>
</type>
<type name="Magento\InventoryConfigurableProductIndexer\Indexer\SourceItem\SiblingSkuListInStockProvider">
Expand Down
Loading