TransWikia.com

Loading a product collection through multiple stores

Magento Asked by 0xCAFEBABE on December 14, 2021

We’ve created an additional indexer that is started by using the backend indexer functions. We have flat tables enabled. We have multiple storeviews that correspond to different languages.

The following code is an excerpt from the indexer code.

public function rebuildIndex ($productIds) {
  $stores = Mage::app()->getStores();
  foreach ($stores AS $store) {
    Mage::app()->setCurrentStore($store);
    $products = Mage::getModel('catalog/product')
      ->getCollection()
      ->setStore($store)
      ->addAttributeToFilter('status', 1)
      ->addAttributeToFilter('visibility', array ('in' => array (Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)))
      ->addAttributeToSelect('*')
      ->setPageSize(1000)
      ->setCurPage(++$page);
  }
}

The problem is that for the second and third store, the products loaded use the language of the first storeview.

We’ve tried finding out what the problem is. So far, we’ve seen that when using flat tables, the product flat tables created are these:

  • catalog_product_flat_1
  • catalog_product_flat_2
  • catalog_product_flat_3

The number at the end corresponds to the storeview (and therefor the language). However, in all cases, the generated SELECT statement selects from the first table, making the language of the products loaded in all 3 storeviews the language of the first storeview.

We can’t disable flat tables (which we tried, and that fixes the problem). Is there any way to load the products for each of the storeviews?

One Answer

I too was facing the same issue.

Mage::unregister('_resource_singleton/catalog/product_flat'); didn't do the job for me.

I was able to solve it by setting the current context to Admin using:

Mage::app()->setCurrentStore(0);

When you look into _construct method inside app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

You can see that it's initializing catalog/product_flat only when isEnabledFlat returns true.

And isEnabledFlat would return false when we are in the admin context

Answered by Shankar Thyagarajan on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP