skip to Main Content

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); ?>

2

Answers


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

    $customerRepository = $objectManager->get('MagentoCustomerApiCustomerRepositoryInterface');
    $customer = $customerRepository->getById(1);
    $cattrValue = $customer->getCustomAttribute('c_address');
    

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

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $product = $objectManager->get('MagentoCatalogModelProduct')->load('YOUR PRODUCT ID');
    echo $product->getAttributeText('your_attribut');
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search