douji5523 2010-04-23 17:26
浏览 47
已采纳

如何在Magento搜索中重置addAttributeToFilter

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.

  • 写回答

3条回答 默认 最新

  • dtn51137 2010-04-23 17:44
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)