How can I reset addAttributeToFilter in Magento search

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.

+4
source share
3 answers

The whole point of a singleton is to get the same object every time, so disabling the $ finder does not work. you can use

  $ searcher-> removeAttributeToSelect ($ k) 
to remove each attribute filter.
+1
source

@matei removeAttributeToSelect doesn't actually work. Today I found that the correct way to reset the collection is:

 $searcher->clear() ->getSelect()->reset('where'); 

This will remove the loaded _items and delete any where clauses that are in the collection. Then you can add your criteria and load collection.

+9
source

As matei has already pointed out, the problem here is that getSingleton is retrieving the same object. I replaced this declaration line with:

 $searcher = Mage::getModel('catalogsearch/advanced')->getProductCollection(); 

Now it works as desired.

0
source

Source: https://habr.com/ru/post/1307800/


All Articles