0

I tried to get the attribute option list in my custom block for filter the product collection. But not able to get the option list.

My Code is:

 <?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\Model\ResourceModel\Eav\Attribute $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));   // Here is need to change dynamicaly     
        return $collection;         
    }       
}

In My Controllers

<?php
namespace Sample\Shopbyage\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{   
    protected $resultPageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {

        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {   
        $resultPage = $this->resultPageFactory->create();   
        $data = $this->getRequest()->getPostValue();
        $age = $data['dob'];
        $dateOfBirth = date('Y-m-d', strtotime($age));
        $today = date("Y-m-d");
        $calculatedAge = date_diff(date_create($dateOfBirth), date_create($today));

        $noYear = $calculatedAge->format('%y');
        $noMonth = $calculatedAge->format('%m');
        $noDay = $calculatedAge->format('%d');

        if($noYear>0){
            $ageRange = ($noYear-1).' to '.$noYear.' Years';
        }else if($noMonth>0){
            $ageRange = ($noMonth-1).' to '.$noMonth.' Months';
        }else if($noDay>0){
            $ageRange = '0 to 1 Month';
        }

        $block = $resultPage->getLayout()->createBlock('Sample\Shopbyage\Block\Shopbyage')->setAge($ageRange)->setTemplate('Sample_Shopbyage::products.phtml');
        $this->getResponse()->setBody($block->toHtml());            
    }   
}
  • Can you please explain what are you actually trying to fetch ? all attribute values of product or ? – Vishwas Bhatnagar May 11 '18 at 12:44
  • i need to an age attribute options and id. – Rat May 11 '18 at 12:48
  • Update code how are you loading product from filtered product collection – Vishwas Bhatnagar May 11 '18 at 12:51
  • @VishwasBhatnagar, i updated the code – Rat May 11 '18 at 12:56
  • So what i could understand from code that you are passing the age but not getting the filtered collection ? is that so ? – Vishwas Bhatnagar May 11 '18 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.