Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php magento:无法获取自定义产品属性(选择类型)_Php_Magento_Attributes - Fatal编程技术网

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

Php magento:无法获取自定义产品属性(选择类型),php,magento,attributes,Php,Magento,Attributes,我正在为magento应用程序编写自己的RESTAPI。 我想获取自定义产品属性值。 但结果总是如此 价值:不适用 这意味着我无法获取属性值。 我不知道为什么。有人能帮忙吗?非常感谢 代码如下所示。谢谢 } 以及ProductsController.php }你们用平板桌子吗?该属性在平面表格中可用吗?谢谢。我犯了一个错误,它应该是$product=Mage::getModel'catalog/product'->加载$productid;------非$product=Mage::getMod

我正在为magento应用程序编写自己的RESTAPI。 我想获取自定义产品属性值。 但结果总是如此 价值:不适用 这意味着我无法获取属性值。 我不知道为什么。有人能帮忙吗?非常感谢

代码如下所示。谢谢 } 以及ProductsController.php
}你们用平板桌子吗?该属性在平面表格中可用吗?谢谢。我犯了一个错误,它应该是$product=Mage::getModel'catalog/product'->加载$productid;------非$product=Mage::getModel目录/产品->加载$productid;除了你使用的引语,还有什么不同?
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;
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;
}