Showing “Hot”, “New” and “Sale” Badges on Magento Products
Small badges on product images help shoppers spot bestsellers, new items and discounts at a glance. In Magento 1 you can work all three out from data you already have.
Step 1: collect bestseller SKUs
Add this at the top of your list template. It builds an array of SKUs ordered by quantity sold:
<?php
/* Best Seller */
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc'); // most best sellers on top
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); //filter only active products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //filter only visible in catalog products
$bestseller = array();
foreach ($collection as $_product){
$bestseller[] = $_product->getSku();
}
/* Best Seller */
?>
Step 2: output the badges
Inside the product loop, check the new-from dates, compare the regular and final price, and look the SKU up in the bestseller list:
<?php
/* NEW */
$newProduct = ''; $onsaleProduct = '';
$newFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsFromDate();
$newToDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsToDate();
$now = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
if(($newFromDate< $now && $newFromDate != NULL) && ($newToDate> $now || $newToDate == '')){
$newProduct = "<div class='product_icon'>
<img class='new' alt='' src='".$this->getSkinUrl('images/new_product_img.png') ." ' />
</div>";
}
?>
<?php
/* SALE */
if ($_product->getPrice() > $_product->getFinalPrice()){
$onsaleProduct = "<div class='product_icon'>
<img class='onsale' alt='' src='".$this->getSkinUrl('images/sale_product_img.png') ." ' />
</div>";
} ?>
<?php echo $newProduct; ?>
<?php echo $onsaleProduct; ?>
<?php if(in_array($_product->getSku(),$bestseller)){ ?>
<div class="product_icon">
<img class="hot" src="<?php echo $this->getSkinUrl('images/hot_product_img.png'); ?>" alt="">
</div>
<?php } ?>
Upload your badge images to the skin folder and position them with CSS. On large catalogues, limit the bestseller query to keep pages quick.
Heads up: this guide applies to Magento 1, which stopped receiving official security updates in June 2020. If your store still runs it, plan a move to Magento 2 or Adobe Commerce soon. We can help with the migration.
Want this done for you? Find out more about our Magento development service.