1

I want to select ids of a specific brand when I have the name and the Id of a product for example 111

I found out the ID of manufacturer by using getManufacturer()

I used this code in template/catalog/product/list.phtml :

$_productCollection=$this->getLoadedProductCollection();
$_collection_selected=$_productCollection->addAttributeToFilter('manufacturer', array('eq' => 111));
    foreach ($_collection_selected as $_product_selected):
                $product_selected_ids[]=$_product_selected->getId();
                 print_r($product_selected_ids);
            endforeach;

but it shows me all the products I don't know what is wrong?

7
  • What is Block instance i.e. your $this, but most likely your collection is already been loaded.
    – Sergei Guk
    Commented Feb 28, 2014 at 19:15
  • What I meant it's already been loaded from magento and you can't filter it like that (not tested). And yes you need to use Code
    – Sergei Guk
    Commented Feb 28, 2014 at 19:22
  • yeah I got it mostly by magento.stackexchange.com/questions/9791/… it seems I am close to solution
    – Nickool
    Commented Feb 28, 2014 at 19:24
  • I think this might help stackoverflow.com/questions/5280392/…
    – Sergei Guk
    Commented Feb 28, 2014 at 19:26
  • what should i use for type_id?
    – Nickool
    Commented Feb 28, 2014 at 19:28

1 Answer 1

0

Your problem probably lies in the fact that the collection you try to adjust is already loaded. When a collection is loaded, the query is already executed and the data is ready for usage in the collection.

The only way to adjust the collection after loading is by calling a $collection->clear(). If you want mor detailed technical information you can check the Mage_Catalog_Model_Resource_Product_Collection in which the clear() method can be found.

5
  • I didn't write $this->getLoadedProductCollection() it was there before and it doesn't have problem and I don't change it so no need to call it
    – Nickool
    Commented Feb 28, 2014 at 21:30
  • Yes, correct. The call was already there and you did not made a change it. But afterwards, the 2nd line in your snippet, you try to append a attribute filter to that very collection. This new attribute filter will be ignored by Magento, since the collection you try to adjust is already in a 'loaded' state. Hence the result is still all the products. You can only apply attribute filters before the collection is loaded. Hence the loaded state must be cleared as I tried to explain in my answer.
    – Tim Hofman
    Commented Feb 28, 2014 at 21:38
  • you don't know how much I should thank you! brilliant!
    – Nickool
    Commented Feb 28, 2014 at 21:48
  • Sounds like you made some progress :). Good luck with the rest!
    – Tim Hofman
    Commented Feb 28, 2014 at 21:54
  • yeah I learned a big tip from you,you solved my problem that i am struggling from the morning
    – Nickool
    Commented Feb 28, 2014 at 22:02

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.