1

I'm new to magento and I have some troubles. I've managed to filter products by style or color, which are configurable products, but I can't figure out how to filter by size, where products are simple products.

Here's the code I use for style and color:

    if (isset($_GET['style'])) {
        $this->addAttributeToFilter('style', array('eq' => $_GET['style']));


    if (isset($_GET['solid'])) {
         $this->addAttributeToFilter('solid', array('eq' => $_GET['solid']));
     }

I get attributute name and ID from URL. My filter show styles and color only if they are available for products inside the filtered category, but it always show all sizes and when I try to filter any of them, nothing is returned. Also, there is a problem when I select for example more than 1 style to filter. I should get 2 products with two styles, but instead I get only one (first one).

  • $this is your product collection? – Sukumar Gorai Nov 20 '18 at 8:26
  • Yes. Sorry for not mentioning it. – dext3rr Nov 20 '18 at 8:26
  • Your question is not clear as sometimes you says color, size and somewhere you saying solid and style? What do you want to filter exactly? – Sukumar Gorai Nov 20 '18 at 8:29
  • I want to filter sizes. Colors (attribute name "solid") and styles works. – dext3rr Nov 20 '18 at 8:33
0

You can try the following code instead of yours:

$this->addAttributeToSelect('size');

if (isset($_GET['style'])) {
    $this->addAttributeToFilter('style', array('eq' => $_GET['style']));
}

if (isset($_GET['solid'])) {
     $this->addAttributeToFilter('solid', array('eq' => $_GET['solid']));
}

if (isset($_GET['size'])) {
     $this->addAttributeToFilter('size', array('eq' => $_GET['solid']));
}

If you want to filter the attributes by multiple options then you can try the below code:

$this->addAttributeToFilter('my_attribute_code', array('in' => array(12,10))); // 12, 10 is option ids
  • I already tried this method. Doesn't work. Works for configurable products, but not for simple products. – dext3rr Nov 20 '18 at 9:05
  • Did you check if your simple products have the correct options selected? – Sukumar Gorai Nov 20 '18 at 9:07
  • Yes. Every simple product has a size attribute with selected size. – dext3rr Nov 20 '18 at 9:15

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.