Abhilash
Abhilash

Reputation: 206

Magento And- OR query

We are having two attributes check_in_stock and show_in_front. I would like to have magento equivalent query for : where (check_in_stock > 0 OR (check_in_stock=0 and show_in_front=1)).

Please help me to find the equivalent magento query

Upvotes: 2

Views: 136

Answers (2)

Dima
Dima

Reputation: 1

Greetings from Itera Research,

Here I am happy to share reply from our Magento team lead, please check below:

"First you need to lookup visibility and status attributes ids

select attribute_id from eav_attribute where attribute_code = 'visibility';

in my case it returns 102

select attribute_id from eav_attribute where attribute_code = 'status';

in my case it returns 96

and substitute these ids to the following query

SELECT e.*, at_qty.qty, at_status.value AS status, at_visibility.value AS visibility FROM catalog_product_entity AS e LEFT JOIN cataloginventory_stock_item AS at_qty ON (at_qty.product_id=e.entity_id) AND (at_qty.stock_id=1) INNER JOIN catalog_product_entity_int AS at_status ON (at_status.entity_id = e.entity_id) AND (at_status.attribute_id = '96') AND (at_status.store_id = 0) INNER JOIN catalog_product_entity_int AS at_visibility ON (at_visibility.entity_id = e.entity_id) AND (at_visibility.attribute_id = '102') AND (at_visibility.store_id = 0) inner join cataloginventory_stock_status as ss on (ss.product_id = e.entity_id) where at_visibility.value = 1 and ss.stock_status = 1

Hope this helps,

if not, please ping me a message,

Jen Zorina

Itera Research

Upvotes: 0

imso077
imso077

Reputation: 311

Well you can use the SQL queries like:

Mage::getModel("catalog/product")->getCollection()->getSelect()->where('check_in_stock > 0 OR (check_in_stock=0 and show_in_front=1)')

This wil lcreate an sql output like:

"SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (check_in_stock > 0 OR (check_in_stock=0 and show_in_front=1))"

Upvotes: 1

Related Questions