3

I have Attribute Code. Am trying to fetch value but this code is not fetching attributes values that are in the decimal table!

$_product->getResource()->getAttribute($attributeCode)->getFrontend()->getValue($_product);

!

5 Answers 5

7

We can get attributeValue using following code

if it is a custom attribute

$products = $this->_productRepository->get($sku);
if(is_object($products->getCustomAttribute($attributeCode))){
    return $products->getCustomAttribute($attributeCode)->getValue();
}
5

I used this.

$attribute_value = $_product->getData($attributeCode);

It's Working!

4

U Can used this and Get Custome And Defult attributes Option Data, For Example

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$atrributesRepository  = $objectManager->create('\Magento\Catalog\Model\Product\Attribute\Repository');
$selectOptions = $atrributesRepository->get('AttributeCode')->getOptions();
foreach ($selectOptions as $selectOption) {
    print_r($selectOption->getData());
}
2

You can use this code to get attributes value :

$eavConfig = $this->objectManager->get('\Magento\Eav\Model\Config'); 
$attribute = $eavConfig->getAttribute('catalog_product', 'color'); 
$alloptions = $attribute->getSource()->getAllOptions(); 
foreach($alloptions as $option) { 
    $label = $option['label']; 
    $AllOptionsArr[$label][] = $option['value']; 
}
0
$product = $this->_productRepository->create()->load($productId);
$attributeValue = $product->getData('yourattribute_code');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.