X
    Categories: Knowledgebase

How to count sale product in Magento 1

– If you want to show stock sold on the product page for the entire duration the product has been active, try this code:


	getSku());
	 $product = Mage::getResourceModel('reports/product_collection')
	 ->addOrderedQty()
	 ->addAttributeToFilter('sku', $sku)
	->setOrder('ordered_qty', 'desc')
	->getFirstItem();

	echo 'Total sale '.(int)$product->ordered_qty; 
	?>

– If you want to how many have been sold of the product for today, try this code:


	getSku());
	$to = $_product->getResource()->formatDate(time());
	$from = $_product->getResource()->formatDate(time() - 60 * 60 * 24 * 1);
	$product = Mage::getResourceModel('reports/product_collection')
	->addOrderedQty($from, $to, true)
	->addAttributeToFilter('sku', $sku)
	->setOrder('ordered_qty', 'desc')
	->getFirstItem();
	echo 'Total Ordered Today '.(int)$product->ordered_qty; 
	?>

– If you want to have it so that it only shows quantity sold on the product page on items that are currently on sale.. use the following., There will also be an alert for customers when there is only 1 remaining in stock, try this code:


	helper('tax')->getPrice($_product, $_product->getFinalPrice());
	$_regularPrice = $this->helper('tax')->getPrice($_product, $_product->getPrice());
	if ($_regularPrice != $_finalPrice){
		$sku = nl2br($_product->getSku());
		$to = $_product->getResource()->formatDate(time());
		$from = $_product->getResource()->formatDate(time() - 60 * 60 * 24 * 1);
		$_productCollection = Mage::getResourceModel('reports/product_collection')
		->addOrderedQty($from, $to, true)
		->addAttributeToFilter('sku', $sku)
		->setOrder('ordered_qty', 'desc')
		->getFirstItem();

		$product = $_productCollection;
		echo 'Already Bought Today '.(int)$product->ordered_qty;

	}

	if ((int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()==1){ ?>

	

ONLY 1 LEFT IN STOCK!

Leo: