I created a module and a new block so that I can overwrite \Magento\Catalog\Block\Product\ListProduct.

My catalog consists of configurable products, and I need to filter based on attributes assigned to the child simple products. In initializeProductCollection() I am checking for a query string (ie: colour=123), and if set creating a list of all simple products with the matching attribute. I then find all of the parent products and use that list of ids to filter the main product collection, ie:

$collection->addAttributeToFilter('entity_id', array('in' => $parentIds));

However, the output is not quite right. If I

echo $collection->getSelect();

I can see a second set of entity ids which appears to just be the first 12 items in the root category, so its effectively finding items with an entity ID that matches my list of parent ids (good!) AND ids that match the first 12 items from the root catalog (bad!).

The resulting query is something like:

SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, IFNULL(review_summary.reviews_count, 0) AS `reviews_count`, IFNULL(review_summary.rating_summary, 0) AS `rating_summary` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.customer_group_id = 0 AND price_index.website_id = '1' LEFT JOIN `review_entity_summary` AS `review_summary` ON e.entity_id = review_summary.entity_pk_value AND review_summary.store_id = 1 AND review_summary.entity_type = (SELECT `review_entity`.`entity_id` FROM `review_entity` WHERE (entity_code = 'product')) WHERE (e.entity_id IN (399, 404, 415, 430, 442, 448, 453, 458, 462, 466, 468, 475)) AND (`e`.`entity_id` IN('453', '458', '527', '701', '730', '751', '773', '779', '787', '794', '815', '841', '852', '885', '892', '915', '922', '943')) ORDER BY FIELD(e.entity_id,399,404,415,430,442,448,453,458,462,466,468,475)

You can see the WHERE statement has two sets of ids - I basically need to ignore the first set and only have the latter.

Your Answer

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

Browse other questions tagged or ask your own question.