0

I need to display all products on sale in one catagory page.. rather than going through the whole catalog &adding products manually I want a function that will pick out "Special Price" products

I found this article online http://www.creativemgroup.com/creative-media-web-services/magento-blog/68-create-a-magento-on-sale-category-the-right-way

& got is working here

The problem is it doesnt sort products by "Sort By" Changing Sort options makes no difference.. How can I use this code but still be able to use the sort toolbar?..

Many thanks for any help..

////////////////////////////// Here is the code in my Catogory - Custom Design //////////////////////////////

<reference name="content">
<remove name="product_list"/>
<block type="catalog/product_sale" name="product_sale" alias="sale" template="catalog/product/list.phtml">
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setLimit"><limit>32</limit></action>
<action method="setColumnCount"><columns>3</columns></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
<action method="addPriceBlockType">
<type>bundle</type>
<block>bundle/catalog_product_price</block>
<template>bundle/catalog/product/price.phtml</template>
</action>
</block>
</reference>
<reference name="product_list_toolbar">
<action method="setDefaultGridPerPage">
<limit>32</limit>
</action>
</reference>

////////////////////////////////////////// Here is the main PHP function /////////////////////////////////////////

class Mage_Catalog_Block_Product_Sale extends Mage_Catalog_Block_Product_List {

//public $_collection;  

public function getProductsLimit() 
{ 
    if ($this->getData('limit')) {
        return intval($this->getData('limit'));
    } else {
        return 32;
    }
}

public function __construct()
{
    parent::__construct();
    $collection = $this->_getProductCollection();
    $this->setCollection($collection);
}






 protected function _getProductCollection()  
 {  
            $page = Mage::getBlockSingleton('page/html_pager')->getCurrentPage();
            date_default_timezone_set(Mage::getStoreConfig('general/locale/timezone'));
            $todayDate = strftime("%Y-%m-%d",Mage::app()->getLocale()->storeTimeStamp(Mage::app()->getStore()->getId()));
            $storeId    = Mage::app()->getStore()->getId();  
            $product    = Mage::getModel('catalog/product');  

            $this->_productCollection = $product->setStoreId($storeId)  
                ->getCollection()  
                ->addAttributeToSelect(array('name','status', 'price', 'special_price', 'small_image','required_options','special_from_date', 'special_to_date'), 'inner')
                ->joinField('stock_status','cataloginventory/stock_status','stock_status',
                    'product_id=entity_id', array(
                      'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK,
                      'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
                    ))
                ->addAttributeToFilter('special_price', array('gt' => 0), 'left')
                ->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')
                //->setOrder('created_at', 'desc')
                ->addAttributeToSort('created_at', 'desc')
                ->addFinalPrice()
                ->addStoreFilter()
                ->setPageSize($this->getProductsLimit())
                ->setCurPage($page)
                ->addAttributeToFilter('status', 1)
                ->addUrlRewrite();

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);  
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);


            $checkedProducts = new Varien_Data_Collection();
                foreach ($this->_productCollection as $k => $p) {
                    $p = $p->loadParentProductIds();
                    $parentIds = $p->getData('parent_product_ids');

                    if (is_array($parentIds) && !empty($parentIds)) {
                        if (!$checkedProducts->getItemById($parentIds[0])) {
                            $parentProduct = Mage::getModel('catalog/product')->setStoreId($storeId)->load($parentIds[0]);
                            if ($parentProduct->isVisibleInCatalog()) {
                                $checkedProducts->addItem($parentProduct);
                            }
                        }
                    } else {
                        if (!$checkedProducts->getItemById($k)) {
                            $checkedProducts->addItem($p);
                        }
                    }
                    if (count($checkedProducts) >= $this->getProductsLimit()) {
                        break;
                    }
                }

return $this->_productCollection;

 }  

}

//////////////////////////////

dan_code89
  • 25
  • 1
  • 2
  • 11

2 Answers2

1

My guess is that your problem is related to not including layered navigation in your logic. You have overridden the _getProductCollection() method and instantiated your collection directly, rather than how it normally gets injected from Mage_Catalog_Model_Layer. You are going to have to reincorporate that feature back into your new method.

Community
  • 1
  • 1
fantasticrice
  • 1,631
  • 1
  • 21
  • 31
  • How do I include '_getProductCollection()' into my new php ?.. Is there a way I can pull that method **from default layered navigation** back into my code?.. Would appreciate any help :) – dan_code89 May 01 '15 at 11:37
  • @dan_code89 - Take a look at the definition of the original `_getProductCollection()` in `app/code/core/Mage/Catalog/Block/Product/List.php`. Since you are extending from this class, I would see if you can do whatever changes you need on the collection returned from `parent::_getProductCollection()`, which will be the one injected from the layered navigation. – fantasticrice May 01 '15 at 16:17
  • Thanks for the help mate.. I have pulled `list.php` functions for Toolbar & Sorter into my `Sale.php` file.. dont think I can figure this I'm not a PHP coder unfortunately.. a very good graphic designer lol but I need a bit more time on the code... I will keep looking through & try to fix.. I think a lot of the elements on the page are pre defined by the "code in my Category tab" I cant see a way to add `list.php` toolbar functions in there either.. Bit stuck but will keep looking :) – dan_code89 May 01 '15 at 17:11
0
      $params = $this->getRequest()->getParams();
      $dir = (!empty($params['dir'])) ? $params['dir'] : 'desc';   
      $order = (!empty($params['order']) && $params['order'] != 'position') ? $params['order'] : 'created_at';

      //modify collection method:
      ->addAttributeToSort($order, $dir)
UUDotCom
  • 126
  • 1
  • 3
  • 16