获取Magento属性名称而不是ID号码?

时间:2012-02-29 11:45:40

标签: php database magento attributes magento-1.5

我使用以下脚本导出我们的产品列表,以便将其发送给Google以获取其购物列表服务(Google Base)。该脚本运行良好,但它将品牌和制造商列留空:

<?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());
}

有问题的行如下:

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

我找不到显示制造商名称的方法,而不是它的ID我试图将'getAttribute'更改为'getData'和'getName',但没有人做我需要的。

感谢任何帮助!

2 个答案:

答案 0 :(得分:19)

改为使用:

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

作为背景信息的一点:
原生Magento selectmultiselect属性选项存储在eav_attribute_optioneav_attibute_option_value表格中。
option_id中的主键eav_attribute_option是与product属性关联的值 该想法是与产品相关联的选项值独立于商店视图,例如,该选项标签的翻译。

getAttributeText()方法使用属性源模型从数据库加载匹配标签并返回与ID匹配的标签:

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

有问题的来源模型是eav/entity_attribute_source_table getAttributeText()方法应该用于产品上的所有选择和多选属性。

答案 1 :(得分:3)

您是否尝试过Vinai建议的以下内容?:

$product->getAttributeText('manufacturer');

这就是名字。 其他人建议:

$product->getManufacturer();

得到了id。看看一些相关的主题:

Magento: fetching manufacturer/brand from database

查看此搜索的结果: https://stackoverflow.com/search?q=%5Bmagento%5D+manufacturer

我会测试一些样品,让你知道哪一个最可靠。

HTH