cancel
Showing results for 
Search instead for 
Did you mean: 

How to get custom attribute on a .phtml file

How to get custom attribute on a .phtml file

Hi,

 

I would like to know how to get custom attribute value on a .phtml file, on 1. category page list.phtml and 2. product page view.phtml.

For instance, if I create a dropdown attribute 'mycustomattribute' and the value would be an ID.

 

I would like to add a php condition such as:

if(myattributevalue == 123):

echo "This is 123";

endif;

 

4 REPLIES 4

Re: How to get custom attribute on a .phtml file

Hi @tvgarden,

You can get custom attribute value on the frontend using product object.

Also make sure in admin you have selected attribute property "use in product listing" -> Yes

Store->Attribute-> Edit your attribute

For example: if attribute code is customcolor then:

$product->getCustomcolor();

Or

$product->getData('customcolor');

If you want select type attribute label instead of value then you can use following code as well.

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

I hope it will help you!

Re: How to get custom attribute on a .phtml file

@Vimal Kumar  Thanks for the your answers.

 

i try to get the data of customer attributes i am not get the data  

// log the method
 print_r(get_class_methods($customer));
 $customer = $observer->getEvent()->getCustomer();
echo  "Mobile atti". $customer->getCustomAttribute('mobile_att);

Re: How to get custom attribute on a .phtml file

display some of the custom product attributes to transactional email of magento. ... What I have to write in default.phtml file to get art attribute value ...

Re: How to get custom attribute on a .phtml file

try this it will help you.

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct(); 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$productTypeInstance = $product->getTypeInstance();
if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) 
{
    $usedProducts = $productTypeInstance->getUsedProducts($product);

    $data = explode(',',$_product->getData('your_attribute'));
    if(count($data) > 1)
    { 
        foreach($data as $key => $value):                    
            $attr = $_product->getResource()->getAttribute('your_attribute');
            if ($attr->usesSource()): 
                $option_value = $attr->getSource()->getOptionText($value);
                echo $option_value;
            endif;
        endforeach;
    }
}