1

How to get the list of option values for particular attribute code in magento 2?

<?php
namespace Sample\Shopbyage\Block;                                                                        
use \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;

class Shopbyage extends \Magento\Catalog\Block\Product\AbstractProduct
{

    protected $_productCollectionFactory;   
    protected $catalogProductVisibility;
    protected $urlHelper;
    protected $sqlBuilder;
    protected $imageHelperFactory;
    protected $eavEntityFactory;


    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,      
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
         \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
         \Magento\Widget\Helper\Conditions $conditionsHelper,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\Review\Model\ReviewFactory $reviewFactory, 

        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;  
        $this->catalogProductVisibility = $catalogProductVisibility;
        $this->httpContext = $httpContext;
        $this->sqlBuilder = $sqlBuilder;       
        $this->urlHelper = $urlHelper;
        $this->conditionsHelper = $conditionsHelper;

        parent::__construct($context, $data);

    }

    protected function _prepareLayout()
    {
    }

     public function toHtml()
    {       
        return parent::toHtml();
    }

    public function getShopbyageUrl(){
       return $this->getUrl('shopbyage');
    }
    public function getProductCollection(){     
        $ageValues = $this->getAge();
        $objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
        $coll = $objectManager->create('Magento\Catalog\Api\ProductCustomOptionRepositoryInterfaceAttribute $attributeFactory');
        /*$coll->optionFactory->create();
        $coll->load($ageValues);
        $attrAll = $coll->load()->getItems();
        foreach($attrAll as $st=>$value){
            print_r($value);
        }*/
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('age',array(5,6));        
        return $collection;

    }



}

I have tried above code but not working.

Jeeva Chezhiyan
  • 1,498
  • 3
  • 20
  • 39
Rat
  • 834
  • 13
  • 28
  • check this... https://magento.stackexchange.com/questions/105167/magento-2-how-to-get-attribute-options-value-of-eav-entity/184730 – Jeeva Chezhiyan May 30 '18 at 13:02
  • I have tried above link, but we got the error as "Fatal error: Uncaught TypeError: Argument 10 passed to Hexa\Shopbyage\Block\Shopbyage::__construct() must be an instance of Magento\Eav\Model\Config" – Rat May 30 '18 at 13:10

1 Answers1

0

Try this, Run setup:di:compile command after adding this code.

<?php
namespace Sample\Shopbyage\Block;                                                                        
use \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;

class Shopbyage extends \Magento\Catalog\Block\Product\AbstractProduct
{

    protected $_productCollectionFactory;   
    protected $catalogProductVisibility;
    protected $urlHelper;
    protected $sqlBuilder;
    protected $imageHelperFactory;
    protected $eavEntityFactory;
    protected $eavConfig;



    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,      
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
         \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
         \Magento\Widget\Helper\Conditions $conditionsHelper,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\Review\Model\ReviewFactory $reviewFactory,
        \Magento\Eav\Model\Config $eavConfig,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;  
        $this->catalogProductVisibility = $catalogProductVisibility;
        $this->httpContext = $httpContext;
        $this->sqlBuilder = $sqlBuilder;       
        $this->urlHelper = $urlHelper;
        $this->conditionsHelper = $conditionsHelper;
        $this->eavConfig = $eavConfig;
        parent::__construct($context, $data);

    }

    protected function _prepareLayout()
    {
    }

     public function toHtml()
    {       
        return parent::toHtml();
    }

    public function getShopbyageUrl(){
       return $this->getUrl('shopbyage');
    }
    public function getProductCollection(){     
        $ageValues = $this->getAge();
        $objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
        $coll = $objectManager->create('Magento\Catalog\Api\ProductCustomOptionRepositoryInterfaceAttribute $attributeFactory');
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
        $options = $attribute->getSource()->getAllOptions();
        /*$coll->optionFactory->create();
        $coll->load($ageValues);
        $attrAll = $coll->load()->getItems();
        foreach($attrAll as $st=>$value){
            print_r($value);
        }*/
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('age',array(5,6));        
        return $collection;

    }

}

If this helpful for you means accept the answer..

Jeeva Chezhiyan
  • 1,498
  • 3
  • 20
  • 39