Hello guys,
I need to show an attribute into the product page and I have some difficulties with the “select” attributes. This is the xml that I have written in the product update design configuration:
<referenceContainer name="product.info.media">
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.compatibility" template="product/view/attribute.phtml" after="-">
<arguments>
<argument name="at_call" xsi:type="string">getCompatibility</argument>
<argument name="at_code" xsi:type="string">compatibility</argument>
<argument name="css_class" xsi:type="string">compatibility</argument>
<argument name="at_label" translate="true" xsi:type="string">Compatibility</argument>
<argument name="add_attribute" xsi:type="string">itemprop="compatibility"</argument>
</arguments>
</block>
</referenceContainer>
In this way I get the index of the array and not the values! I have added this row:
<argument name="at_type" xsi:type="string">text</argument>
but the result is an error:
Notice: Array to string conversion in /app/code/Magento/Catalog/view/frontend/templates/product/view/attribute.phtml on line 38
thanks
Seems that Magento 2 template doesn’t consider the case where the attribute is an array.
I have changed the attribute.phtml in my theme in this way:
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/**
* Product view template
*
* @see \Magento\Catalog\Block\Product\View\Description
*/
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
if ($_attributeType && $_attributeType == 'text') {
$_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
} else {
$_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>
<?php if ($_attributeValue): ?>
<div class="product attribute <?php /* @escapeNotVerified */ echo $_className?>">
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php /* @escapeNotVerified */ echo $_attributeLabel?></strong><?php endif; ?>
<?php if(!is_array($_attributeValue)): ?>
<div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $_attributeValue; ?></div>
<?php else: ?>
<?php foreach ($_attributeValue as $value): ?>
<div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $value; ?></div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endif; ?>