简体   繁体   中英

Get magento 2 custom attribute value

I am able to display attribute values using the code below BUT if the attribute is empty it just prints out the word "No"

<?php echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product);?>

To get the customer attribute,you can use like this:

 $customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface'); $customer = $customerRepository->getById(1); $cattrValue = $customer->getCustomAttribute('c_address');

To get the product attribute,you can use like this:

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load('YOUR PRODUCT ID'); echo $product->getAttributeText('your_attribut');

The simplest way is,

 $customer = $CUSTOMER_OBJECT; // GET customer object $customer->getCustomAttribute('variable_name')->getValue();

But you need to control $customer->getCustomAttribute('variable_name') is not NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM