【问题标题】:how can i get the the attribute label from the attribute value in magento 2?如何从 magento 2 中的属性值中获取属性标签?
【发布时间】:2017-02-27 16:33:49
【问题描述】:

我从下面的代码中得到了属性值 232

$_product->getData('farben');

我如何从这个值中得到它的标签?

【问题讨论】:

    标签: attributes product magento2


    【解决方案1】:

    试试

    $_product->getAttributeText('farben');
    

    【讨论】:

      【解决方案2】:

      请试试这个代码

      第 1 步)首先您必须加载产品

      $_productCollection = $block->getLoadedProductCollection();
      

      步骤2)在产品列表页面中,会有一个foreach循环来列出这样的产品

      foreach ($_productCollection as $_product)
      

      Step3)您的代码将在此循环中。将以下代码放置在您要显示属性标签的任何位置。

      $_product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($_product);
      

      只需将 your_attribute_code 替换为您的属性名称即可。

      【讨论】:

      • @Divya 实际上此代码用于获取自定义属性值而不是标签。如果您有任何想法,请给出相关答案
      【解决方案3】:

      试试这个

      <?php $_product = $block->getProduct(); ?>
      <?php if (null !== $_product->getCustomAttribute('attribute_code')): ?>
          <?= $_product->getAttributeText('attribute_code'); ?>
      <?php endif; ?>
      

      【讨论】:

      • 您或许应该解释一下代码,以便 OP 理解问题所在。
      【解决方案4】:
      $passing_marks = $_product->getResource()->getAttribute('attribute_code');
      
      $passing_marks->getFrontend()->getLabel($_product);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多