spanakorizo
spanakorizo

Reputation: 91

Magento: How to collect all products from a category that have x attribute

i need to create my own list.phtml from scratch and i need to display products from a certain category that will have "x" attribute. I have read a lot and play with the current getLoadedProductCollection() ?> in list.phtml but i cannot make it work.

Any advise?

Edit: Ok i was able to show all products from a category using this code: http://blog.decryptweb.com/category-products-magento/

Now how i will to filter the color as well in the $collection?

Edit2: by using this code ->addAttributeToFilter('sku', array('like' => 'UX%')) i did manage to display only products contain this SKU But when i try with color it doesnt show any products. My color attribute is multiple select. and i set the code to ('color', array('like' => 'black')) Also the layered navigation will not work? And the toolbar is not correct, it will allways display all products and it says "1 product"

Thanks in advance

Upvotes: 0

Views: 933

Answers (1)

Nick
Nick

Reputation: 6965

There are two ways you could have debugged this. The first is to look at the SQL that the collection is using to query the database, with:

echo $collection->getSelect()->assemble();

The second is to find the value of the colour attribute from the data array of your product, like:

var_dump($collection->getFirst()->getData('colour'));

In both of these cases you would have found that the attribute is stored in the database as an integer. In order to query the collection for an attribute that is stored in a select, multi-select, or similar, you'll always need to us the integer that represents the option, not the attribute label.

Upvotes: 0

Related Questions