0

Currently working on a Magento 2 build, and I've been stumped by something I guess is probably pretty simple.

As it stands, product pages are pretty much default, and product attributes are shown in the 'More Information' tab at the bottom.

What I'm attempting to do is echo an attribute's value elsewhere on the product page.

Since by default it seems to pass all the values to the page, I'm guessing there's simply a piece of syntax I'm missing for calling it in one of the product page view files.

I've tried a few things, such as:

$_product->getAttributeText('discount_banner');

But they seem to be the syntax for Magento pre-2.0.

If anyone can help out with what I'm doing wrong, I'd be incredibly appreciative.

1

You can achieve this all through your catalog_product_view.xml. I had to spit out some attributes just below the breadcrumbs of the product view using the following.

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.manufacturer" template="product/view/attribute.phtml" after="product.info.item">
    <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" translate="true" xsi:type="string">default</argument>
        <argument name="title" translate="true" xsi:type="string">Overview</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="manufacturer"</argument>
    </arguments>
</block>
0

You could simply check the file for \Magento\Catalog\Block\Product\View\Attributes in vendor/magento/module-catalog/view/frontend/templates/product/view/attributes.phtml

<?php
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct()
?>
<?php if ($_additional = $block->getAdditionalData()): ?>
    <div class="additional-attributes-wrapper table-wrapper">
        <table class="data table additional-attributes" id="product-attribute-specs-table">
            <caption class="table-caption"><?php /* @escapeNotVerified */ echo __('More Information') ?></caption>
            <tbody>
            <?php foreach ($_additional as $_data): ?>
                <tr>
                    <th class="col label" scope="row"><?php echo $block->escapeHtml(__($_data['label'])) ?></th>
                    <td class="col data" data-th="<?php echo $block->escapeHtml(__($_data['label'])) ?>"><?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif;?>
1
  • I've been looking into that file, unfortunately it appears to get called very late into the page load so I can't manipulate the array when I need to. Trying to mimic it elsewhere doesn't seem to find any data whatsoever, probably due to specific inheritance from other files. Feb 17 '16 at 15:36
0

You can try with this

<?php
$custom_attribute = $_product->getResource()->getAttribute('custom_attribute_code'); 
if ($custom_attribute) 
{ 
    $custom_attribute_value = $custom_attribute_attribute ->getFrontend()->getValue($_product); 
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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