magento2 - Filter & get attribute option id & value from product collection -
i have created 4 attributes make, model, variant, year. 4 attributes have unique option_id & value.
in ajax have pass option_id make. need option_id & value model if option_id make exists.
almost have done part, 1 or more values not getting correctly (i.e) gives empty value , have check in back-end shows values.
please check below code.
public function execute() { //$make = $this->getrequest()->getparam('make'); $pro = $this->getrequest()->getparam('pro'); $optiontext = $this->getrequest()->getparam('txt'); $category = $this->_categoryfactory->create()->load($pro); //$attributeid = $this->_eavattribute->getidbycode('catalog_product', 'model'); $attribute = $this->eavconfig->getattribute('catalog_product', 'make'); if ($attribute->usessource()) { $option_id = $attribute->getsource()->getoptionid($optiontext); } $collection = $this->_productcollectionfactory->create() ->addattributetoselect(array('make','model','variant','year')) ->addcategoryfilter($category) ->addfieldtofilter('model', array('neq' => 'null' )) ->addattributetofilter('make', array('in' => $option_id)) ->addstorefilter(0); $_objectmanager = \magento\framework\app\objectmanager::getinstance(); $resource = $_objectmanager->get('magento\framework\app\resourceconnection'); $list2 = array(); foreach ($collection $key => $value) { $list = $value->getdata(); $listmo = $list['model']; $list2[] = $listmo; } if ($list2 != '' && $list2 != null) { $filtermodel = array_unique($list2); $res_arr_values = array(); foreach ($filtermodel $key => $value1) { $fmod = $value1; $query = "select `option_id`,`value` `pro_eav_attribute_option_value` `option_id` in ($fmod) , `store_id`=1 "; $connection = $resource->getconnection(); $result = $connection->fetchall($query); foreach ($result $key => $value2) { $fres = $value2; $res_arr_values[] = $fres; } } //$filteryear = array_unique($list4); $resultjson = $this->_resultjsonfactory->create(); return $resultjson->setdata($res_arr_values); //return $this->_pagefactory->create(); } else { $opt_er = 'error'; $opt_id = array($option_id, $opt_er); $resultjson = $this->_resultjsonfactory->create(); return $resultjson->setdata($opt_id); } }
Comments
Post a Comment