10/31/2011

How To Display Best Selling Products On Magento Store

To promote best selling products on your site Magento use so many way to show bestseller product, One of them I am going to show. Below code will fetch the all the list of all product which have already sell.
<?php
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->setOrder('ordered_qty', 'desc');
?>

This is the collection of all the product which already have sell.

Now use foreach loop to show some number of product and it details. I wish to show only the name of the product which has sell most. And I will show on the left sidebar of the page.
<div class="block best-seller">
<div class="right-title-bg"><h3>Best Selling Product</h3></div>
<div class="block-content">
<ul class="best-selling-item">
$count = 0;
foreach($_productCollection as $product):
$id = $product->getId();
$prod = Mage::getModel('catalog/product')->load($id);?>
<li><a href="<?php echo $prod->getProductUrl();?>" title="<?php echo $prod->getName();?>"><?php echo $prod->getName();?></a></li>
<?php
$count++ ;
if($count==$totalPerPage)
break;
endforeach;
?>
</ul>
</div>
</div>

If you wish to show all the details of the product then you also can do that as you will get the product Id from $prod->getId(); and from product Id you can fetch each and every details of the product

10/29/2011

How to print product collection query in magento

Let you have write a product collection with some attribute to select some product, but you are not getting proper result then you must need to print the sql query to see whether you have write the correct syntax in magento default collection. then write the below code to get the sql query .

<?php
$productcollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('bestseller', array('eq' => 1))
->addAttributeToSelect('*');
echo $productcollection->getSelect()->assemble();
?>