Show Product Attribute Under Price in Magento 2

magento2product-attribute

I'm trying to show a product attribute underneath the price in Magento 2. I have overridden the template by adding it to my theme:

Magento_Catalog/templates/product/price/amount/default.phtml

This is the contents of this file:

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

// @codingStandardsIgnoreFile

?>

<?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

<span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
    <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
<?php if ($block->getDisplayLabel()): ?>
    <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
<?php endif; ?>
<span <?php if ($block->getPriceId()): ?> id="<?= /* @escapeNotVerified */ $block->getPriceId() ?>"<?php endif;?>
    <?= ($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
    data-price-amount="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>"
    data-price-type="<?= /* @escapeNotVerified */ $block->getPriceType() ?>"
    class="price-wrapper <?= /* @escapeNotVerified */ $block->getPriceWrapperCss() ?>"
><?= /* @escapeNotVerified */ $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?></span>
<?php if ($block->hasAdjustmentsHtml()): ?>
    <?= $block->getAdjustmentsHtml() ?>
<?php endif; ?>
<?php if ($block->getSchema()): ?>
    <meta itemprop="price" content="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>" />
    <meta itemprop="priceCurrency" content="<?= /* @escapeNotVerified */ $block->getDisplayCurrencyCode() ?>" />
<?php endif; ?>
</span>

So I would assume that I need to add this at the top of the file:

<?php
  $_product = $block->getProduct();
  $myattr = $_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product);
?>

Then use the following to include it where I need it

<?php if($myattr != "") { echo $myattr } ?>

But I just get a blank page when I view it on the front end. Can anyone help please?

Best Answer

You can try with a separate phtml file and layout XML as describe below.

Assumed "model_number" is the custom attribute that you want to display and your custom theme is "VendorName_customtheme"

step 1: create /app/design/frontend/VendorName/customtheme/Magento_Catalog/layout/catalog_product_view.xml

File catalog_product_view.xml

<?xml version="1.0"?> 
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">    
    <body>              
    <referenceContainer name="product.info.main">
       <block class="Magento\Catalog\Block\Product\View\Attributes" name="product.mycustom.attribute" as="model_number" template="Magento_Catalog::product/view/custom_attrib.phtml" after="product.info.price" />                    
        </referenceContainer>                   
    </body>
</page>

step 2: create /app/design/frontend/VendorName/customtheme/Magento_Catalog/templates/product/view/custom_attrib.phtml

File : custom_attrib.phtml

<?php
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct();
?>
<div class="">
    <?php
    $attribute = $_product->getResource()->getAttribute('model_number');                                
    if ($attribute) 
    { 
     $attr_value = $attribute ->getFrontend()->getValue($_product); ?>   
     <?= $block->escapeHtml(__('Model no.')). $attr_value ?>
    <?php } ?>  
</div>

step 3: Run this CLI command from your magento root

sudo rm -rf pub/static/frontend/*

enter image description here

Related Topic