Skip to content

Commit f1e7865

Browse files
committed
MDEE-40:[Commerce Export] Implement stock_item_status feed
- use product id instead of sku: changelog table can work only with integers
1 parent 240f5d9 commit f1e7865

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

InventoryDataExporter/Model/Provider/StockStatus.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ public function __construct(
6565
*/
6666
public function get(array $values): array
6767
{
68-
$skus = \array_column($values, 'sku');
68+
$productIds = \array_column($values, 'productId');
6969
$connection = $this->resourceConnection->getConnection();
7070
$output = [];
7171

7272
try {
73-
$select = $this->query->getQuery($skus);
73+
$select = $this->query->getQuery($productIds);
7474
$cursor = $connection->query($select);
75-
$processedSkus = [];
75+
$processedIds = [];
7676
while ($row = $cursor->fetch()) {
77-
$processedSkus[] = $row['sku'];
77+
$processedIds[] = $row['productId'];
7878
$output[] = $this->fillWithDefaultValues($row);
7979
}
8080

81-
$select = $this->query->getQueryForDefaultStock(\array_diff($skus, $processedSkus));
81+
$select = $this->query->getQueryForDefaultStock(\array_diff($productIds, $processedIds));
8282
$cursor = $connection->query($select);
8383
while ($row = $cursor->fetch()) {
8484
$output[] = $this->fillWithDefaultValues($row);

InventoryDataExporter/Model/Query/InventoryStockQuery.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ private function getTable(string $tableName): string
5151
/**
5252
* Get query for provider
5353
*
54-
* @param array $skus
55-
* @param bool $defaultStock
54+
* @param array $productIds
5655
* @return Select
5756
*/
58-
public function getQuery(array $skus): Select
57+
public function getQuery(array $productIds): Select
5958
{
6059
$connection = $this->resourceConnection->getConnection();
6160
foreach ($this->getStocks() as $stockId) {
@@ -65,12 +64,19 @@ public function getQuery(array $skus): Select
6564
}
6665
$select = $connection->select()
6766
->from(['isi' => $this->getTable(sprintf('inventory_stock_%s', $stockId))], [])
68-
->where('isi.sku IN (?)', $skus)
67+
->joinInner(
68+
[
69+
'product' => $this->resourceConnection->getTableName('catalog_product_entity'),
70+
],
71+
'product.sku = isi.sku',
72+
[]
73+
)->where('product.entity_id IN (?)', $productIds)
6974
->columns(
7075
[
76+
'sku' => "isi.sku",
77+
'productId' => "product.entity_id",
7178
'qty' => "isi.quantity",
7279
'isSalable' => "isi.is_salable",
73-
'sku' => "isi.sku",
7480
'stockId' => new Expression($stockId),
7581
'manageStock' => new Expression(1),
7682
'useConfigManageStock' => new Expression(1),
@@ -87,17 +93,17 @@ public function getQuery(array $skus): Select
8793
/**
8894
* Get data for default stock: "inventory_stock_1" is a view used as a fallback
8995
*
90-
* @param array $skus
96+
* @param array $productIds
9197
* @return Select
9298
* @see \Magento\InventoryCatalog\Setup\Patch\Schema\CreateLegacyStockStatusView
9399
*/
94-
public function getQueryForDefaultStock(array $skus): Select
100+
public function getQueryForDefaultStock(array $productIds): Select
95101
{
96102
$connection = $this->resourceConnection->getConnection();
97103
$stockId = $this->defaultStockProvider->getId();
98104
$select = $connection->select()
99105
->from(['isi' => $this->getTable(sprintf('inventory_stock_%s', $stockId))], [])
100-
->where('isi.sku IN (?)', $skus)
106+
->where('stock_item.product_id IN (?)', $productIds)
101107
->joinInner(
102108
[
103109
'stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item'),
@@ -106,9 +112,10 @@ public function getQueryForDefaultStock(array $skus): Select
106112
[]
107113
)->columns(
108114
[
115+
'sku' => "isi.sku",
116+
'productId' => "stock_item.product_id",
109117
'qty' => "isi.quantity",
110118
'isSalable' => "isi.is_salable",
111-
'sku' => "isi.sku",
112119
'stockId' => new Expression($stockId),
113120
'manageStock' => 'stock_item.manage_stock',
114121
'useConfigManageStock' => 'stock_item.use_config_manage_stock',

InventoryDataExporter/etc/di.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<virtualType name="Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexMetadata" type="Magento\DataExporter\Model\Indexer\FeedIndexMetadata">
2525
<arguments>
2626
<argument name="feedName" xsi:type="string">stock_statuses</argument>
27-
<argument name="feedIdentity" xsi:type="string">sku</argument>
27+
<argument name="feedIdentity" xsi:type="string">productId</argument>
2828
<!-- source table used only during full reindex -->
2929
<argument name="sourceTableName" xsi:type="string">catalog_product_entity</argument>
30-
<argument name="sourceTableField" xsi:type="string">sku</argument>
30+
<argument name="sourceTableField" xsi:type="string">entity_id</argument>
3131
<argument name="feedTableName" xsi:type="string">inventory_data_exporter_stock_status</argument>
3232
<argument name="feedTableField" xsi:type="string">id</argument>
3333
<argument name="feedTableMutableColumns" xsi:type="array">

0 commit comments

Comments
 (0)