- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019
03:25 PM
09-26-2019
03:25 PM
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;
Labels:
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019
05:08 PM
09-26-2019
05:08 PM
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!
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!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2022
10:45 PM
03-01-2022
10:45 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022
12:55 AM
03-03-2022
12:55 AM
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 ...
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2022
11:28 PM
04-15-2022
11:28 PM
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; } }