I am trying to filter my product collection using Range for a drop down attribute.
Attribute name – btu_input_exact
Here is my code to fetch product collection
$productCollection = $collectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToSelect('btu_input_exact')
->addAttributeToFilter('manufacturer', ['in' => $brands])
->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
And now this is the code to add range filter
$attributeCode = 'btu_input_exact';
// Set the values for filtering
$minValue = '110,000'; // Example minimum value
$maxValue = '130,000'; // Example maximum value
// Add filters to the product collection
$productCollection->addAttributeToFilter(
$attributeCode,
['gteq' => $minValue]
);
$productCollection->addAttributeToFilter(
$attributeCode,
['lteq' => $maxValue]
);
This is the query that I get when I use echo $productCollection->getSelect();
SELECT `e`.*, `at_manufacturer`.`value` AS `manufacturer`, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, `at_btu_input_exact`.`value` AS `btu_input_exact` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_int` AS `at_manufacturer` ON (`at_manufacturer`.`entity_id` = `e`.`entity_id`) AND (`at_manufacturer`.`attribute_id` = '83') AND (`at_manufacturer`.`store_id` = 0) INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`entity_id` = `e`.`entity_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1) INNER JOIN `catalog_product_entity_int` AS `at_btu_input_exact` ON (`at_btu_input_exact`.`entity_id` = `e`.`entity_id`) AND (`at_btu_input_exact`.`attribute_id` = '208') AND (`at_btu_input_exact`.`store_id` = 0) WHERE (at_manufacturer.value IN('21321', '21289')) AND (IF(at_status.value_id > 0, at_status.value, at_status_default.value) = '1') AND (at_btu_input_exact.value >= '110,000') AND (at_btu_input_exact.value <= '130,000')
This query has the condition at the end but it is not fetching the data. I have made sure that I have a product withing this range.
I feel this is happening as this attribute is drop down attribute and the values are saved as option_ids. here is the option_id values for the above values.
So I tried using option_ids in my code. Example code using option_ids
$attributeCode = 'btu_input_exact';
// Set the values for filtering
$minValue = '6487'; // Example minimum value
$maxValue = '6428'; // Example maximum value
// Add filters to the product collection
$productCollection->addAttributeToFilter(
$attributeCode,
['gteq' => $minValue]
);
$productCollection->addAttributeToFilter(
$attributeCode,
['lteq' => $maxValue]
);
echo $productCollection->getSelect();die;
This is the query that I get
SELECT e
.*, at_manufacturer
.value
AS manufacturer
, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS status
, at_btu_input_exact
.value
AS btu_input_exact
FROM catalog_product_entity
AS e
INNER JOIN catalog_product_entity_int
AS at_manufacturer
ON (at_manufacturer
.entity_id
= e
.entity_id
) AND (at_manufacturer
.attribute_id
= ’83’) AND (at_manufacturer
.store_id
= 0) INNER JOIN catalog_product_entity_int
AS at_status_default
ON (at_status_default
.entity_id
= e
.entity_id
) AND (at_status_default
.attribute_id
= ’97’) AND at_status_default
.store_id
= 0 LEFT JOIN catalog_product_entity_int
AS at_status
ON (at_status
.entity_id
= e
.entity_id
) AND (at_status
.attribute_id
= ’97’) AND (at_status
.store_id
= 1) INNER JOIN catalog_product_entity_int
AS at_btu_input_exact
ON (at_btu_input_exact
.entity_id
= e
.entity_id
) AND (at_btu_input_exact
.attribute_id
= ‘208’) AND (at_btu_input_exact
.store_id
= 0) WHERE (at_manufacturer.value IN(‘21321’, ‘21289’)) AND (IF(at_status.value_id > 0, at_status.value, at_status_default.value) = ‘1’) AND (at_btu_input_exact.value >= ‘6487’) AND (at_btu_input_exact.value <= ‘6428’)
But still the productCollection is empty. I know this could be happening because of comma in my values and also it is a drop down attribute. But I need a way to filter productCollection for this.
I can try to fetch the complete productCollection and remove the products according to my condition manually. But I have a lot of products on my website and this will make the process too slow.
Could someone suggest a way to filter productCollection according to above condition.