Pages

Wednesday 14 January 2015

Magento::Bestselling products in Home Page

1. Create a new file for Block

Magento_root/app/code/local/Mage/Catalog/Block/Product/Bestseller.php
Bestseller.php
<?php
/**
 * Catalog Product Bestseller Block
 */
class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract
{
    public function getCollection()
    {
        $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');
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        $collection->setPage(1, $this->getLimit());
        return $collection;
    }
}
2. Create a template file for the new block:
Magento_root/app/design/frontend/base/default/template/catalog/product/bestseller.phtml
bestseller.phtml
<?php
/**
 * Bestseller Products block template
 *
 * @see Mage_Catalog_Block_Product_Bestseller
 */
?>
<div class="block block-list block-viewed">
    <div class="block-title">
        <strong><span><?php echo $this->__($this->getHeader()) ?></span></strong>
    </div>
    <div class="block-content">
        <?php $products = $this->getCollection(); ?>
        <?php if (0 < $products->getSize()) { ?>
            <table class="bestseller-table">
                <tr>
                    <?php foreach ($products as $p) { ?>
                        <td style="padding: 15px 15px 0px 15px;">
                            <a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) ?>" class="product-image">
                                <img src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($p->getName()) ?>" />
                            </a>
                            <h3 class="product-name">
                                <a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName())?>">
                                    <?php echo $this->htmlEscape($p->getName()) ?>
                                </a>
                            </h3>
                            <?php echo $this->getPriceHtml($p, true) ?>
                        </td>
                    <?php } ?>
                </tr>
                <tr>
                    <?php foreach ($products as $p) { ?>
                    <td style="padding: 0px 15px 15px;">
                        <?php if($p->isSaleable()): ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($p) ?>')">
                                <span><span><?php echo $this->__('Add to Cart') ?></span></span>
                            </button>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    </td>
                    <?php } ?>
                </tr>
            </table>
        <?php } ?>
    </div>
</div>
3. Now you need to call the created block, to do that go to Backend > CMS > Pages > Edit Page ‘Home page’ and add the following lines of code:
{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml" header="Bestsellers" limit=4}}
Also you can add a new block via layout update:
<reference name="content">
        <block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml" before="-">
            <action method="setLimit"><limit>3</limit></action>
            <action method="setHeader"><header>Best Sellers</header></action>
        </block>
</reference>
For instance, try to add this block to the Category View page here: Backend > Catalog > Manage Categories > Click needed category in the category tree > `Custom Design` horizontal tab > ` Custom Layout Update` field Feel free to manipulate the result using the header and the limit variables.

No comments:

Post a Comment