Magento: fetching manufacturer/brand from database

2020-07-30 02:34发布

问题:

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

回答1:

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;


回答2:

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>';
}


标签: magento