Pages

Thursday, July 10, 2014

How to get a list of manufacturers ids

To get an array of the manufacturer names & ids use the following code:


$product = Mage::getModel('catalog/product');
$attributes =Mage::getResourceModel('eav/entity_attribute_collection')
  ->setEntityTypeFilter($product->getResource()->getTypeId())
  ->addFieldToFilter('attribute_code', 'manufacturer');
$attribute = $attributes->getFirstItem()
  ->setEntity($product->getResource());
$manufacturers = $attribute->getSource()->getAllOptions(false);


you can test code snippet like above with a php shell as provided in Magento PHP Developer's Guide (p.58)


the result looks similar to this:

magento > print_r($manufacturers);
Array
(
  [0] => Array
  (
   [value] => 6
   [label] => Manfucturer A
  )
  [1] => Array
  (
   [value] => 20
   [label] => Manfucturer B
  )
  [2] => Array
  (
   [value] => 21
   [label] => Manfucturer C
  )
  [3] => Array
  (
   [value] => 27
   [label] => Manfucturer D
  )
)
magento >


No comments:

Post a Comment