按属性下拉选项值过滤产品,类别ID在magento中

时间:2015-12-17 18:17:47

标签: php magento

我想按类别ID按属性下拉选项值过滤产品。我正在使用以下查询来过滤但不起作用?

$collection1 = Mage::getModel('catalog/product')->getCollection();

$options = Mage::getModel('eav/config')
    ->getAttribute('catalog_product', 'city')
    ->getSource()
    ->getAllOptions(); //get all options

$optionId = false;
foreach ($options as $option) {
    if (strtolower($option['label']) == 'delhi') { option
        $optionId = $option['value'];
        break;
    }
}

if ($optionId) { 
    $collection1->addAttributeToFilter('city', $optionId);
}

$category = Mage::getModel('catalog/category')->load(49);
$collection1->addCategoryFilter($category);

请在此查询中建议我做错了什么?

1 个答案:

答案 0 :(得分:4)

你试过这样的吗?

$category = Mage::getModel('catalog/category')->load(49);
$_productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addCategoryFilter($category) 
    ->addAttributeToFilter('city',
        array('finset' => Mage::getResourceModel('catalog/product')
            ->getAttribute('city')
            ->getSource()
            ->getOptionId('delhi')
        )
    )
    ->addAttributeToSelect('*');