Friday 8 July 2011

how to get best selling product in magento?

Best Seller is a common module of an eCommerce solution. but it’s not a default module in magento.
u want to display the best selling products in your Magento store on the frontpage or anywhere else in your store? i found this snippets @MagentoForum, it’s easy to use…

[Step 1] Create a Bestseller.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Bestseller.php

class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $products = 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_visibility')->addVisibleInCatalogFilterToCollection($products);
 
        $products->setPageSize(3)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

[Step 2] Create a bestseller.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/bestseller.phtml

getProductCollection()) && $_products->getSize()): ?>

__('Best Seller Products') ?>

getItems()) ?> getItems() as $_product): ?>
>


htmlEscape($_product->getName()) ?>


<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>

getRatingSummary()): ?>
getReviewsSummaryHtml($_product, 'short') ?>

getPriceHtml($_product, true) ?>
isSaleable()): ?>


__('Out of stock') ?>


getevent_date()) {echo $_product->getevent_date();} ?>

[Step 3] now put this line where you want to view best selling products..

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

Source : http://harisur.net/how-to-get-best-selling-product-in-magento

how to get mostviewed products in magento?

[Step 1] Create a Mostviewed.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Mostviewed.php

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
 
        $products->setPageSize(5)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

[Step 2] Create a mostviewed.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/mostviewed.phtml

getProductCollection()) && $_products->getSize()): ?>
__('These Products Are Popular Right Now!') ?>
getItems()); echo $_collectionSize; ?>

[Step 3] now put this line where you want to view mostviewed products..

{{block type="catalog/product_mostviewed" template="catalog/product/mostviewed.phtml"}}

Source : http://harisur.net/how-to-get-mostviewed-products-in-magento

how to get onsale products in magento?

[Step 1] Create a Onsale.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Onsale.php

class Mage_Catalog_Block_Product_Onsale extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
            ->addAttributeToFilter('special_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToSort('special_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1);
        $this->setProductCollection($collection);
    }
}

[Step 2] Create a onsale.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/onsale.phtml

getProductCollection()) && $product_->getSize()): ?>
__('On Sale Now!') ?>
getItems()); echo $_collectionSize; ?>

[Step 3] now put this line where you want to view onsale products..

{{block type="catalog/product_onsale" template="catalog/product/onsale.phtml"}}

Source : http://harisur.net/how-to-get-onsale-products-in-magento

Monday 6 June 2011

how to get the currency code for current store in magento?

// Currency Code for Bangladesh is "BDT"
Mage::app()->getStore()->getCurrentCurrencyCode();

Source : http://harisur.net/how-to-get-the-currency-code-for-current-store-in-magento

how to get the currency symbol for current store in magento?

// Currency Symbol for Bangladesh is "Tk"
Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();

Source : http://harisur.net/how-to-get-the-currency-symbol-for-current-store-in-magento

how to define the product price with default currency format in mageto?

normally $_product->getPrice() method return the product price in basic number format with 4 digits after decimal point, it’s look like 123.4567.
But the default currency format of magento is $123.46. see, default currency format done round with 2 digits after decimal point. and another important thing is, it’s put the currency code before the price.
lets see how to do that…
echo $_product->getPrice();
// will display : 123.4567
echo Mage::helper('core')->currency($_product->getPrice());
// will display : BDT123.46 [BDT, currency code of Bangladesh]

Source : http://harisur.net/how-to-define-the-product-price-with-default-currency-format-in-mageto