Magento2 How to get custom product attribute value in Product View page

In Magento2 product detail page you can able to see the product description, short description, price, etc.. these are all the attributes already exists in Magento. But every business may need some more additional attribute based on their products. let’s say if you want to create additional attribute and you want to show those attributes in product view page, you need to follow the below steps.

Let’s assume I want to specify the product warranty in product detail page.

Step 1 : Create a new attribute ‘warranty’ in Magento back end under STORES->Attributes->Product.

Step 2 : Assign the newly created attribute ‘warranty’ to the default attribute set. You can see the attribute set option under STORES->Attributes->Attribute set.
I assume you already know how to create and assign the attribute to attribute set. So we don’t want to go deeper.

Step 3: Now we can able to see the attribute while creating the products. we can give whatever value for the attribute.

Step 4: Edit the catalog_product_view.xml file and update the below content. you can add this section inside the container. you can add your block next to block. So it will be shown next to the short description.

<block class="Magento\Catalog\Block\Product\View\Description" name="" template="product/view/demo.phtml" after="-">
<argument name="at_call" xsi:type="string">getWarranty</argument>
<argument name="at_code" xsi:type="string">warranty</argument>
<argument name="css_class" xsi:type="string">warranty </argument>
<argument name="at_label" xsi:type="string">warranty </argument>
<argument name="add_attribute" xsi:type="string">itemprop="warranty "</argument>

Step 5: Create a new file warranty.phtml under mageno2root/vendor/magento/module-catalog/view/frontend/templates/product/view with the below content.

$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
<?php endif; ?>

That’s it. Now we can able to see the warranty value in product detail page next to short description section.
Hope it helps for someone who starts to work in Magento2.


  1. hi,
    When i implement the same code, my attribute not getting displayed in product page

  2. Use the below code you can get the custom opton value

    $i = 1; foreach ($currentproduct->getOptions() as $value) {
    echo “Custom Option:” . $i . ““;
    echo “Custom Option TYPE: ” . $value->getType() . “”;
    echo “Custom Option TITLE: ” . $value->getTitle() . “”;
    echo “Custom Option Values: “; // Getting Values if it has option values, case of select,dropdown,radio,multiselect
    $values = $value->getValues();
    foreach ($values as $values) {
    } $i++;

  3. I want to get dropdown attribute value on product list view how do i use above code?

Leave a Comment

Your email address will not be published. Required fields are marked *

8 + = 17