php - Magento - Get Best Sellers Product including Soldout Products -
this code products available in stock. want products, including sold-out. have idea?
$productcount = 5; $storeid = mage::app()->getstore()->getid(); $productsbestsellermens = mage::getresourcemodel('reports/product_collection') ->addorderedqty() ->addattributetoselect('*') ->setstoreid($storeid) ->addcategoryfilter($mensid) ->setpagesize($productcount);
the comment osdave place start finding answer question.
at it's heart magento uses zend_db when accessing database. result can use 'magic' __tostring() method of zend_db_select output underlying mysql query generated php code. category filtering specific situation i've adapted original code slightly:
$productcount = 5; $storeid = mage::app()->getstore()->getid(); $productsbestsellermens = mage::getresourcemodel('reports/product_collection') ->addorderedqty() ->addattributetoselect('*') ->setstoreid($storeid) ->setpagesize($productcount); var_dump((string) $productsbestsellermens->getselect()); exit;
this crude simple way of finding resulting sql query is:
select sum(order_items.qty_ordered) `ordered_qty`, `order_items`.`name` `order_items_name`, `order_items`.`product_id` `entity_id`, `e`.`entity_type_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`sku`, `e`.`has_options`, `e`.`required_options`, `e`.`created_at`, `e`.`updated_at` `sales_flat_order_item` `order_items` inner join `sales_flat_order` `order` on `order`.entity_id = order_items.order_id , `order`.state <> 'canceled' left join `catalog_product_entity` `e` on (e.type_id not in ('grouped', 'configurable', 'bundle')) , e.entity_id = order_items.product_id , e.entity_type_id = 4 (parent_item_id null) group `order_items`.`product_id` having (sum(order_items.qty_ordered) > 0)
as can see reviewing query there nothing filters 'sold out' products.
you can handle display of 'sold out' products using issaleable()
method of mage_catalog_model_product
class. example of in practice shown within magento template file /app/design/frontend/base/default/template/catalog/product/view.phtml
.
it's worth noting when tried use same method pull list of best-sellers found results didn't match audited sales data. fuller investigation , corrected method retrieving best sellers macth auditedsales data available on our blog.
Comments
Post a Comment