Skip to content

Instantly share code, notes, and snippets.

@zainengineer
Last active October 21, 2018 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zainengineer/7533af9942eae2fd95e4cada6d666165 to your computer and use it in GitHub Desktop.
Save zainengineer/7533af9942eae2fd95e4cada6d666165 to your computer and use it in GitHub Desktop.
<?php
//n98-magerun2.phar dev:console
#useful for old multi-stock implementation for magento
#but can be skipped
$stockId = 7;
$storeId = 3;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\Indexer\IndexerRegistry $indexRegistry */
//$indexRegistry = $objectManager->create('\Magento\Framework\Indexer\IndexerRegistry');
//
//$productFlatIndexer = $indexRegistry->get('catalog_product_flat');
//$indexRegistry->getIndexesZ();
//$productFlatIndexer->invalidate();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$myClassReflection = new ReflectionClass(get_class($productCollection));
$secret = $myClassReflection->getProperty('_flatEnabled');
$secret->setAccessible(true);
$flat = $secret->getValue($productCollection);
$flat[$storeId] = false;
$secret->setValue($productCollection,$flat);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $productCollection;
$collection->setStoreId($storeId);
/**
* visibility always adds joins with cat_index
* @see \Magento\Catalog\Model\ResourceModel\Product\Collection::_productLimitationJoinStore()
*
*/
//
$collection->setVisibility([2,4]);
//if you do not set this filter because of join with cat_index you will get duplicate product_id
$collection->addStoreFilter($storeId);
/**
* multi-inventory
* @var \Magento\CatalogInventory\Helper\Stock $stockFilter
* */
$stockFilter = $objectManager->create('\Magento\CatalogInventory\Helper\Stock');
$stockFilter->addInStockFilterToCollection($collection);
$from = $collection->getSelect()->getPart('from');
$bAddInventory =isset($from['at_inventory_in_stock']) && !empty($stockId);
$bAddInventory && $collection->getSelect()->where('at_inventory_in_stock.stock_id = ?',$stockId);
// return (string) $collection->getSelect();
$collection->setPageSize(100);
return $collection->count();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment