Monday 28 July 2014

Here in this tutorial, I would like to share you how can we set page size for a particular category.

The Page Size is actually a property of a toolbar block that present in prodouct list block. So basically what you need to do is to set page size to a particuar value for toolbar block, when your category get loaded.

For this you can use an observer method. In order to implement it, you need to know the id of category. Here I am taking my reference id as 23. Also for this let us set up a custom module, Programmerrkt_PageSizeSetter.

Code Begins


So first let us activate our module

Location : app/etc/modules/Programmerrkt_PageSizeSetter.xml

 
     
      
             true
             local
         
     
 

Next, Let us configure our module,

Location: app/code/local/Programmerrkt/PageSizeSetter/etc/config.xml

  
     
      
          1.0.0
      
     
     
         
             
                 
                     
                         singleton
                         programmerrkt_pagesizesetter/observer
                         setPageSizeForCategory
                     
                 
             
         
     
     
         
             
                 Programmerrkt_PageSizeSetter_Model
             
         
     
 

As you can see, through our module, we are planning to observe an event controller_action_layout_generate_blocks_after. If we need to change some block properties, then this event would be the perfect and coolest event to listen to. Also you can see we are planning to define a custom method setPageSizeForCategory in our observer. At last, we defined model section for our module inside global node. This is because observer is going to define in our model.

So it is the time to define our observer. So let us do that

Location: app/code/local/Programmerrkt/PageSizeSetter/Model/Observer.php

getAction();
   $fullActionName = $controller->getFullActionName();
   $id = (int)$controller->getRequest()->getParam('id');

         //check whether current page is correspond to our special category. If not, returns
   if($fullActionName == "catalog_category_view" && $id == $this->_categoryId)
   {
    //check whether toolbar block exist or not
    $toolbar =  $controller->getLayout()->getBlock('product_list_toolbar');
    if($toolbar)
    {
     //sets page size to corresponding list mode
     $listMode = $toolbar->getCurrentMode();
           $toolbar = $toolbar->addPagerLimit($listMode , $this->_pageSize);
    }
    
   }

   return;
  }
 }
 

So here in setPageSizeForCategory method, we ensures that we are standing in particular category page. If it is not, the control returns. If the page is the category that we are talking about, we will set page size to the toolbar block of that category.

The method that we use here to set page size is addPagerLimit. It has 3 parameters to pass. First one is list mode. It will be either grid/list. We are passing the current mode of toolbar block to this method, so that we dont need to really worry on list mode confusion. Next parameter is the size of page. Last one is label. Here we dont want to pass any labels. Hence not using it.

You can also see that our observer class has two properties $_categoryId and $_pageSize. First one holds the id of our special category and second one holds page size values. So you need to set these two properties according to your need. This will allow us setPageSizeForCategory untouched.

Drawback
:- There is a small drawback for this approach. The event we are observing here will trigger in every page load. Since the scope our module is limitted to a particular category, most of the time our module will return without doing anythong. I feel it as awkward and that's why I mentioned it here. But common !!! this is also a solution. Isn't it?

Output



If anyone solve this issue in better way, let me know and please share it. :)

1 comment:

  1. Casino Finder (Washington) - Mapyro
    › › United States › United States › United States › United States › United States › United States › United States Mon, 거제 출장안마 Dec 13Extra Salt - $2,000.00 - $1,000.00 Mon, Dec 13Atomic Salt - 서귀포 출장샵 $2,000.00 - 안동 출장마사지 $1,000.00 Mon, 서울특별 출장마사지 Dec 13Atomic Salt - 경상북도 출장샵 $2,000.00 - $1,000.00

    ReplyDelete