If you need only one product attribute value in Magento 2, you do not always need to load the entire product through a repository or collection. That can be wasteful in loops, cron jobs and imports. Using the Product Resource…
Reading a customer attribute (Magento 2)
Want to read a particular customer attribute in Magento 2? Use the following code to retrieve an attribute value for any customer by customer ID:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, ... ) { $this->customerRepository = $customerRepository; } $customer = $this->customerRepository->getById($customerId); $customAttr = $customer->getCustomAttribute('what_ever'); if ($customAttr) { echo $customAttr->getValue(); } |
Getting a product attribute value for All Store Views (Magento 2)
Sometimes you need to retrieve the value of a Magento 2 product attribute for “All Store Views”, for example in a template. To do this, set the store ID to 0, load the product and the required product attribute, and…
Getting a category attribute value (Magento 1)
You can use this code to retrieve any attribute of a Magento category:
|
1 |
$attributeValue = $category->getData('attribute_code'); |
Getting the value of a category attribute (Magento 1)
Use this code to retrieve the value (rather than the ID) of a category attribute: $value = $currentCategory->getResource()->getAttribute(‘attribute_identifier’)->getFrontend()->getValue($currentCategory);
Getting a product’s manufacturer (Magento 1)
The following code retrieves the name of a product’s manufacturer. $manufacturer = $product->getAttributeText(‘manufacturer’);