
Magento 2 product attributes are generally the properties of the Magento 2 product that help customers choose between product variations and find the best suitable option. It could be color, size, weight, material, etc.
To learn more about creating and managing product attributes, see:
There are two types of attributes used by Magento 2:
There are 2 ways to add new product attributes in Magento 2:
Under Attribute properties, describe basic information of the product attributes Magento 2.
Following are Catalog Input Types to define the product attributes Magento 2.
Using the below codes can Get All Options Of Attribute By Attribute Code In Magento 2.
<?php
namespace Dolphin\Mymodule\Ui\Component\Form\Color;
use Magento\Framework\Data\OptionSourceInterface;
use \Magento\Eav\Model\Config;
class Options implements OptionSourceInterface
{
/**
* @var Config
*/
protected $_eavConfig;
/**
* @param Config $eavConfig
*/
public function __construct(
Config $eavConfig
)
{
$this->_eavConfig = $eavConfig;
}
/**
* {@inheritdoc}
*/
public function toOptionArray()
{
$coloOptions = $this->getColorOption();
return $coloOptions;
}
protected function getColorOption()
{
$attribute = $this->_eavConfig->getAttribute('catalog_product', 'color'); //color is product attribute.
$options = $attribute->getSource()->getAllOptions();
$optionsExists = array();
/*foreach ($options as $option) {
if ($option['value'] > 0) {
$optionsExists[] = $option['label'];
}
}*/
return $options;
}
}Checkout: hire Magento 2 developer
Result:
Array
(
[0] => Array
(
[label] =>
[value] =>
)
[1] => Array
(
[value] => 49
[label] => Black
)
[2] => Array
(
[value] => 50
[label] => Blue
)
[3] => Array
(
[value] => 51
[label] => Brown
)
[4] => Array
(
[value] => 52
[label] => Gray
)
[5] => Array
(
[value] => 53
[label] => Green
)
)I Hope, This instruction will be helpful for you.
If you have any difficulties regarding this blog, do consider them posting in the Comments section below!
I’m here to help.
Thank you!
Click one of our contacts below to chat on WhatsApp