Skip to main content
added 438 characters in body
Source Link
Piyush
  • 5.9k
  • 9
  • 34
  • 66

Create a helper function for this

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

public function <func_name>()
{
   $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
   $options = $attribute->getSource()->getAllOptions();
   return $options;
}

Then in your model file'sfile add to the constructor of your helper class an instance like this:

public function __construct(\Namespace\Module\Helper\Helper_name $helper)
{
    $this->helper = $helper;
}

then replace your getAllOptionsgetOptions function you can call the helper function to get attributes all optionswith this :

public function getOptions()
{
    $data = $this->helper-><func_name>();
    $res = [];
    foreach ($data as $index => $value) {
       $res[] = ['value' => $value['value'], 'label' => $value['label']];
    }
    return $res;
}

Create a helper function for this

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

public function <func_name>()
{
   $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
   $options = $attribute->getSource()->getAllOptions();
   return $options;
}

Then in your model file's getAllOptions function you can call the helper function to get attributes all options

Create a helper function for this

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

public function <func_name>()
{
   $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
   $options = $attribute->getSource()->getAllOptions();
   return $options;
}

Then in your model file add to the constructor of your helper class an instance like this:

public function __construct(\Namespace\Module\Helper\Helper_name $helper)
{
    $this->helper = $helper;
}

then replace your getOptions function with this :

public function getOptions()
{
    $data = $this->helper-><func_name>();
    $res = [];
    foreach ($data as $index => $value) {
       $res[] = ['value' => $value['value'], 'label' => $value['label']];
    }
    return $res;
}
Source Link
Piyush
  • 5.9k
  • 9
  • 34
  • 66

Create a helper function for this

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

public function <func_name>()
{
   $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
   $options = $attribute->getSource()->getAllOptions();
   return $options;
}

Then in your model file's getAllOptions function you can call the helper function to get attributes all options