6

I am trying to retrieve the list of dropdown attributes and check if the value exists (if it does i need to get the value and assign it to the product) and if it doesnt i will have to create it and get its value to assign it to the product.

$attribute = $this->objectManager->create('Magento\Eav\Model\Entity\Attribute');
$attributeId = $attribute->getIdByCode('catalog_product','manufacturer');
$model = $this->objectManager->create('Magento\Catalog\Model\ResourceModel\Eav\Attribute');
$model->load($attributeId);
print_r($model->getFrontendLabel());
Zubair
  • 63
  • 1
  • 1
  • 5

5 Answers5

18

Following Magento 2 guidelines, you should not use ObjectManager by yourself. Instead, you must use dependency injection. More info here

In your Block/Controller/Helper..., create a constructor and inject \Magento\Catalog\Api\ProductAttributeRepositoryInterface class. For example :

private \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository;

public function __construct(
    \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository
) {
    $this->productAttributeRepository = $productAttributeRepository;
}

Then, in your dedicated method, you want to call (PHPDoc added for clarity) :

/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $manufacturerOptions */
$manufacturerOptions = $this->productAttributeRepository->get('manufacturer')->getOptions();

You can now get options values and labels this way :

foreach ($manufacturerOptions as $manufacturerOption) {
    $manufacturerOption->getValue();  // Value
    $manufacturerOption->getLabel();  // Label
}
Yonn Trimoreau
  • 539
  • 7
  • 23
2
<?php echo $_product->getResource()->getAttribute('movement')->getFrontend()->getValue($_product);?>

$_product is the object of Product The above code returns attribute value of attribute name "movement".

Sreenath
  • 21
  • 2
1

Using API Service layer, for EAV Attribute of any entity type, Inject the service data member in your constructor as follow.

protected $eavAttributeRepository;
public function __construct(
    ...
    \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepositoryInterface,
    ...
){
    ...
    $this->eavAttributeRepository = $eavAttributeRepositoryInterface;
    ...
}

And the you can get the attribute using this.

$attribute = $this->eavAttributeRepository->get('catalog_product', 'attribute_code_here');
// vardump($attribute->getData());

In order to get attribute option values array, use this.

$options = $attribute->getSource()->getAllOptions();
saiid
  • 635
  • 1
  • 6
  • 20
  • This is very helpful, and to add on to this for anyone else looking, if you want to get the attribute code by label,if you get the attribute using the above method then you can do this to get the individual attribute code: $attributeCodeId = $attribute->getSource()->getOptionId('Attribute Label'); – Erica S. Jan 03 '20 at 00:11
1
Try the following code

$attribute_code = "coffe_type";
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product',$attribute_code );

$options = $attribute->getSource()->getAllOptions();
foreach($options as $option) {
    $optionsExists[] = array('label' => $option['label'], 'value'=> $option['value'] );
}

print_r($optionsExists);
ssatish4u
  • 131
  • 2
  • 9
0

Inject an instance of \Magento\Catalog\Model\Product\Attribute\Repository in your constructor (in a block, helper class or wherever):

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Repository $_productAttributeRepository
 */
protected $_productAttributeRepository;

/**
 * ...
 * @param \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository
 * ...
 */
public function __construct(
    ...
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
    ...
) {
    ...
    $this->_productAttributeRepository = $productAttributeRepository;
    ...
}

Then create a method in your class to get the attribute by code:

/**
 * Get single product attribute data 
 *
 * @return Magento\Catalog\Api\Data\ProductAttributeInterface
 */
public function getProductAttributeByCode($code)
{
    $attribute = $this->_productAttributeRepository->get($code);
    return $attribute;
}

You can then call this method like so, e.g. inside a .phtml file

$attrTest = $block->getProductAttributeByCode('test');

Then you can make calls on the attribute object, e.g.

  1. Get options: $attrTest->getOptions()
  2. Get frontend label for each store: $attrTest->getFrontendLabels()
  3. Debug the data array: echo '> ' . print_r($attrTest->debug(), true);

debug: Array ( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [backend_type] => varchar [frontend_input] => text [frontend_label] => Product Manual Download Label [is_required] => 0 [is_user_defined] => 1 [default_value] => Product Manual Download [is_unique] => 0 [is_global] => 0 [is_visible] => 1 [is_searchable] => 0 [is_filterable] => 0 [is_comparable] => 0 [is_visible_on_front] => 0 [is_html_allowed_on_front] => 1 [is_used_for_price_rules] => 0 [is_filterable_in_search] => 0 [used_in_product_listing] => 0 [used_for_sort_by] => 0 [is_visible_in_advanced_search] => 0 [position] => 0 [is_wysiwyg_enabled] => 0 [is_used_for_promo_rules] => 0 [is_required_in_admin_store] => 0 [is_used_in_grid] => 1 [is_visible_in_grid] => 1 [is_filterable_in_grid] => 1 [search_weight] => 1 )

ajmedway
  • 1,492
  • 14
  • 28
  • Any Idea How to load all attributes using productAttributeRepository? I want collection of attributes but with all this options you showed. – Ronak Chauhan Aug 12 '17 at 06:45