0

I have tried using a single attribute filter and i got it but when applying multiple attributes i am not getting the response.please find the below code that i tried by using multiple attribute.

public function getfilterproducts()
{

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            //============load category object============//
    $categoryid = 7;
    $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
    $categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');     
    $category = $categoryFactory->create()->load($categoryid);
    $productItems = $category->getProductCollection()
    ->addAttributeToSelect('*')
    ->setPageSize(20);


    $filter_arr = array(
        array('attribute'=> 'mobile_color','eq' => "57"),
        array('attribute'=> 'manufacturer','eq' => "65"),


        );
    $productItems = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->addAttributeToFilter($filter_arr);
        // $productItems->setPageSize(20);

    $productItems->setVisibility($this->productVisibility->getVisibleInCatalogIds());

    foreach ($productItems as $product) {
        echo $product->getName()."</br>";


    }     
}
4
  • Have you confirm that you pass proper attribute code and value? And also can you tell me what output you getting from above code? Dec 18, 2017 at 13:35
  • I am not getting any response from the above code...but when i am passing a single attribute like this array('attribute'=> 'mobile_color','eq' => "57") i am getting the product details.
    – christy
    Dec 18, 2017 at 13:39
  • @christy check if you have product for the filtered attribute combination?
    – Piyush
    Dec 18, 2017 at 13:49
  • @Piyush yes i have checked ...
    – christy
    Dec 18, 2017 at 14:04

1 Answer 1

0

Please try below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//============load category object============//
$categoryid = 7;
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$category = $categoryFactory->create()->load($categoryid);
$productItems = $category->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('mobile_color', 57)
    ->addAttributeToFilter('manufacturer', 65)
    ->setPageSize(20);

foreach ($productItems as $product) {
    echo $product->getName()."==";
}
1
  • I am not getting any response..
    – christy
    Dec 18, 2017 at 14:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.