2

I have a problem getting the collection, I have two shops and each one have their sku prepended the shop code like EN_1234 FR_3513. I tried to get the collection like always:

$productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('entity_id')
    ->addAttributeToFilter('sku', array('like' => $shop.'%'));

But from a time to now it only works for one shop, EN. I saw the query and it's getting their data from catalog_product_flat_1 where all the products are from EN, but the products of FR are in catalog_product_flat_4.

I tried to reindex by console and with the admin, but nothing. What can I do?

UPDATE

$shop = 'EN' or $shop = 'FR'

I see part of the problem is because I'm calling a script I have in the root of magento, If I use this same function inside the model all works.

4
  • can you please add $shop variable value Commented Nov 29, 2017 at 16:30
  • Use Mage::app()->setCurrentStore(0) to force collection to get all products, give it a try, additionally what is the value of $shop?
    – Adnan
    Commented Nov 29, 2017 at 16:49
  • @Adnan Mage::app()->setCurrentStore(0), works in part, because I would want to get the collection by website, but it's a good point, thanks
    – oootramas
    Commented Nov 30, 2017 at 8:58
  • 1
    @Adnan I'll accept your answer as correct, if you can write as it, I'll approbe it
    – oootramas
    Commented Dec 1, 2017 at 11:38

2 Answers 2

1

Thanks for your feedback @oootramas, Put

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

before the collection and it will fetch all the products of all stores, like

Mage::app()->setCurrentStore(0);
$productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('entity_id')
    ->addAttributeToFilter('sku', array('like' => $shop.'%'));

It will force Magento to get all products from all stores, by default it fetches the product from current store.

0

try below code

$productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('entity_id')    
    ->addAttributeToFilter('sku', array('like' => $shop.' %')) // starts with needle and space after

reference

1
  • the sku is like EN_134, if I put a space they look for EN _134, and it doesn't exist
    – oootramas
    Commented Nov 30, 2017 at 8:28

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.