I want to override the Product grid tool-bar for custom work in my module.I inlcude my filters in the filters array but the product grid is not executing my code when i select my filter like , i want to add best seller and most viewed filters to toolbar ,My config.xml code is :
<catalog>
<rewrite>
<product_list_toolbar>Mymodule_Block_Catalog_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>.
I created a block class for that like :
<?php
class Mymoudle_Block_Catalog_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function getAvailableOrders()
{
$vari = Mage::helper('Mymoudle')->getMethods();
return $vari;
}
public function getCurrentDirection()
{
$dir = parent::getCurrentDirection();
$url = strtolower($this->getRequest()->getParam($this->getDirectionVarName()));
if (!$url){
$dir = 'desc';
}
return $dir;
}
public function setCollection($collection)
{
parent::setCollection($collection);
$methods = $this->getMethods();
if (isset($methods[$this->getCurrentOrder()])){
$methods[$this->getCurrentOrder()]->apply($collection, $this->getCurrentDirection());
}
if($this->getCurrentOrder() == 'bestselling'){
if(Mage::helper('Mymoudle')->getbestperiod() > 0){
$limit = (int)$this->getLimit();
$collection = Mage::helper('Mymoudle')->getBestsellingProducts($limit,$this->getCurrentDirection());
}
}
if($this->getCurrentOrder() == 'most_viewed'){
if(Mage::helper('Mymoudle')->getviewdtimeperiod() > 0){
$limit = (int)$this->getLimit();
$collection = Mage::helper('Mymoudle')->getMostViewedProducts($limit);
}
}
echo $collection->getSelect();
return $collection;
}
public function getCollection()
{
return $this->_collection;
}
}.
this is my Helper class :
<?php
class Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
{
const SORT_PRODUCTS_STATUS = 'sortproducts/setting/settings';
const SORT_PRODUCTS_POSITION_FILTER = 'sortproducts/general/position_filter';
const SORT_PRODUCTS_BEST_PERIOD = 'sortproducts/general/best_period';
const SORT_PRODUCTS_VIEWD_PERIOD = 'sortproducts/general/viewed_period';
const SORT_PRODUCTS_WHISLIST_PERIOD = 'sortproducts/general/wishlist_period';
public function getextEnable()
{
return Mage::getStoreConfig(self::SORT_PRODUCTS_STATUS);
}
public function getpositionstatus()
{
return Mage::getStoreConfig(self::SORT_PRODUCTS_POSITION_FILTER);
}
public function getbestperiod()
{
return Mage::getStoreConfig(self::SORT_PRODUCTS_BEST_PERIOD);
}
public function getviewdtimeperiod()
{
return Mage::getStoreConfig(self::SORT_PRODUCTS_VIEWD_PERIOD);
}
public function getwishlistperiod()
{
return Mage::getStoreConfig(self::SORT_PRODUCTS_WHISLIST_PERIOD);
}
public function getMethods()
{
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection()->AddFieldToFilter('used_for_sort_by','1');
foreach ($attributes as $attr) {
$value = $attr->getAttributeCode();
$label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
$arr[$value] = $label;
}
if($this->getbestperiod() > 0)
{
$t = array('bestselling' => $this->__('Best Seller'));
}
if($this->getviewdtimeperiod() > 0)
{
$ts = array('most_viewed' => $this->__('Most Viewed'));
}
$arr = array_merge($arr,$ts);
$scd = array_merge($arr, $t);
if($this->getpositionstatus())
{
$scd = array_merge($scd,array('position' => $this->__('Position')));
}
asort($scd);
return $scd;
}
public function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
public function getBestsellingProducts($pagecount,$orderby)
{
// number of products to display
$productCount = $pagecount;
// store ID
$storeId = Mage::app()->getStore()->getId();
// get today and last 30 days time
$today = time();
$last = $today - (60*60*24*$this->getbestperiod());
$from = date("Y-m-d", $last);
$to = date("Y-m-d", $today);
if($orderby == 'asc')
{
$neworder = 'desc';
}elseif($orderby == 'desc')
{
$neworder = 'asc';
}
// get most viewed products for current category
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty($from, $to)
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', $neworder)
->setPageSize($productCount);
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')
->addVisibleInCatalogFilterToCollection($products);
return $products;
}
public function getMostViewedProducts($pagecount)
{
// number of products to display
$productCount = $pagecount;
// store ID
$storeId = Mage::app()->getStore()->getId();
// get today and last 30 days time
$today = time();
$last = $today - (60*60*24*$this->getviewdtimeperiod());
$from = date("Y-m-d", $last);
$to = date("Y-m-d", $today);
// get most viewed products for current category
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty($from, $to)
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount()
->setPageSize($productCount);
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')
->addVisibleInCatalogFilterToCollection($products);
return $products;
}
}.
When i select my custom filter the getselect() code shows that my code runs but nothing happend to grid.The problem is grid not showing my cusotm filter results.
thanks in advance
Best Answer
In your block's
setCollection
function the first thing you do isparent::setCollection
. This will set the collection as is passed into the function. You then proceed to get new collections from a helper. But these are never set anywhere maybe you should move the call toparent::setCollection
until just before the return and then return$this
rather than$collection