1

We are creating a bestseller block and use the code below. With the code below we get a nice list of products. The only problem is that many of these products are either very old - and hardly ever sold. It almost seems that, either:

a. reports/product_collection contains very old, not actual data

b. the results are sorted the wrong way around (doesn't seem so)

c. something else? UPDATE: it seems the output is SIMPLE producs only - and we sell configurable products in our fashion store. This is why only some products are showing: these are the only simple products we have.

question: How can we get the 'real' bestsellers in the code below?

help appreciated (Magento 1.9.2.4)

public function getCollection()
{
    $storeId = Mage::app()->getStore()->getId();
    $collection = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->setOrder('ordered_qty', 'desc');

    if (Mage::helper('catalog/product_flat')->isEnabled()) {
        $collection->getSelect()
            ->joinInner(array('e2' => 'catalog_product_flat_' . $storeId), 'e2.entity_id = e.entity_id');
    } else {
        $collection->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image'));
    }

    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

    $_limit = (int)$this->getLimit() ?: 3;
    $collection->setPage(1, $_limit);
    return $collection;
}

1 Answer 1

0

Don't know why you are getting this old products. But you might try this solution is working nicely for me:

public function getBestsellerProducts()
{
    $storeId = (int) Mage::app()->getStore()->getId();

    // Date
    $date = new Zend_Date();
    $toDate = $date->setDay(1)->getDate()->get('Y-MM-dd');
    $fromDate = $date->subMonth(1)->getDate()->get('Y-MM-dd');

    $collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addStoreFilter()
        ->addPriceData()
        ->addTaxPercents()
        ->addUrlRewrite()
        ->setPageSize(6);

    $collection->getSelect()
        ->joinLeft(
            array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
            "e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId} AND aggregation.period BETWEEN '{$fromDate}' AND '{$toDate}'",
            array('SUM(aggregation.qty_ordered) AS sold_quantity')
        )
        ->group('e.entity_id')
        ->order(array('sold_quantity DESC', 'e.created_at'));

    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    return $collection;
}

Full article at: http://inchoo.net/magento/bestseller-products-in-magento/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.