Show Custom Attributes in Magento 2 on The Frontend Side

Magento 2 custom attributes some times does not shows the data on the storefront page, like product details page, category page or listing page. So here we are giving you some different ways to show custom attributes data on the product details page.

You can print the Magento 2 custom attribute value on the product details page in the following way.

To get the product value

$product->getData('attribute_code');

or

$_product->getData('attribute_code');

or

$product->getAttributeCode()

or

$_product->getAttributeText('attribute_code');

 

How to Show Custom Attributes Identifer in phtml File

You can directly copy the below code and past in to your desired file location to print the value of custom attribute. Just you need to change the attribute code and then clean the magento caches.

<?php $_product->getAttributeText('attribute_code'); ?>

 

Magento 2 Interview Question: Most Important Question & Answer

 

How to Create Custom Attribute in Magento 2

In order to show custom attributes on the front-end we have to create attribute from the backend.

Just open the Admin=>Stores=>Attribute=>Product and click on the product tab you will see the screen like below image.

Create Attribute in Magento 2

Now you have to open the add new attribute tab and fill the required details.

Default level will look like the title in your catalog product  attributes.

Show Custom Attributes

If you want to show custom attributes value of at the front store, then for this you will have to keep the visible as catalog page on storefront as shown in the screen.

Show Custom Attributes on Detail Page

Now you have to decide in which group you want to put this attribute and accordingly you can create a new group or assign it to an old group.

Create Group of Custom Attributes

 

Create Custom Attribute Group in Magento 2

Here you see in the screen that I have created a custom group called short specification, now this short specification name will look like a group in the catalog product section.

Create Custom Attributes Group

Here you are seeing in the screen that the name of the group which has been created, looks like this.

Custom Attributes Group

Now this short specification group looks like a group in the catalog product section

However, if you save the value, then it will not be visible on the front store because you have not yet called its identifier in your desired page of Magento.

Custom Attribute in Catalog Product

Now we need to add the identifier in the below file location for the product details page but you can call this according to your requirements.

design/frontend/Theme_Vender/Theme_Name/Magento_Catalog/templates/product/view/attribute.phtml

 

<div class="product-config">
<strong class="custom_attributes">

<div><?php echo $_product->getData('brand_movement'); ?></div>
<div><?php echo $_product->getData('style'); ?></div>
<div><?php echo $_product->getData('chrono'); ?></div>
<div><?php echo $_product->getAttributeText('case_materiel'); ?></div>
<div><?php echo $_product->getAttributeText('strap_material'); ?></div>
<div><?php echo $_product->getData('stiching'); ?></div>

</strong>

</div>

Now you can see here that all the values are visible because I have called the identity of the custom attribute in the product detail page.

Show Custom Attributes in Magento 2

 

 

Also Check:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Magento Store: Best 36 Magento Websites Example in The World