如何在 Magento 搜索中重置 addAttributeToFilter
我在循环中获取 addAttributeToFilter 函数以在 Magento 中运行时遇到问题。我的商店中有测试数据,支持搜索以下所有数据;
$attributeSelections=array( array('size' => 44, 'color' => 67, 'manufacturer' => 17),
array('size' => 43, 'color' => 69, 'manufacturer' => 17),
array('size' => 42, 'color' => 70, 'manufacturer' => 17));
我的代码用于搜索这些组合;
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);
}
该循环给出以下结果(为了观看乐趣而稍微净化);
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 ) )
所以我的循环是函数并生成搜索。然而,在循环的第一次迭代中输入 addAttributeToFilter 的值似乎仍然为每次搜索存储。 我尝试清除我的搜索对象,例如 unset($searcher) 和 unset($result)。我也尝试过magento函数,例如getNewEmptyItem()、resetData()、distinct()和clear(),但没有一个达到预期的效果。
基本上我想做的是在我的脚本尝试以编程方式创建具有这些属性组合的产品之前检查重复的产品。属性选择数组的大小可能不同,因此需要循环。
如果有人能够阐明我的问题,我将非常感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@matei 不幸的是,removeAttributeToSelect 实际上不起作用。我今天发现重置集合的正确方法是:
这将删除加载的
_items
,并删除集合选择上的任何where
子句。然后,您可以添加新的条件并加载
集合。@matei the removeAttributeToSelect doesn't actually work unfortunately. I discovered today that correct way to reset a collection is:
That will remove the loaded
_items
, and remove anywhere
clauses that are on the collection's select. You can then add your criteria fresh andload
the collection.单例的全部要点是每次都获取相同的对象,因此取消设置 $searcher 不起作用。您可以使用
to remove each attribute filter.
The whole point of the singleton is to get the same object every time so unsetting $searcher doesn't work. You can use
to remove each attribute filter.
正如 matei 已经指出的那样,这里的问题在于 getSingleton 检索相同的对象。我将此声明行替换为;
现在它可以按预期工作了。
As matei pointed out already the issue here lies with getSingleton retrieving the same object. I replaced this declaration line with;
It now works as desired.