dongqiang1226 2015-09-27 20:16
浏览 28

magento:无法获取自定义产品属性(选择类型)

I am writting my own rest api for magento app. I want to get the custom product attr value. But the result is alway value : N/A This mean I can't get the attributes value. I don't know why. Anyone can help? Many Thanks.

The code is like follow. Thanks.

public function _getAditional(array $excludeAttr = array()) {
    $data = array ();
    $productId = ( int ) $this->getRequest ()->getParam ( 'productid' );
    $product = Mage::getModel ( "catalog/product" )->load ( $productid );
    $attributes = $product->getAttributes ();
    //$attributes = Mage::getBlockSingleton('catalog/product_view_attributes')->getAdditionalData($product);
    foreach ( $attributes as $attribute ) {
        if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            $value = $attribute->getFrontend()->getValue($product);     

            if (!$product->hasData($attribute->getAttributeCode())) {
                $value = Mage::helper('catalog')->__('N/A');
            } elseif ((string)$value == '') {
                $value = Mage::helper('catalog')->__('No');
            } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                $value = Mage::app()->getStore()->convertPrice($value, true);
            }

            if (is_string($value) && strlen($value)) {
                $data[$attribute->getAttributeCode()] = array(
                    'label' => $attribute->getStoreLabel(),
                    'value' => $value,
                    'code'  => $attribute->getAttributeCode()
                );
            }
        }
    }
    return $data;

}

And the ProductsController.php

class Sunpop_RestConnect_ProductsController extends Mage_Core_Controller_Front_Action {

    public function getcustomoptionAction() {
        $baseCurrency = Mage::app ()->getStore ()->getBaseCurrency ()->getCode ();
        $currentCurrency = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
        $productid = $this->getRequest ()->getParam ( 'productid' );
        $product = Mage::getModel ( "catalog/product" )->load ( $productid );
        $selectid = 1;
        $select = array ();
        foreach ( $product->getOptions () as $o ) {
            if (($o->getType () == "field") || ($o->getType () == "file")) {
                $select [$selectid] = array (
                        'option_id' => $o->getId (),
                        'custom_option_type' => $o->getType (),
                        'custom_option_title' => $o->getTitle (),
                        'is_require' => $o->getIsRequire (),
                        'price' => number_format ( Mage::helper ( 'directory' )->currencyConvert ( $o->getPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' ),
                        'price_type' => $o->getPriceType (),
                        'sku' => $o->getSku (),
                        'max_characters' => $o->getMaxCharacters () 
                );
            } else {
                $max_characters = $o->getMaxCharacters ();
                $optionid = 1;
                $options = array ();
                $values = $o->getValues ();
                foreach ( $values as $v ) {
                    $options [$optionid] = $v->getData ();
                    if(null!==$v->getData('price') && null!==$v->getData('default_price')){
                        $options [$optionid]['price']=number_format ( Mage::helper ( 'directory' )->currencyConvert ( $v->getPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' );
                        $options [$optionid]['default_price']=number_format ( Mage::helper ( 'directory' )->currencyConvert ( $v->getDefaultPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' );
                    }
                $optionid ++;
            }
            $select [$selectid] = array (
                    'option_id' => $o->getId (),
                    'custom_option_type' => $o->getType (),
                    'custom_option_title' => $o->getTitle (),
                    'is_require' => $o->getIsRequire (),
                    'price' => number_format ( Mage::helper ( 'directory' )->currencyConvert ( $o->getFormatedPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' ),
                    'max_characters' => $max_characters,
                    'custom_option_value' => $options 
            );
        }

        $selectid ++;
        // echo "----------------------------------<br/>";
    }
    echo json_encode ( $select );
}

public function getproductdetailAction() {
    $productdetail = array ();
    $baseCurrency = Mage::app ()->getStore ()->getBaseCurrency ()->getCode ();
    $currentCurrency = Mage::app ()->getStore()->getCurrentCurrencyCode();
    $productid = $this->getRequest ()->getParam ("productid");
    $product = Mage::getModel ("catalog/product")->load ($productid);       
    $storeUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); 
    //$description =  nl2br ( $product->getDescription () );
    $description =   $product->getDescription ();
    $description = str_replace("{{media url=\"",$storeUrl,$description);
    $description = str_replace("\"}}","",$description);

    if ($product->getOptions ())
        $has_custom_options = true;
    else
        $has_custom_options = false;
    $addtionatt=$this->_getAditional();
    $productdetail = array (
            'entity_id' => $product->getId (),
            'sku' => $product->getSku (),
            'name' => $product->getName (),
            'news_from_date' => $product->getNewsFromDate (),
            'news_to_date' => $product->getNewsToDate (),
            'special_from_date' => $product->getSpecialFromDate (),
            'special_to_date' => $product->getSpecialToDate (),
            'image_url' => $product->getImageUrl (),
            'url_key' => $product->getProductUrl (),
            'is_in_stock' => $product->isAvailable (),
            'has_custom_options' => $has_custom_options,
            'regular_price_with_tax' => number_format ( Mage::helper ( 'directory' )->currencyConvert ( $product->getPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' ),
            'final_price_with_tax' => number_format ( Mage::helper ( 'directory' )->currencyConvert ( $product->getSpecialPrice (), $baseCurrency, $currentCurrency ), 2, '.', '' ),
            'storeUrl' => $storeUrl,
            'symbol' => Mage::app ()->getLocale ()->currency ( Mage::app ()->getStore ()->getCurrentCurrencyCode () )->getSymbol () ,
            'weight'=>number_format($product->getWeight()),
            'additional'=>$addtionatt,
            'description' => $description
    );
    echo json_encode ( $productdetail );
}

public function getPicListsAction() {
    $productId = ( int ) $this->getRequest ()->getParam ( 'product' );
    $_product = Mage::getModel ( "catalog/product" )->load ( $productid );
    $_images = Mage::getModel ( 'catalog/product' )->load ( $productId )->getMediaGalleryImages ();
    $images = array ();
    foreach ( $_images as $_image ) {
        $images [] = array (
                'url' => $_image->getUrl (),
                'position' => $_image->getPosition () 
        );
    }
    echo json_encode ( $images );
}

public function _getAditional(array $excludeAttr = array()) {
    $data = array ();
    $productId = ( int ) $this->getRequest ()->getParam ( 'productid' );
    $product = Mage::getModel ( "catalog/product" )->load ( $productid );
    $attributes = $product->getAttributes ();
    //$attributes = Mage::getBlockSingleton('catalog/product_view_attributes')->getAdditionalData($product);
    foreach ( $attributes as $attribute ) {
        if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            $value = $attribute->getFrontend()->getValue($product);     

            if (!$product->hasData($attribute->getAttributeCode())) {
                $value = Mage::helper('catalog')->__('N/A');
            } elseif ((string)$value == '') {
                $value = Mage::helper('catalog')->__('No');
            } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                $value = Mage::app()->getStore()->convertPrice($value, true);
            }

            if (is_string($value) && strlen($value)) {
                $data[$attribute->getAttributeCode()] = array(
                    'label' => $attribute->getStoreLabel(),
                    'value' => $value,
                    'code'  => $attribute->getAttributeCode()
                );
            }
        }
    }
    return $data;
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值