|
Alright, I looked around on the forums for a way to list the best sellers (by actual product sales). This is a workaround and should in no way at all be the permanent solution.
First, follow the tutorial for listing featured products on the home page. (dan_w’s tutorial)
1. Do the same thing you would for a featured block, but make it a bestselling block. Heres what needs changed initially:
Homepage.php (change to Bestsellers.php)
class Mage_Catalog_Block_Product_Homepage extends Mage_Catalog_Block_Product_Abstract
change this to:
class Mage_Catalog_Block_Product_Bestsellers extends Mage_Catalog_Block_Product_Abstract
2. Change homepage.phtml to bestselling.phtml
3. Add your block to the correct page:
{{block type="catalog/product_bestsellers" name="home.catalog.product.bestsellers" alias="product_bestsellers" template="catalog/product/bestsellers.phtml"}}
4. Make an attribute named sales_rank (text area) and add it to your default attribute set.
Now that you have everything set up, I feel I should warn you: The best selling script will make your pages load slow. I suggest making it a cronjob and run it nightly instead of running it at every page load. Basically, this script goes through all of the orders, checks the items, talys the product sales, assigns sales to an array of productIDs, and then loads products based on that ID. After that, a custom attribute is set (sales_rank) and the product is saved. Then we use our sales_rank to sort the products.
$SalesPerProduct = array(); $i=1; while(!isset($End)){
$order = Mage::getModel('sales/order')->load($i); $items = $order->getAllItems(); if(empty($items)){$End='now';} //$ids = array_keys($items); //$skus = array(); foreach ($items as $itemId => $item) { $itemId = $item->getProductId(); if(!isset($SalesPerProduct[$itemId])) { $SalesPerProduct[$itemId] = 0; } $SalesPerProduct[$itemId]++; } $i++;
} arsort($SalesPerProduct); foreach ($SalesPerProduct as $ProductID => $Sales) { $ProductForUpdate = Mage::getModel('catalog/product')->load($ProductID); $ProductForUpdate->setSalesRank($Sales); $ProductForUpdate->save(); //print_r($ProductForUpdate); } /*END*/
This is the code that updates all of the products. Put it where you like - but I suggest making it a nightly cronjob (so your best sellers will only update daily, but this isn’t a big deal).
Next, delete this line in Bestselling.php:
$products->setOrder('hot_deals')->setPageSize(5)->setCurPage(1);
Now, find this code:
$products = $product->setStoreId($storeId)->getCollection() ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner') ->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left') ;
And add an extra line to it, as so:
$products = $product->setStoreId($storeId)->getCollection() ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner') ->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left') ->setOrder('sales_rank', 'DESC') ;
This was a spur of the moment thing and I am sure there is a better way. Please comment back with suggestions!
|