问题描述
我在使用'addAttributeToFilter'过滤具有客户属性的产品集时遇到问题.我有一个带有选项是"和否"的下拉菜单属性,我只想显示属性设置为是"的产品列表.我当前正在使用:
I'm having issues filtering a product collection with a customer attribute using 'addAttributeToFilter.' I have a dropdown menu attribute with the options 'yes' and 'no' and I'd like to to show only a list of products with the attribute set to 'yes.' I'm currently using:
public function getReleasesCollection() {
$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToFilter('preorder', array ('eq' => 1)) ->addAttributeToSelect('name') ->addAttributeToSelect('releasedate');
public function getReleasesCollection() {
$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToFilter('preorder', array ('eq' => 1)) ->addAttributeToSelect('name') ->addAttributeToSelect('releasedate');
但是,这不返回任何产品.
However this does not return any products.
我也尝试使用:
->addAttributeToFilter('manufacturer', 1);
->addAttributeToFilter('manufacturer', '1');
->addAttributeToFilter('manufacturer', 'Yes);
->addAttributeToFilter('manufacturer', array('eq' => '1'));
->addAttributeToFilter('manufacturer', array('eq' => 'Yes'));
我可以按其他任何属性(例如名称,sku,描述等)进行过滤,但无法使用自定义属性进行过滤.
I am able to filter by any other attribute such as name, sku, description, etc. but I'm unable to filter with my custom attribute.
有指针吗?
推荐答案
<?php
class LSC_ReleaseCalendar_Block_Calendar extends Mage_Core_Block_Template
{
public function getReleasesCollection()
{
$preorderAttribute = 'preorder';
$preorderValue = 'yes';
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter(
$preorderAttribute,
array(
'eq' => Mage::getResourceModel('catalog/product')
->getAttribute($preorderAttribute)
->getSource()
->getOptionId($preorderValue)
)
);
foreach ($products as $product) {
echo $product->getName();
echo $product->getReleaseDate();
}
}
}
这篇关于在Magento中正确使用addAttributeToFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!