1

I'm adding code on attribute.phtml page for call custom attribute on the products details page

<div class="product attribute <?= /* @escapeNotVerified */ $_className ?>">
<?php 

$upc_code = $_product->getResource()->getAttribute('upc_code');
if ($upc_code){
    // Get Value
    $upc_value = $upc_code->getFrontend()->getValue($_product);
    // Get Label
    $upc_lable = $upc_code->getStoreLabel();
    echo $upc_lable." : ".$upc_value;
}
?>
</div>

vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml

but there are issues it's call 3 place

enter image description here

want to display under SKU

please help me

Thank you

2

Please Disable the option 'Visible on Catalog Pages on Storefront' into the attribute.Then run the indexer and cache commands

  • value display on there but it displays 3 locations want only 1 location under SKU – Brijesh Patel May 27 at 6:45
  • 1
    Ohk then ovverride the template file and display – Hardik Makwana May 27 at 6:45
1

You can display custom attributes by creating block after the sku block in catalog_product_view

Create catalog_product_view.xml in "app\design\frontend\VENDOR\THEME\Magento_Catalog\layout\catalog_product_view.xml"

<?xml version="1.0"?>
<page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Product\View" name="product_custom_attr" template="Magento_Catalog::product/view/custom_attr.phtml" after="-"/>
        </referenceContainer>
        <move element="product_custom_attr" destination="product.info.stock.sku" after="-"/>
    </body>
</page>

Create custom_attr.phtml file in "app\design\frontend\VENDOR\THEME\Magento_Catalog\templates\product\view\custom_attr.phtml"

<?php $_product = $this->getProduct(); ?>
<div class="product attribute">
<div class="value"><?php echo $_product->getData('upc_code') ?></div>
</div>
  • ok will try can u suggest me what I did the wrong code in attribute files – Brijesh Patel May 27 at 7:04
  • you can see in vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml file......attribute.phtml file called in three places you mentioned in screenshot.....to display under sku u need to create additional block using xml – Ranganathan May 27 at 7:07
  • ok, are u know why it displays 3 places when calling in from attribute files if we call it from other files it's ok – Brijesh Patel May 27 at 7:15
  • pardon me?..... – Ranganathan May 27 at 7:18

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.