Monday 11 February 2013

Magento: Get Bestselling products by category and date time

This article will show you how to get best selling products. You will see how to get overall best selling products as well as best selling products by category and by date time range.
Here is the code:-

Get overall Bestselling products
public function getBestsellingProducts()
{  
    // number of products to display
    $productCount = 5;
     
    // store ID
    $storeId    = Mage::app()->getStore()->getId();      
     
    // get most viewed products for current category
    $products = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')     
                    ->addOrderedQty()
                    ->setStoreId($storeId)
                    ->addStoreFilter($storeId)                  
                    ->setOrder('ordered_qty', 'desc')
                    ->setPageSize($productCount);
     
    Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);
     
    return $products;
}
Get Bestselling products for current category
public function getBestsellingProducts()
{  
    // number of products to display
    $productCount = 5;
     
    // store ID
    $storeId    = Mage::app()->getStore()->getId();      
     
    // get most viewed products for current category
    $products = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')     
                    ->addOrderedQty()
                    ->setStoreId($storeId)
                    ->addStoreFilter($storeId)
                    ->addCategoryFilter(Mage::registry('current_category'))
                    ->setOrder('ordered_qty', 'desc')
                    ->setPageSize($productCount);                    
     
    Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);
     
    return $products;
}
Get Bestselling products for last 30 days
public function getBestsellingProducts()
{  
    // number of products to display
    $productCount = 5;
     
    // store ID
    $storeId    = Mage::app()->getStore()->getId();
 
    // get today and last 30 days time
    $today = time();
    $last = $today - (60*60*24*30);
 
    $from = date("Y-m-d", $last);
    $to = date("Y-m-d", $today);
     
    // get most viewed products for current category
    $products = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')     
                    ->addOrderedQty($from, $to)
                    ->setStoreId($storeId)
                    ->addStoreFilter($storeId)                  
                    ->setOrder('ordered_qty', 'desc')
                    ->setPageSize($productCount);
     
    Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);
     
    return $products;
}
Note: For Magento version 1.5 and higher, you can also look into the following files to get bestselling products:
- app/code/core/Mage/Sales/Model/Mysql4/Report/Bestsellers.php
- app/code/core/Mage/Sales/Model/Mysql4/Report/Bestsellers/Collection.php
Hope this helps. Thanks.

Magento Admin CSS, JS & URL problem after website transfer

Scenario:
I moved a Magento shop from one sever to another. And, I am having the following problems:-
- The CSS and Javascript files are not loading properly in admin section. However, the frontend section is working fine. When I view the source code, I see that the CSS path is not the full skin path URL. There is adminhtml/default/default/reset.css instead of http://example.com/skin/adminhtml/default/default/reset.css.

- The admin URL is not proper. I am getting repetitive URL. Like index.php/admin/index.php/admin
Solution:
I searched over the internet and found the solution on StackOverflow. The same question is asked on MagentoCommerce Forum. I am just sharing the same solution over here.
The solution is to:-
- open table core_config_data
- correct the value for web/unsecure/base_url and web/secure/base_url
- set the value to 0 for both dev/js/merge_files and dev/css/merge_css_files
However, all these were okay in my core_config_data table.
The final suggestion was to
- clear cache by deleting var/cache directory
& this solved my problem.
Hope it helps. Thanks.