3

I have a cron file like Col.php. I want to get products collection in my cron file.

code in my file:

public function execute()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $collection = $objectManager->create(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class
    )->create();

    //Enable status check
    $collection->addAttributeToFilter(
        'status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
    );

    //Stock filter
    $collection->joinField(
        'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
    );

    //Visibility check
    $visibility = $objectManager->create('\Magento\Catalog\Model\Product\Visibility');
    $collection->setVisibility($visibility->getVisibleInSiteIds());

    $this->logger->debug($collection->getSize());
}

After executing my cronjob through shell

php bin/magento cron:run --group=mygroup_cron_group

I am getting error in exception log file:

Base table or view not found: 1146 Table 'my_db.catalog_category_product_index_store0' doesn't exist, query was: SELECT COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 0)
 INNER JOIN `catalog_category_product_index_store0` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=0 AND cat_index.visibility IN(3, 2, 4) AND cat_index.category_id='0' WHERE ((at_status.value = 1)) AND (e.created_in <= 1) AND (e.updated_in > 1) {"exception":"[object] (Zend_Db_Statement_Exception(code: 42): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_db.catalog_category_product_index_store0' doesn't exist, query was: SELECT COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 0)
 INNER JOIN `catalog_category_product_index_store0` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=0 AND cat_index.visibility IN(3, 2, 4) AND cat_index.category_id='0' WHERE ((at_status.value = 1)) AND (e.created_in <= 1) AND (e.updated_in > 1)

Why it is trying to fetch products from store 0 how can I get products collection?

2 Answers 2

8

Try following code.

protected $storeManager;
protected $emulation;
protected $productCollectionFactory;
protected $productStatus;
protected $productVisibility;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Store\Model\App\Emulation $emulation,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility
)
{
    $this->storeManager = $storeManager;
    $this->emulation = $emulation;
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
}

public function execute()
{
    $this->emulation->startEnvironmentEmulation(1, \Magento\Framework\App\Area::AREA_FRONTEND, true);

    $collection = $this->productCollectionFactory->create();
    $collection
        ->addAttributeToSelect('*')
        ->addStoreFilter($this->storeManager->getStore()->getId())
        ->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()])
        ;

    $this->emulation->stopEnvironmentEmulation();
}
3
  • startEnvironmentEmulation worked for me. Thanks mate :) Feb 14, 2019 at 5:18
  • perfect answer!!! +1 made my day :) May 7, 2020 at 11:05
  • Working fine...!! Aug 11, 2021 at 13:52
1

Try this,

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addWebsiteFilter();

    // filter current store products
    $collection->addStoreFilter();
    $collection->setVisibility($objectManager->get('\Magento\Catalog\Model\Product\Visibility')->getVisibleInSiteIds());
    $collection->setPageSize(5);
    foreach ($collection as $product) {
        $output->writeln($product->getName());
    }
    return;

Hope this will help.

3
  • Tried your code, still getting same error in exception.log Feb 13, 2019 at 13:04
  • Try any of this $collection->addStoreFilter(1); or $collection->addStoreFilter(0,1); Feb 13, 2019 at 13:07
  • $collection->addStoreFilter(1); worked for me, but other answer is more accurate. Thanks for taking time and answering my question. +1 Feb 14, 2019 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.