[英]Magento: Cant get product collection use custom attribute filter?
I have a collection of products in my magento catalog that are tagged as samplers. 我的magento目录中有一些产品被标记为采样器。 I need to pull back this specific collection.
我需要撤回该特定集合。 The attribute is part of an attribute set called candies.
该属性是称为糖果的属性集的一部分。
When I go to load the collection, I add a filter to narrow by the attribute set, and then add another filter for the sampler attribute I created. 当我去加载集合时,我添加了一个过滤器以缩小属性集,然后为我创建的采样器属性添加另一个过滤器。 No matter what I do with the filter, I always get back ALL the candies not just the ones with the attribute of sampler set to "yes" or 1.
无论我对过滤器做什么,我总是会取回所有糖果,而不仅仅是采样器属性设置为“是”或1的糖果。
Here is my code, how do I get the result I am looking for? 这是我的代码,如何获得所需的结果?
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*') //select all fields available to product
->addFieldToFilter('attribute_set_id', 9) //filter the collection by the candies attribute set
->addAttributeToFilter('sampler','yes'); //filter the collection to only products that have the attribute sampler set to "yes" - this part doesnt work.
Thanks in advance. 提前致谢。
See this: http://gielberkers.com/magento-attributes-missing-using-flat-catalog/ 请参阅: http : //gielberkers.com/magento-attributes-missing-using-flat-catalog/
After considering the comments here, I would try this setting. 在考虑了此处的评论之后,我将尝试此设置。
You'll have iterate over the collection. 您将遍历整个集合。 Load the id of each product, then use ->getAttributeText('').
加载每个产品的ID,然后使用-> getAttributeText('')。
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', 'configurable')
;
foreach ($collection as $_product) {
$id = $_product->getId();
if($tncValue = $_product->load($id)->getAttributeText('terms_and_conditions')){
echo $id . ' => ' . $tncValue . "\n";
}
}
<?php
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('attribute_set_id', 9)
->addAttributeToFilter('sampler', array('eq' => '1'));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.