开发者

Magento: how to filter the result of Mage::getResourceModel('eav/entity_attribute_collection') by Category

开发者 https://www.devze.com 2023-01-13 18:47 出处:网络
I\'ve used the follow code to get the whole list of manufacturer. $currentCategory = Mage::registry(\'current_category\');

I've used the follow code to get the whole list of manufacturer.

    $currentCategory = Mage::registry('current_category');
    $product = Mage::getModel('catalog/product');       
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getRe开发者_如何学JAVAsource()->getTypeId())->addFieldToFilter('attribute_code', 'brand_name');      
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());       
    $manufacturers = $attribute->getSource()->getAllOptions(false);      

Now, how can I get the manufacturer list that belong to a specific category? I've been digging for a while to find out a way to limit the result by a category but no luck. Can ResourceModel filtered by Category?

Thanks.


I think you'd be better off starting with a collection of products from the category and then getting the attributes that match those products. Something like:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
foreach($products as $product):
  $product = Mage::getModel('catalog/product')->load($product->getId());
  $manufacturers[] = $product->getBrandName();      
endforeach;

You would need to deduplicate the array, but array_unique() should help you out there.

HTH, JD

EDIT

This will prevent the need to load each product, should improve performance:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
$products->addAttributeToSelect('brand_name');
$products->load();  //execute the query
$manufacturers = $products->toArray(array('brand_name'));

Keep in mind that with caching, the performance hit only costs you once.

JD


Thanks for the nice way to get the option id for the attribute. It really helped me a lot! Here is what I ended up doing to get the option id and its text value:

$currentCategory = Mage::registry('current_category');
$products = $currentCategory->getProductCollection();
$products->addAttributeToSelect('brand_name');
$products->load();  //execute the query
$manufacturers = $products->toArray(array('brand_name'));   
$temp = array();
foreach($manufacturers as $v) {
    $temp[] = $v['brand_name'];
}
$brands = implode(",",array_unique($temp));
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT eav_attribute_option_value.option_id, eav_attribute_option_value.value FROM eav_attribute_option_value INNER JOIN eav_attribute_option ON eav_attribute_option_value.option_id=eav_attribute_option.option_id WHERE eav_attribute_option.attribute_id=505 AND eav_attribute_option_value.option_id IN (".$brands.")");

while ($row = $readresult->fetch() ) {
    $brandlist[] = array('value'=>$row['option_id'], 'label'=>$row['value']);
}

The brandlist array will have the option id and value so that I can use it to link.

Thanks again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号