search
top

Product Quantities Ordered in Magento

I am explaining here the way to display the total quantities ordered in the Product view page.

I have used Magento version 1.7.0.2

1) Block file

Create a file app/code/local/Mage/Catalog/Block/Product/View/Quantity.php (note the codepool is ‘local’ )

Copy this code in the file and save the file.

class Mage_Catalog_Block_Product_View_Quantity extends Mage_Catalog_Block_Product_View_Abstract
{
    /**
     * Retrieve quantity ordered
     *
     * @return integer
     */
    public function getOrderedQuantity()
    {    
    	$product_id = $this->getProduct()->getId();
    
		$report = Mage::getResourceModel('reports/product_sold_collection')
		->addOrderedQty()				
		->addAttributeToSelect(array('name','id'))
		->addFieldToFilter('entity_id',$product_id)
		->getFirstItem();
	
		return intval($report->getOrderedQty());
	}
}

2) View File

Create a file app/code/design/frontend/default/default/template/catalog/product/view/quantity.phtml

I have used package – default and theme – default, this can be as per your structure

Copy this code in the file and save the file.

<?php echo 'Qty->'.$_qty = $this->getOrderedQuantity() ?>

3) Layout File

If you dont have ‘local.xml’ file for your theme, create one at app/code/design/frontend/default/default/layout/local.xml

Copy this code in the file and save the file.

<layout version="0.1.0">
<!--  below layout for displaying the quantities ordered in product view page -->
    <catalog_product_view>  
    	<reference name="content">	    	   		
	            <block type="catalog/product_view_quantity" name="catalog.product.quantity" before="-" template="catalog/product/view/quantity.phtml"/>     
	    </reference>  
     </catalog_product_view>   
</layout>

And your are done, hope this is helpful

Leave a Reply

top