How can I reset addAttributeToFilter in Magento searches
- by Bobby
I'm having problems getting the addAttributeToFilter function within a loop to behave in Magento. I have test data in my store to support searches for all of the following data;
$attributeSelections=array( array('size' => 44, 'color' => 67, 'manufacturer' => 17),
array('size' => 43, 'color' => 69, 'manufacturer' => 17),
array('size' => 42, 'color' => 70, 'manufacturer' => 17));
And my code to search through these combinations;
foreach ($attributeSelections as $selection) {
$searcher = Mage::getSingleton('catalogsearch/advanced')->getProductCollection();
foreach ($selection as $k => $v) {
$searcher->addAttributeToFilter("$k", array('eq' => "$v"));
echo "$k: $v<br />";
}
$result=$searcher->getData();
print_r($result);
}
This loop gives the following results (slightly sanitised for veiwing pleasure);
size: 44
color: 67
manufacturer: 17
Array ( [0] => Array ( [entity_id] => 2965 [entity_type_id] => 4 [attribute_set_id] => 28 [type_id] => simple [sku] => 1006-0001 [size] => 44 [color] => 67 [manufacturer] => 17 ) )
size: 43
color: 69
manufacturer: 17
Array ( [0] => Array ( [entity_id] => 2965 [entity_type_id] => 4 [attribute_set_id] => 28 [type_id] => simple [sku] => 1006-0001 [size] => 44 [color] => 67 [manufacturer] => 17 ) )
size: 42
color: 70
manufacturer: 17
Array ( [0] => Array ( [entity_id] => 2965 [entity_type_id] => 4 [attribute_set_id] => 28 [type_id] => simple [sku] => 1006-0001 [size] => 44 [color] => 67 [manufacturer] => 17 ) )
So my loop is function and generating the search. However, the values fed into addAttributeToFilter on the first itteration of the loop seem to remain stored for each search.
I've tried clearing my search object, for example, unset($searcher) and unset($result). I've also tried magento functions such as getNewEmptyItem(), resetData(), distinct() and clear() but none have the desired effect.
Basically what I am trying to do is check for duplicate products before my script attempts to programatically create a product with these attribute combinations. The array of attribute selections may be of varying sizes hence the need for a loop.
I would be very appreiciative if anyone might be able to shed some light on my problem.