0

I'm able to display product attributes values as follow:

app/design/frontend/Vendor_name/theme_name/Magento_Catalog/layout/catalog_product_view.xml

...

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.formato" template="product/view/attribute.phtml" after="product.info.sku">
  <arguments>
    <argument name="at_call" xsi:type="string">getManufacturer</argument>
    <argument name="at_code" xsi:type="string">manufacturer</argument>
    <argument name="css_class" xsi:type="string">manufacturer</argument>
    <argument name="at_label" xsi:type="string">manufacturer</argument>
    <argument name="add_attribute" xsi:type="string">itemprop="manufacturer"</argument>
  </arguments>
</block>

...

The code above will print the value (e.g, 64). How can I print the associated manufacturer's name (e.g, Ferrari)?

edit

My attribute.phtml is as follow:

<?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 echo $_className?>">
        <?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?>:</strong><?php endif; ?>
        <span class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></span>
    </div>
<?php endif; ?>

2 Answers 2

1

eureka! Just add:

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

to the block's arguments.

0

This is my solution:

you have to edit/create your layout file:

    <referenceContainer name="product.info.main">               
        <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.manufacturer" template="product/view/attribute.phtml" after="-">
            <arguments>
                <argument name="at_call" xsi:type="string">getManufacturer</argument>
                <argument name="at_code" xsi:type="string">manufacturer</argument>
                <argument name="css_class" xsi:type="string">manufacturer</argument>
                <argument name="at_type" xsi:type="string">text</argument>              
                <argument name="at_label" xsi:type="string">Manufacturer</argument>
                <argument name="add_attribute" xsi:type="string">itemprop="manufacturer"</argument>
            </arguments>
        </block>
    </referenceContainer>

this file must be edited:

/app/design/frontend/[TEMPLATE]/framework/Magento_Catalog/templates/product/view/attribute.phtml


<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Product view template
 *
 * @see \Magento\Catalog\Block\Product\View
 */
?>
<?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 echo implode(" - ", $_attributeValue)?>
        </div>
    <?php else: ?>
        <div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $_attributeValue; ?></div>
    <?php endif; ?>
</div>
<?php endif; ?>

Your Answer

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

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