1

I have this code from Mukesh Chapagain: link here

$_product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$manufacturerName = $_product->getAttributeText('manufacturer');
$manufacturerId = $_product->getManufacturer();

This seems not to pick up the manufacturers even though I have them as attributes. Is it due to the fact that the manufacturer field is a drop-down?

any help in getting manufacturer attribute will be appreciated

karto
  • 3,538
  • 8
  • 43
  • 68
  • Is your attribute actually 'manufacturer' or is it 'brand'? Can you post a link to Mukesh's code? – nachito Oct 25 '11 at 14:54
  • @nachito:http://blog.chapagain.com.np/magento-get-manufacturer-name-and-id-from-product/ – karto Oct 27 '11 at 20:38
  • There is nothing wrong with the above code, just be sure that the product you are loading has a valid `manufacturer` value. Obviously nothing will get loaded unless you replace `PRODUCT_ID` with the real ID of a product entity. – clockworkgeek Nov 17 '11 at 13:34
  • thanks man. I replaced but still not working – karto Nov 21 '11 at 13:01

3 Answers3

7

To retrieve all Manufactures

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');

foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
     $attributeArray[$option['value']] = $option['label'];
     }  

foreach($attributeArray as $key=>$val){
echo $val;

}

Get products from Manufactures

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToSelect('manufacturer');
    $collection->addFieldToFilter(array(
        array('attribute' => 'manufacturer', 'eq' =>$designer_id),
    ));

Get selected manufacturer to products

 $_productCollection=$this->getLoadedProductCollection();
 foreach ($_productCollection as $_product):
  echo $_product->getAttributeText('manufacturer');
 endforeach;
Gowri
  • 16,587
  • 26
  • 100
  • 160
  • thanks man. it works fine. but not for all versions/stores. any tweaking? – karto Nov 21 '11 at 11:55
  • @karto:Welcome,don't forgot to upvote and accept if it helps.I just start Magento journey with 1.5.1.0. So i'm unaware of previous versions.Even if you give me exact problem what you facing with versions.I try to help. – Gowri Nov 21 '11 at 12:00
  • it should have worked for all versions though. whats your mail pls?: rinkarto2000 at gmail.com – karto Nov 21 '11 at 13:05
  • `getAllOptions()` takes no arguments. – user487772 Mar 30 '17 at 11:37
0

This work for me:

SELECT 
    CPEI.entity_id, 
    CPE.sku, 
    CPEI.attribute_id, 
    EA.attribute_code,
    CPEI.value AS value_id, 
    EAOV.value
FROM 
    catalog_product_entity_int AS CPEI 
        INNER JOIN eav_attribute_option_value AS EAOV ON (EAOV.option_id = CPEI.value AND EAOV.store_id = CPEI.store_id)
        INNER JOIN catalog_product_entity AS CPE USING(entity_id)
        INNER JOIN eav_attribute_option AS EAO USING(option_id)
        INNER JOIN eav_attribute AS EA ON(CPEI.attribute_id = EA.attribute_id)
WHERE 
(
    CPEI.value IS NOT NULL AND 
    EA.attribute_code = 'manufacturer' AND
    CPEI.store_id = 0  
);
Mohamad Hamouday
  • 2,070
  • 23
  • 20
0

Honestly, I cannot tell what is wrong with the code in question.

However I have recently been working on something related - if you're trying to use a collection of products. Rather than trying to fix something which probably isn't broken, think of this as an alternative suggestion.

To start with you would need to download my library of query patterns. It contains a class for drop-down attributes. The following adds a manufacturer_text column to the collection.

$products = Mage::getResourceModel('catalog/product_collection');
Knectar_Select_Product_Values::enhanceProducts($products, 'manufacturer');

foreach ($products as $product) {
    echo $product->getManufacturerText(), '<br>';
}
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127