1

How to get product Attribute options in Magento 2 in custom module, I need to sort them in alphabetical order.

I have a helper thats called from controller and my helper class is like this

<?php
namespace xxx;

class Admin extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;
    protected $_logger;
    protected $directory_list;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Filesystem\DirectoryList $directory_list)
    {
        parent::__construct($context);
        $this->_objectManager = $context->getObjectManager();
        $this->_logger = $context->getLogger();
        $this->directory_list = $directory_list;
    }


    /**
     * @param string $_attr_code
     * @param bool|false $is_numeric
     */
    public function sortAttributeFilterOptions($_attr_code = '', $is_numeric = false)
    {
        $model = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Eav\Attribute')->setEntityTypeId(4);
        //$model->load($id);
    }
}

2 Answers 2

2

keep below code in your helper file to get all option of attribute in helper file,

class Admin extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,      
        \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
    ) {
        parent::__construct($context);
        $this->eavAttributeRepository = $eavAttributeRepository;
    }
public function sortAttributeFilterOptions($_attr_code = '', $is_numeric = false){
    $attributes = $this->getEavObj($_attr_code);
    $options = $attributes->getSource()->getAllOptions(false);

    $allOptionIds = [];
    $optionLabel = [];
    foreach($options as $value){    
        $optionLabel[ $value['value'] ] = $value['label'];
        $allOptionIds[] = $value['value'];
    }
    print_r($optionLabel);
    return $optionLabel;              
}

public function getEavObj($attributeCode){
    $attribute = $this->eavAttributeRepository->get(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,$attributeCode);
    return $attribute;
}
0

you can apply this code in you helper

use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;

class SomeClass
{
    public function __construct(CollectionFactory $attributeCollectionFactory)
    {
        $this->attributeCollectionFactory = $attributeCollectionFactory->create();
    }
    public fuction getAllOptions() {
      $attributes = $this->attributeCollectionFactory->getItems();
      foreach ($attributes as $attribute) {
        echo $attribute->getId();
        $optionsForSort = array();
        $options = $attribute->getOptions();
        foreach ($options as $option) {
          $optionsForSort[] = $option->getValue();
        }
/** your sort function */
       $this->sortAttributeFilterOptions($optionsForSort);
      }
    }
}

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.