0

I'm building a Luma child theme and I want to display my "brand" product attribute above the product title on my product page.

I am currently using the following code in my child theme's copy of catalog_product_view.xml to also display my "ingredients" attribute, this works perfectly because the attribute type is just text:

<referenceContainer name="product.info.main">

  <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.ingredients" template="product/view/attribute.phtml" before="-">
      <arguments>
          <argument name="at_call" xsi:type="string">getIngredients</argument>
          <argument name="at_code" xsi:type="string">ingredients</argument>
          <argument name="css_class" xsi:type="string">ingredients</argument>
      </arguments>
  </block>

</referenceContainer> 

My issue is that my Brand attribute type is a dropdown box, if I try to use the code above for dropdown attributes, it just displays a number in the frontend and not my dropdown box value.

How can I properly display the first value in the list? I do not need to display the attribute in a dropdown on the frontend, there is only one value associated with this dropdown, not a list with multiple options.

The fact that the attribute type is a dropdown is only for backend management purposes, so displaying the first value from the list as just text would be fine.

1 Answer 1

1

See the blocks original template (vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml), especially this condition:

$_attributeType = $block->getAtType();
//[...]
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code))
        ? $_product->getAttributeText($_code)
        : '';
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}

So, in your list of arguments add the argument "at_type" and set it to "text" and you should see the attribute option's label

<argument name="at_type" xsi:type="string">text</argument>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.