Reputation: 517
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description'))
->addStoreFilter($storeId)
->setPageSize(4)
->setCurPage(1)
->setOrder('ordered_qty', 'desc')->load();
Bestsellers code not working in Magento 1.6.2. The collection is not even filtered by store.
Zend_Debug::dump($storeId)
; gives me string '2' (length=1)
but I can not execute Zend_Debug::dump($products->getSelect());
because it gives me an error like There has been and error processing your request
and it give s me the following query:
SELECT SUM(order_items.qty_ordered) AS ordered_qty, order_items.name AS order_items_name, order_items.product_id AS 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, e.name, e.price, e.small_image, e.short_description FROM tp_sales_flat_order_item AS order_items INNER JOIN tp_sales_flat_order AS order ON order.entity_id = order_items.order_id AND order.state <> 'cancelled' LEFT JOIN tp_catalog_product_entity AS e ON (e.type_id NOT IN ('grouped', 'configurable', 'bundle')) AND e.entity_id = order_items.product_id AND e.entity_type_id = 4 WHERE (parent_item_id IS NULL) GROUP BY order_items.product_id HAVING (SUM(order_items.qty_ordered) > 0) ORDER BY ordered_qty desc LIMIT 4
Please help me.
Upvotes: 0
Views: 1452
Reputation: 167
Try this:
<?php
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty($from, $to, true)
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addCategoryFilter($category)
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc');
?>
Upvotes: 1
Reputation: 2495
Try to set the storeid for your collection. Insert this line after adding OrderedQty:
->setStoreId($storeId)
Upvotes: 0
Reputation: 1
i was also face this type of issues in magento 1.6.2. i found that there is some problem in attribute filter while i am filtering collection.
i have replace addAttributeToFilter with addFieldToFilter and it's work.
Try this may be it will be helpful for your
Upvotes: 0
Reputation: 66
Try this:
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrdersCount()
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description'))
->addStoreFilter($storeId)
->setPageSize(4)
->setCurPage(1)
->setOrder('orders', 'desc')->load();
The changes are the addOrdersCount() method, and the ->setOrder() method.
Upvotes: 0