views:

267

answers:

3

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.

+2  A: 

The whole point of the singleton is to get the same object every time so unsetting $searcher doesn't work. You can use

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

matei
Thanks I just realised the issue lay within the getSingleton. I have not tested your solution but I have changed my to getModel for the initial declaration and all is working fine.
Bobby
A: 

As matei pointed out already the issue here lies with getSingleton retrieving the same object. I replaced this declaration line with;

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

It now works as desired.

Bobby
If his answer solved your problem, click the checkmark next to his post to accept his answer.
Joseph Mastey
A: 

Hi All,

I am using addAttributeToFilter but it is not working and getting wrong result.

Ex:

$collection->addAttributeToFilter(array( array( 'attribute' => 'Author_first_name_1', 'like' => "%" . trim(Mage::helper('catalogsearch')->getQuery()->getQueryText()) . "%", ), array( 'attribute' => 'Author_surname_1', 'like' => "%" . trim(Mage::helper('catalogsearch')->getQuery()->getQueryText()) . "%", ) ));

Here, I am trying to get the result whose first name or last name match with query string. But not getting correct result. If I remove first name condition from here then it is working fine.

Can any one help me??

Thanks in advanced Hiral