Magento – How Can a product model get its eav Attribute in Magento 2

eavmagento2PHPresource-model

I added a custom field to product in admin panel named vendor_name
So I wanna get value of this field by do something like

$_product->getVendorName()

Like what I can do with non eav attribute (i.e $product->getSku())
But $_product->getVendorName() give me null value ,
What is the best solution to get attribute ? I wanna same syntax for both eav and None eav attribute.

Best Answer

In Store > Attributes > Product > YOUR ATTRIBUTE > Storefront Properties set Visible on Catalog Pages on Storefront and Used in Product Listing to TRUE.

In your code you can use:

<?php echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->getYourAttribute(), 'your_attribute') ?>

or

<?php echo $_product->getYourAttribute() ?>