Magento2: Yes/No attribute value is not shown on a product details page

Created on 16 Sep 2016  路  11Comments  路  Source: magento/magento2

Preconditions

  • Magento CE 2.1.1 with sample data (Luma theme).

    Steps to reproduce

  1. Navigate to the backend.
  2. Go to Stores -> Attributes -> Product
  3. Click on the Add New Attribute button
  4. Set "Catalog Input Type for Store Owner" = Yes/No, set "Visible on Catalog Pages on Storefront" = Yes, and fill other required fields.
  5. Click Save Attribute button
  6. Go to Products -> Catalog. Click on some product that is visible on storefront.
  7. Find the newly created attribute field and fill it with Yes or No value. Save product.
  8. Clean cache and reindex:
$ bin/magento cache:clean
$ bin/magento indexer:reindex
  1. Open a storefront product page.

    Actual result

The More Information tab doesn't contain our newly created attribute.

Expected result

The More Information tab contains our newly created attribute.

Additional information

If I make some yes/no attribute visible on frontend, it doesn't appear in the More Information tab on a product details page. I debugged it and found that this relates to the fact that translated string are wrapped into the Phrase object: https://community.magento.com/t5/Magento-2-Feature-Requests-and/function-returns-Phrase-object-Could-this-return-string/idi-p/45075

Particularly, it's \Magento\Catalog\Block\Product\View\Attributes::getAdditionalData() - it requires $value to be string to be added to the $data array. In case of Yes/No attributes $value is an object of \Magento\Framework\Phrase.

Catalog Fixed in 2.2.x Fixed in 2.3.x Cannot Reproduce Clear Description Format is valid Ready for Work bug report

All 11 comments

Internal ticket created MAGETWO-59267. Thanks for reporting

Yes/No Attributes are not visible on frontend due to error in the file

-/Magento/Catalog/Block/Product/View/Attributes.php

function getAdditionalData(array $excludeAttr = []) check if the attributes value is string but in case of Yes/No Attributes the value is something like this -

object(Magento\Framework\Phrase)#2322 (2) { ["text":"Magento\Framework\Phrase":private]=> string(3) "Yes" ["arguments":"Magento\Framework\Phrase":private]=> array(0) { } }

here is the check for string -

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

other attribute types are working fine for me.

When will this be fixed?

When will this be fixed. Has been open for a long time.

This is our solution:

Extend the condition for attributes that are instances of \Magento\Framework\Phrase.

use Magento\Framework\Phrase;
...
if ($value instanceof Phrase || (is_string($value) && strlen($value))) {
    $data[$attribute->getAttributeCode()] = [
        'label' => __($attribute->getStoreLabel()),
        'value' => $value,
        'code' => $attribute->getAttributeCode(),
    ];
}

@TKlement, fix idea looks good to me, maybe prepare a pull request? ;)

Internal ticket to track issue progress: MAGETWO-65364

@TKlement The solution you outlined is close, but if you have a large number of custom attributes this will show any non-defined attributes for a product as "N/A". If you modify your solution slightly to this:

use Magento\Framework\Phrase;
...
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
   $value = $this->priceCurrency->convertAndFormat($value);
} elseif ($value instanceof Phrase) {
    $value = $value->getText();
}

if (is_string($value) && strlen($value)) {
...

So instead of modifying the last if block in Magento\Catalog\Block\Product\View\Attributes->getAdditionalData() to allow objects of type Phrase, you instead add an additional elseif to the block before that to check for type Phrase and then set the value equal to $value->getText().

By doing this if the text value of the attribute is still empty, it will not be shown on the frontend of the site.

@alexkuk, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.3.0-dev, 2.2.0, 2.1.9

The issue has been fixed and delivered to 2.2-develop branch. Will be available with upcoming patch release

Sorry @okobchenko, this is not included in 2.2.2, right?

Was this page helpful?
0 / 5 - 0 ratings