How to get product attribute parameters by attribute code in Magento 2.0

I am trying to get a list of attributes of a drop-down list and check if this value exists (if I need to get the value and assign it to the product), and if it is not created, it will have to create it and get its value in order 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()); 
+5
source share
5 answers

Following the recommendations of Magento 2, you should not use ObjectManager yourself. Instead, you should use dependency injection. More here

In your block / controller / helper ... create a constructor and enter the class \Magento\Catalog\Model\Product\Attribute\Repository . For instance:

 /** * @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 in your highlighted method you want to call (PHPDoc added for clarity):

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

Now you can get the parameter values ​​and labels:

 foreach ($manufacturerOptions as $manufacturerOption) { $manufacturerOption->getValue(); // Value $manufacturerOption->getLabel(); // Label } 
+16
source
 <?php echo $_product->getResource()->getAttribute('movement')->getFrontend()->getValue($_product);?> 
Product

$ _ is a product object. The above code returns the value of the attribute name of the motion attribute.

+2
source

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

 /** * @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; } 

Then you can call this method like this inside the .phtml file

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

Then you can make calls to the attribute object, for example.

  • Get parameters: $attrTest->getOptions()
  • Get interface labels for each store: $attrTest->getFrontendLabels()
  • Debugging a dataset: echo '> ' . print_r($attrTest->debug(), true); 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] => Download Guide Download shortcut [is_required] => 0 [is_user_defined] => 1 [default_value] => Download Guide 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 0] => 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)

0
source

Using the API service level, for the EAV attribute of any entity type, enter the service data element in the constructor as follows.

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

And you can get the attribute using this.

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

To get an array of attribute parameter values, use this.

 $options = $attribute->getSource()->getAllOptions(); 
0
source

The code below is useful for finding specific attribute values. Like here, the color refers to my attribute, using the code below we can get that all the colors are mapped to this attribute.

 $attributeId = Mage::getResourceModel('eav/entity_attribute')>getIdByCode('catalog_product','color'); $collection =Mage::getResourceModel('eav/entity_attribute_option_collection')>setPositionOrder('asc')->setAttributeFilter($attributeId)->setStoreFilter(0)->load(); print_r($collection->getData()); 
-7
source

Source: https://habr.com/ru/post/1239058/


All Articles