【问题标题】:magento get custom attribute labelmagento 获取自定义属性标签
【发布时间】:2014-01-24 12:14:57
【问题描述】:

我有一个问题,我想在产品页面中显示自定义属性的标签。 我从这个链接开始更好地解释我,因为这是我想要做的: http://www.customy.com/blog/how-to-display-video-on-magento-product-page/

我想在产品页面的侧边栏中有一个视频产品,所以我创建了一个新的 custompage.phtml 并将其从 catalog.xml 放在侧边栏中,在我的 custompage.phtml 中我将此代码放入了自定义标签:

getResource()->getAttribute('video')->getStoreLabel();?>

但我有这个错误:

“致命错误:在..path//中的非对象上调用成员函数getResource()”

我尝试了不同的代码,但仍然有这个问题。 我想我忘了在我的 .phtml 中放一些东西,但我是 Magento 的新手,我不知道是什么!

提前致谢!

【问题讨论】:

    标签: magento attributes


    【解决方案1】:

    由于 $_product 不起作用,因此您需要先加载对象,然后再尝试访问该属性。试试这个:

    $product_id = Mage::registry('current_product')->getId();
    $_product=Mage::getModel('catalog/product')->load($product_id);
    echo $_product->getResource()->getAttribute('video')->getStoreLabel();
    

    【讨论】:

      【解决方案2】:

      如果您无权访问产品模型,我编写了一个小查询从 DB 中获取它。这可以做得更好,但应该是您课程的一个不错的起点:

      protected $_dbConn;
      
      public function __construct()
      {
          $this->_dbConn = Mage::getSingleton('core/resource')->getConnection('core_read');
      }
      
      public function getAttributeLabel($code)
      {
          $query = "
          SELECT b.value
          FROM eav_attribute a
          JOIN eav_attribute_label b
          ON a.attribute_id = b.attribute_id
          WHERE a.attribute_code = '".$code."'";
      
          return $this->_dbConn->fetchOne($query);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多