Skip to content

Commit

Permalink
MAGETWO-96344: [ForwardPort][SO-3036] Bug with stock status in price …
Browse files Browse the repository at this point in the history
…indexer
  • Loading branch information
slopukhov committed Dec 10, 2018
1 parent 6478d2b commit 47675cc
Showing 1 changed file with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item;
use Magento\CatalogInventory\Model\Stock;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\PriceModifierInterface;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Query\Generator;

/**
* Class for filter product price index.
Expand All @@ -40,22 +40,38 @@ class ProductPriceIndexFilter implements PriceModifierInterface
*/
private $connectionName;

/**
* @var Generator
*/
private $batchQueryGenerator;

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

/**
* @param StockConfigurationInterface $stockConfiguration
* @param Item $stockItem
* @param ResourceConnection $resourceConnection
* @param string $connectionName
* @param Generator $batchQueryGenerator
* @param int $batchSize
*/
public function __construct(
StockConfigurationInterface $stockConfiguration,
Item $stockItem,
ResourceConnection $resourceConnection = null,
$connectionName = 'indexer'
$connectionName = 'indexer',
Generator $batchQueryGenerator = null,
$batchSize = 100
) {
$this->stockConfiguration = $stockConfiguration;
$this->stockItem = $stockItem;
$this->resourceConnection = $resourceConnection ?: ObjectManager::getInstance()->get(ResourceConnection::class);
$this->connectionName = $connectionName;
$this->batchQueryGenerator = $batchQueryGenerator ?: ObjectManager::getInstance()->get(Generator::class);
$this->batchSize = $batchSize;
}

/**
Expand All @@ -76,32 +92,37 @@ public function modifyPrice(IndexTableStructure $priceTable, array $entityIds =

$connection = $this->resourceConnection->getConnection($this->connectionName);
$select = $connection->select();

$select->from(
['price_index' => $priceTable->getTableName()],
[]
);
$select->joinInner(
['stock_item' => $this->stockItem->getMainTable()],
'stock_item.product_id = price_index.' . $priceTable->getEntityField()
. ' AND stock_item.stock_id = ' . Stock::DEFAULT_STOCK_ID,
[]
['stock_item.product_id', 'MAX(stock_item.is_in_stock) as max_is_in_stock']
);

if ($this->stockConfiguration->getManageStock()) {
$stockStatus = $connection->getCheckSql(
'use_config_manage_stock = 0 AND manage_stock = 0',
Stock::STOCK_IN_STOCK,
'is_in_stock'
);
$select->where('stock_item.use_config_manage_stock = 1 OR stock_item.manage_stock = 1');
} else {
$stockStatus = $connection->getCheckSql(
'use_config_manage_stock = 0 AND manage_stock = 1',
'is_in_stock',
Stock::STOCK_IN_STOCK
);
$select->where('stock_item.use_config_manage_stock = 0 AND stock_item.manage_stock = 1');
}
$select->where($stockStatus . ' = ?', Stock::STOCK_OUT_OF_STOCK);

$query = $select->deleteFromSelect('price_index');
$connection->query($query);
$select->group('stock_item.product_id');
$select->having('max_is_in_stock = 0');

$batchSelectIterator = $this->batchQueryGenerator->generate(
'product_id',
$select,
$this->batchSize,
\Magento\Framework\DB\Query\BatchIteratorInterface::UNIQUE_FIELD_ITERATOR
);

foreach ($batchSelectIterator as $select) {
$productIds = null;
foreach ($connection->query($select)->fetchAll() as $row) {
$productIds[] = $row['product_id'];
}
if ($productIds !== null) {
$where = [$priceTable->getEntityField() .' IN (?)' => $productIds];
$connection->delete($priceTable->getTableName(), $where);
}
}
}
}

1 comment on commit 47675cc

@Maddesto
Copy link
Contributor

Choose a reason for hiding this comment

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

$select should be filtered by entityIds, in other case ProductPriceIndexFilter will be deleting ALL out of stock products from temp price table EACH batch for price indexer

Please sign in to comment.