开发者

Get all manufacturers for a particular store

开发者 https://www.devze.com 2023-04-12 13:35 出处:网络
The following code gets me all the manufacturers in all my stores: $attribute = Mage::getSingleton(\'eav/config\')->addStoreFilter($storeId)->getAttrib开发者_高级运维ute(\'catalog_product\', \'

The following code gets me all the manufacturers in all my stores:

$attribute = Mage::getSingleton('eav/config')->addStoreFilter($storeId)->getAttrib开发者_高级运维ute('catalog_product', 'manufacturer');
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}

However I have multiple stores and I only want the manufacturers for my chosen store id.

I've tried adding store filters to this but with no luck.

Any one have any ideas how to filter this by store?


$product = Mage::getModel('catalog/product');
$storeId = Mage::app()->getStore()->getId();    

$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
    ->setStoreId($storeId);
    ->setEntityTypeFilter($product->getResource()->getTypeId())
    ->addFieldToFilter('attribute_code', 'manufacturer') // This can be changed to any attribute code
    ->load(false);


$attribute = $attributes->getFirstItem()->setEntity($product->getResource());

/* @var $attribute Mage_Eav_Model_Entity_Attribute */
$manufacturers = $attribute->getSource()->getAllOptions(false);
return $manufacturers;

This should do it. You can lower the attribute to select on.

 $product    = Mage::getModel('catalog/product');
 $products   = $product->setStoreId($storeId)->getCollection()
                 ->addAttributeToSelect(array('name', 'price', 'small_image','short_description','manufacturer'), 'inner');

        $this->setProductCollection($products);


See this blog post: http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/

Go to the section labled "How to get Only the Attribute Values that have been used on products"


I got the list of manufacturer from the current store using product count

    public function getAllManu()
{
  $product = Mage::getModel('catalog/product');
  $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
              ->setEntityTypeFilter($product->getResource()->getTypeId())
              ->addFieldToFilter('attribute_code', 'manufacturer'); //can be changed to any attribute
  $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
  $attrMenuItems = $attribute->getSource()->getAllOptions(false);

    foreach ($attrMenuItems as $key => $value) {
       $collection = Mage::getModel('catalog/product')->getCollection();
       $collection->addFieldToFilter(array(array('attribute' => 'manufacturer', 'eq' => $value['value'])));
       $numberOfProducts = count($collection);
       $attrMenuItems[$key]['products_count'] = $numberOfProducts;
    }   

  return $attrMenuItems;
}
0

精彩评论

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

关注公众号