0
votes

I'm using attributes to a product, availablepostalcodes. This is a multi-select, there are 3 options per product: 1234 2345 3456

Now after a post (form) on the categorypage i've $_POST['postalcode'], this contains 2345. But when i do this nothing will filter:

$_productCollection=$this->getLoadedProductCollection();
$_productCollection->addAttributeToFilter("availablepostalcodes", array("finset"=>"5021"));


echo $_productCollection->load()->count(); 

The echo still shows 5, however it should be 1, cause one product has that available postalcode.

This is the product-list view /catalog/product/list.phtml

What do i miss or don't see?

Edit:

$_productCollection = Mage::getModel('catalog/product')
                              ->getCollection()
                              ->addAttributeToSelect('availablepostalcodes')
                              ->addAttributeToFilter('entity_id', array('in' => $_productCollection->getAllIds()));  

Then i get all the products again.

But when i do $_productCollection->addAttributeToFilter("availablepostalcodes", array("in" => array(5021'))); it still doesn't work

2
Hay you can print this result like that echo $_productCollection->addAttributeToFilter("availablepostalcodes", array("finset"=>"5021"))->__toString(); . Result can help to you. - 502_Geek
Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Product_Collection::__toString() in /home/drnjyser/domains/happentest.nl/public_html/app/design/frontend/default/vanhappen/template/catalog/product/list.phtml on line 42 - Ronn0
The method getLoadedProductCollection() already has loaded the products from the database, so any addAttributeToFilter() after loading can't and won't have any effect on the result. - Jürgen Thelen
Maybe the collections clear method could unset the loaded flag so further filtering could be applied. - Simon H
I did an edit in the code. - Ronn0

2 Answers

1
votes

you have a multiselect so you need to filter for the multiselect option id not the option value!

1
votes

try in instead of finset

$_productCollection->addAttributeToFilter("availablepostalcodes", array("in"=>"5021"));