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. :)

Tuesday 22 July 2014

On 18:56 by Unknown in , ,    1 comment
Some times, it would be great if we can add a collapse/expand functionality in CMS Pages in Magento. A perfect example would be a read more option. When we display very long documents through CMS pages, it would be nice if we shrink the content part to a small section and add a read more link bottom of it. When user clicks on that link, the content will be expanded.

We can achieve this functionality by implementing a custom extension. I will show you the steps that we need to take to achieve this. So our extension should have a name Programmerrkt_AdvancedCms.

First step is activating our module. For this add this file in etc/modules

Location : app/etc/modules/Programmerrkt_AdvancedCms.xml


Next step is to define configuration of our module. Let us do that.

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


This file tells to Magento that our module has a layout file for frontend section. That's it. Our layout file is the heart of our module. It is going to hold the important parts of our module. Let us define our layout file.

Location : app/design/frontend/<.your_package>/<.your_theme>/layout/programmerrkt_advancedcms.xml


So here we defined layout for cms_page handler. Magento will look for this layout handler whenever a request for CMS page is made. So this handler would be a perfect handler for us. Next we added jquery and readmore.js in header section. Then at last we defined a template file script.phtml for holding our custom javascript code. Note that we included this template at the bottom of page.This is achieved by using the block before_body_end. This will ensure that, our custom code will invoked perfectly.

Now our script.phtml should look like this.

Location : app/design/frontend/<.yourpackag>/<.your_theme>/template/programmerrkt/advancedcms/readmore/script.phtml

	

As you can see readmore() method is called on an element with an id readomore-demo. So it is required that, you need to enclose all the content your cms page inside this id. Demo is shown here.
We are done. Our output will look like this.

So only thing you need to change is edit script.phtml file according to your needs.

Note: Remember you need to add jquery for proper working of this module. If you have jquery installed, then remove the code that add jquery in layout file. Also you need to download readmore.js and has to include it in skin/frontend/<.your_package/<.your_theme>/js/customjs/readmore.js. Similarly add css file in skin/frontend/<.your_package/<.your_theme>/css/customcss/readmore.css. You can use this css file to decorate links appear in your cms page.

Additional Note: readmore.js requires jquery greater than 1.7.0

Sunday 13 July 2014

On 21:52 by Unknown in ,    2 comments
In this post, I would like to show you how can we get product option values.

For this, let us start with creating a sample product that holds some custom options. Our Product has name Test Product With Option and below I am showing you its custom option that we set through admin



Now its frontend view will look like this.



This is how we can obtain each option value



Here what we have done is, first we load our product that holds custom options. We load the product through its id. So $product holds our product. Now we assigns all options of product to $option. getOptions() will return an array of object that of class Mage_Catalog_Model_Product_Option. Then we are looping through each options and then loads option values by invoking getValues(). This method will also return an array that holds objects of class Mage_Catalog_Model_Product_Option_Value. Each of this object holds complete information of a particular value.

So the output will look like this
Option :custom checkbox
Values:
Array
(
[option_type_id] => 7
[option_id] => 4
[sku] =>
[sort_order] => 0
[default_title] => custom checkbox value 1
[store_title] =>
[title] => custom checkbox value 1
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
(
[option_type_id] => 8
[option_id] => 4
[sku] =>
[sort_order] => 0
[default_title] => custom checkbox value 2
[store_title] =>
[title] => custom checkbox value 2
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Option :custom dropdown
Values:
Array
(
[option_type_id] => 9
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 1
[store_title] =>
[title] => custom option value 1
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
( [option_type_id] => 10
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 2
[store_title] =>
[title] => custom option value 2
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Array
(
[option_type_id] => 11
[option_id] => 5
[sku] =>
[sort_order] => 0
[default_title] => custom option value 3
[store_title] =>
[title] => custom option value 3
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Option :Custom Text Field
Values:

Thus getting option values of custom option of a product is pretty straight forward and easy to understand.