0

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?

9
  • Try to overwrite vendor/magento/module-catalog/view/base/layout/catalog_product_prices.xml file to add new block. It may work Commented Nov 28, 2018 at 10:49
  • I know how to add blocks via XML but not product attributes. Can you elaborate please?
    – Ordog
    Commented Nov 28, 2018 at 10:54
  • Is it needed every where at price displayed ? Commented Nov 28, 2018 at 10:59
  • Yes, underneath every price :)
    – Ordog
    Commented Nov 28, 2018 at 11:02
  • You mean every page ? Commented Nov 28, 2018 at 11:05

2 Answers 2

5

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

7
  • I guess it's working product detail page only. Commented Nov 28, 2018 at 12:32
  • Yes. What other places do you need to display this custom attribute. Commented Nov 28, 2018 at 12:33
  • Ordog needs to display every where that means all pages Commented Nov 28, 2018 at 12:35
  • This is great and certainly a step in the right direction but as @VigneshBala mentioned I need to show this under the prices for Related Products, Upsells and on category pages too.
    – Ordog
    Commented Nov 28, 2018 at 13:02
  • A) You may need to override Blocks for Related Products, Upsells Products to show custom attribute. Also need to update phtml with custom code to display product custom attribute. B) To display product custom attribute in category pages, same code will works . Please make sure "Used in Product Listing" Storefront Properties for the custom attribute must set to Yes from Admin panel product attribute section. Commented Nov 28, 2018 at 14:37
0

You can access the current product (Magento 2.3.x) within price template phtml using

$product = $block->getSaleableItem();

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.