2

I'm currently building a custom product filter via Ajax. I've got the basics working but now I'm trying to filter via category and a specific product attribute.

Here is my code

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');

$categoryData = $this->getRequest()->getParam('whoisitfor');

// Get category selected.
if(isset($categoryData)) {
    $categoryData = $this->expandFormValues($categoryData);
    $categories = [$categoryData];//category ids array
    $collection->addCategoriesFilter(['in' => $categories]);
}

This get's the products for the specific categories that are selected on the frontend, this works fine.

Now when I try and filter it by a specific product attribute it still just returns all the products from the category above not the specific products with the attribute filtered on.

Here is the code:

$collection->addAttributeToFilter('user_experience_level', array('eq' => 3003));

Full code looks like this:

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');

$categoryData = $this->getRequest()->getParam('whoisitfor');

// Get category selected.
if(isset($categoryData)) {
    $categoryData = $this->expandFormValues($categoryData);
    $categories = [$categoryData];//category ids array
    $collection->addCategoriesFilter(['in' => $categories]);
}

$collection->addAttributeToFilter('user_experience_level', array('eq' => 3003));

If anyone can help me with this, it'd be great.

Thanks.

0

you can try this code.

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');

$categoryData = $this->getRequest()->getParam('whoisitfor');

// Get category selected.
if(isset($categoryData)) {
$categoryData = $this->expandFormValues($categoryData);
$categories = [$categoryData];//category ids array
$collection->addCategoriesFilter(['in' => $categories]);

}

$collection->addFieldToFilter('user_experience_level', array('eq' => 3003));

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.