I am having problems with the addAttributeToFilter function in a loop to behave in Magento. I have test data in my store to support the search 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 for 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 cycle gives the following results (slightly disinfected for 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 a function and generates a search. However, the values ββentered in addAttributeToFilter at the first iteration of the loop seem to be stored for each search. I tried to clear my search object like unset ($ searchcher) and unset ($ result). I also tried magento functions like getNewEmptyItem (), resetData (), distinct () and clear (), but none of them have the desired effect.
Basically what I'm trying to do is check for duplicate products before my script tries to programmatically create a product with these attribute combinations. An array of attribute selection can have different sizes, therefore, a loop is required.
I would be very helpful if someone could shed light on my problem.