1

I am trying to filter the productCollection using attribute and values arrays, but it it showing error, here below is my code. Please, let me know how it can be done...

$requestParams = $this->getRequest()->getParam('requestParams');
$shoppingBy = $requestParams["shoppingBy"];

$attributeCodes = array_keys($shoppingBy);

$productCollection = Mage::getModel("catalog/category")
->load($requestParams['categoryId'])
->getProductCollection();

foreach($attributeCodes as $attributeCode) {
    $attributeValue = $shoppingBy[$attributeCode];
    $filter = array('attribute' => $attributeCode, 'in' => $attributeValue);
    $productCollection = $productCollection->addAttributeToFilter($filter);
}

1 Answer 1

1

Edited:

I am not sure 100% to use in for all attribute .Checkout this link for magento collection operator and more details https://magento2.atlassian.net/wiki/display/m1wiki/Using+Magento+1.x+collections

Change:

$productCollection = Mage::getModel("catalog/category")
->load($requestParams['categoryId'])
->getProductCollection();

to

$category= Mage::getModel("catalog/category")
->load($requestParams['categoryId']);
$productCollection =$category->getProductCollection();

And Change

foreach($attributeCodes as $attributeCode) {
    $attributeValue = $shoppingBy[$attributeCode];
    $filter = array('attribute' => $attributeCode, 'in' => $attributeValue);
    $productCollection = $productCollection->addAttributeToFilter($filter);
}

to

$filter=array();
foreach($attributeCodes as $attributeCode) {
    $attributeValue = $shoppingBy[$attributeCode];
    $filter[] = array('attribute' => $attributeCode, 'in' => $attributeValue);
}

$productCollection = $productCollection->addAttributeToFilter($filter);

use : echo $productCollection->getSelect()->__toString(); for getting the query string..

4
  • Sorry, it didn't work. Jun 7, 2016 at 8:56
  • It is not having any product in its collection Jun 7, 2016 at 9:25
  • i guss that your issue at in condition,Please check poroperly
    – Amit Bera
    Jun 7, 2016 at 9:58
  • The data I have sent from AJAX here is JSON, and somewhat looks like this => {"requestParams":{"productCount":8,"categoryId":"6","sortOption":"2672","sortDirection":"asc","productLimit":8,"shoppingBy":{"no_of_poles":["3 Pole"]}}}. I have even logged the in values and they are proper. The logged value is Array([0] => 3 Pole( Jun 7, 2016 at 10:00

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.