cancel
Showing results for 
Search instead for 
Did you mean: 

Display custom attribute in product detail page

Display custom attribute in product detail page

Hello. 

I am trying to display custom attribute on product detail page. I have found many instruction , but none of those codes display anything in my page. 

I am using Luma ( custom theme ) Magento 2.4.5 ( recent ) . 

I like to show Manufacturer on top of the Product Title. but so far I have not been able to do that. also I like this attribute to be a hyperlink to either CMS page or as a search criteria for given manufacturer . 

3 REPLIES 3

Re: Display custom attribute in product detail page

hello.

I finally manged to fix the issue . i used following code in my custom.phtml file

<?php
$product = $block->getProduct();
$attrData = $product->getAttributeText('manufacturer');
$blockTitle = $this->getData('title');
if (trim($attrData) !== "") : ?>
    <div class="custom-attr">
        
        <?php echo $attrData; ?>
    </div>
<?php endif; ?

I can show Manufacturer name over the Product Title. One more thing I Like to do , how can i turn this into a link ? i want to be able to click on the manufacturer name and either show the search result of go to custom Static CMS file.

any advice or code would be appreciate.

thanks.

Re: Display custom attribute in product detail page

Hello @tonysargma79f8 

 

Open /vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml 

 

OR If you are using any theme then follow the following path

 

/app/design/frontend/Vendor_Name/Theme_Name/Magento_Catalog/layout/catalog_product_view.xml, place this code, replacing {AddAttributeCodeHere} with your custom attribute code:

 

<?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" name="product.attribute.{AddAttributeCodeHere}" template="Magento_Catalog::product/view/attribute.phtml">
                <arguments>
                    <argument name="at_call" xsi:type="string">get{AddAttributeCodeHere}</argument>
                    <argument name="at_code" xsi:type="string">{AddAttributeCodeHere}</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

For Reference: https://i.imgur.com/1f4VWtR.png

 

Hope this helps!

 

If you find our reply helpful, please give kudos.

 

An award-winning North American Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9

Re: Display custom attribute in product detail page

Where to place custom.phtml file in directory?