Sunday, February 6, 2011

Magento - How to create a special price page? (with new products first)



I've created a Magento site for a client who wants to have a special price page, wherein she could put products having special price/discounted price..
Requirements:
  • app/code/local/Mage/Catalog/Block/Product/Special.php
  • app/design/frontend/default/donna/template/catalog/product/special.phtml
  • Magento backend - CMS>Pages>create-a-special-price-page
Special.php
=========================================
<?php
class Mage_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_List
{
   function get_prod_count()
   {
      //unset any saved limits
      Mage::getSingleton('catalog/session')->unsLimitPage();
      return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 9;
   }// get_prod_count
   function get_cur_page()
   {
      return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1;
   }// get_cur_page
   /**
    * Retrieve loaded category collection
    *
    * @return Mage_Eav_Model_Entity_Collection_Abstract
   **/
   protected function _getProductCollection()
   {
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        $tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
        $dateTomorrow = date('m/d/y', $tomorrow);
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
        $collection = $this->_addProductAttributesAndPrices($collection)
         ->addStoreFilter()
         ->addAttributeToSort('entity_id', 'desc') //THIS WILL SHOW THE LATEST PRODUCTS FIRST
         ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
         ->addAttributeToFilter('special_to_date', array('or'=> array(0 => array('date' => true, 'from' => $dateTomorrow), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')
         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());
        $this->setProductCollection($collection);
        return $collection;
   }// _getProductCollection
}// Mage_Catalog_Block_Product_New
?> 
==============================================

special.phtml
==============================================
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="widget widget-new-products">
    <div class="widget-title">
        <h2><?php echo $this->__('Special Product') ?></h2>
    </div>
    <div class="widget-products">
    <?php $_columnCount = $this->getColumnCount(); ?>
        <?php $i=0; foreach ($_products->getItems() as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image') ?>" width="195px" height="195px" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                    <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                    <!-- ###### BRANDS EG. BY CHIC ON A MISSION ###### -->
                    <div class="product-brand"><?php echo $this->htmlEscape($_product->getextraline()) ?></div>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> 
                    <?php echo $this->getPriceHtml($_product, true, '-widget-new-grid') ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <!-- <button type="button" title="<?php /* echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><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>-->
                        <div class="out-of-stock-special"><img src="<?php echo $this->getSkinUrl('images/donna/soldout-overon.png') ?>" alt="uitverkocht" width="50px" /></div>
                    <?php endif; ?>
                    <?php /*<ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul> */ ?>
                </div>
                </li>
        <?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
        </ul>
        <?php endif ?>
        <?php endforeach; ?>
        <div class="toolbar-bottom">
            <?php // echo $this->getToolbarBlock()->setTemplate('catalog/product/list/ctoolbar.phtml')->toHtml(); ?>
        </div>
    </div>
</div>
<?php endif; ?>  
========================================

In the CMS Page that you created, click Design tab then in the Page layout>Layout update xml put this code..

<reference name="content">
   <block type="catalog/product_special" name="product_special" template="catalog/product/list.phtml">
       <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
              <action method="setDefaultDirection"><dir>desc</dir></action>
              <action method="setDefaultOrder"><field>entity_id</field></action>
              <block type="page/html_pager" name="product_list_toolbar_pager" />
       </block>
      <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
      <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
   </block>
</reference>
========================================

There you have it... Don't forget to put a special price, special-from-date and special-to-date in your products...
Reference: http://www.magentocommerce.com/boards/viewthread/16098/P15/ :kkrieger's post

7 comments:

  1. Hi,

    I'm using this special page script on Brandeer Sale

    Now I've the problem, that I cant set the number of products on the sale page. It always takes the store default (15) or the value you've choosen in the toolbar..

    I tried it with

    [CODE]
    12

    [/CODE]

    but no changes.. Any suggestions?

    ReplyDelete
  2. It is not working for Magento 1.5.0.1. PLease advise.

    ReplyDelete
  3. I don't know why but it works in my system only showing 8 products??

    ReplyDelete
  4. Hi,
    I really surprised beause in my Magento I have not this folder: app/code/local/Mage/ .....

    I downloaded magento again and it is not inside (1.7.0.2).

    Any ideas?

    ReplyDelete
  5. This works, but if I try to sort it redirects to the home page. do you know how to keep the search on the special page?

    ReplyDelete