Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Magento attribute names instead of ID numbers?

I am using the following script to export a list of our products to send to Google for their shopping listing service (Google Base). The script works well, but it leaves the brand and manufacturer columns empty:

<?php
define('SAVE_FEED_LOCATION','google_base_feed.txt');
set_time_limit(1800);
require_once '../app/Mage.php';
Mage::app('default');
try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w');

    $heading = array('id','mpn', 'upc','title','description','link','image_link','price','brand','product_type','condition', 'google_product_category', 'manufacturer', 'availability');
    $feed_line=implode("\t", $heading)."\r\n";
    fwrite($handle, $feed_line);

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1);
    $products->addAttributeToFilter('visibility', 4);
    $products->addAttributeToSelect('*');
    $prodIds=$products->getAllIds();

    $product = Mage::getModel('catalog/product');

    $counter_test = 0;

    foreach($prodIds as $productId) {

        if (++$counter_test < 30000){

            $product->load($productId);

            $product_data = array();
            $product_data['sku'] = $product->getSku();
            $product_data['mpn'] = $product->getData('upc');
            $product_data['upc'] = $product->getData('upc');

            $title_temp = $product->getName();
            if (strlen($title_temp) > 70){
                $title_temp = str_replace("Supply", "", $title_temp);
                $title_temp = str_replace("  ", " ", $title_temp);
            }
            $product_data['title'] = $title_temp;

            $product_data['description'] = substr(iconv("UTF-8","UTF-8//IGNORE",$product->getDescription()), 0, 900);
            $product_data['Deeplink'] = "http://www.domainname.co.uk/store/".$product->getUrlPath(); 
            $product_data['image_link'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();

            $price_temp = round($product->getPrice(),2);
            $product_data['price'] = round($product->getPrice(),2) * 
1.2;
            $product_data['brand'] = $product->getAttribute('manufacturer');

            $product_data['product_type'] = 'Pet Products and Accessories';
            $product_data['condition'] = "new";
            $product_data['category'] = $product_data['brand'];
            $product_data['manufacturer'] = $product_data['brand'];

            $product_data['availability'] = "in stock";

            foreach($product_data as $k=>$val){
                $bad=array('"',"\r\n","\n","\r","\t");
                $good=array(""," "," "," ","");
                $product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
            }

            echo $counter_test  . " ";

            $feed_line = implode("\t", $product_data)."\r\n";
            fwrite($handle, $feed_line);
            fflush($handle);

        }

    }

    fclose($handle);
}
catch(Exception $e){
    die($e->getMessage());
}

The line in question is the following:

$product_data['brand'] = $product->getAttribute('manufacturer');

I can't find a way to show the manufacturer name instead of it's ID I have tried to change 'getAttribute' to 'getData' and 'getName' but none do what I need.

Any help is appreciated!

like image 535
dannymcc Avatar asked Feb 29 '12 11:02

dannymcc


2 Answers

Use this instead:

$product_data['brand'] = $product->getAttributeText('manufacturer');

As bit of background information:
native Magento select and multiselect attribute options are stored in the eav_attribute_option and eav_attibute_option_value tables.
The primary key option_id from the eav_attribute_option is the value that is associated with the product attribute.
The idea being that the option value associated with the product is independent of the store view, e.g. translation of that option label.

The getAttributeText() method uses the attribute source model to load the matching labels from the database and return the label matching the ID:

public function getAttributeText($attributeCode)
{
    return $this->getResource()
        ->getAttribute($attributeCode)
            ->getSource()
                ->getOptionText($this->getData($attributeCode));
}

The source model in question is eav/entity_attribute_source_table.
The getAttributeText() method should be used for all select and multiselect attributes on products.

like image 72
Vinai Avatar answered Oct 30 '22 11:10

Vinai


Have you tried the following like Vinai suggested?:

$product->getAttributeText('manufacturer');

That gets the name. Others suggested this:

$product->getManufacturer();

That gets the id. Have a look at some related threads:

Magento: fetching manufacturer/brand from database

Have a look at the results from this search: https://stackoverflow.com/search?q=%5Bmagento%5D+manufacturer

I will test some samples out, and let you know which one is most reliable.

HTH

like image 26
ShaunOReilly Avatar answered Oct 30 '22 09:10

ShaunOReilly