RSS Feed

Magento : Enable Widget/Block Codes in Catalog Product

In a recent task, I created a widget, which was about to insert a email opt-in form in the description area.

After creating the widget, I realized the widget worked fine, when placed in WYSIWYG editor in CMS module, but didn’t work if placed in the description field for Product.

After a bit of digging into the template parsing core classes, it was realized  that Magento didn’t support rendering block/widget directives in Catalog section.

So we decided to extend the parsing class, to enable the required feature.

Please find below steps on extending the core class ‘Mage_Catalog_Model_Template_Filter’ and add the required methods to enable the rendering for widget and block directives

1. Create a new module and place it under ‘local’ folder. I named it ‘Maniacs_Directive’

2. Add rewrite in the config file, for the new created module, in order to extend the core file

<catalog>
<rewrite>
<template_filter>Maniacs_Directive_Model_Catalog_Template_Filter</template_filter>
</rewrite>
</catalog>

3. Inside the new class ‘Maniacs_Directive_Model_Catalog_Template_Filter’, add the two methods named as follows: ’blockDirective’ & ‘widgetDirective’

4. Contents for method ’blockDirective’, are as follows:

public function blockDirective($construction) {
$model=Mage::getModel('core/email_template_filter');
return $model->blockDirective($construction);
}

5. Contents for method ‘widgetDirective’, are as follows:

public function widgetDirective($construction) {
$construction[2] .= sprintf(' store_id ="%s"', $this->getStoreId());
$model=Mage::getModel('widget/template_filter');
return $model->widgetDirective($construction);
}

This should enable, the rendering, and all the admin has to do is to add directive codes into the WYSIWYG editor.

 

Re-installing Custom Modules in Magento

While developing custom modules in Magento, you could probably come around a state, where you make changes in setup files, and would look forward to changes to be reflected. But, as the setup file is run for just once, when the module is installed for the first time, the changes wouldn’t reflect.

In order to get this changes reflected, we need to re-install the module, and to perform the process of re-installation, we got to remove any references of last setup.

follow the below mentioned steps, to get your module re-installed:

  • in the config file, of your module, find the name of setup, under global > resources tag
  • in the database, inside ‘core_resource‘ table, search, for the string found above, and delete it or you could execute the following query :

delete * from core_resource where `code`=’<place_the_tag_name_here>’

  • delete all the contents of var/cache folder.
Now try refreshing any page in Magento, and this would re-install your module.

Adding jQuery into Zend Framework Project

Though, Zend Framework (ZF) is shipped with set of Dojo libraries and various helpers/views to implement its methods, due to popularity of jQuery, the integration between the two is most seek.

In order to integrate jQuery, look for “extras/library/ZendX”, folder in your extracted copy of the download from Zend’s site (Full package, not the minimal one).

‘ZendX’, represents extended libraries created by third-parties and have been adopted/included by people at Zend.

Copy the folder ‘ZendX’, to the library folder of your working copy of your project or you can alternatively specify the path to ZendX folder in the php.ini configurations.

Further, there are two methods by which we can instruct our ZF project setup to include the jQuery library, while it is loaded.

First, add the following codes to ‘Bootstrap.php’ :

/**
* init jquery view helper, enable jquery, jqueryui, jquery ui css
*/
protected function _initJquery() {
$this->bootstrap('view');
$view = $this->getResource('view'); //get the view object
//add the jquery view helper path into your project
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
//jquery lib includes here (default loads from google CDN)
$view ->jQuery()
->enable()//enable jquery ;
//->setCdnSsl(true) if need to load from ssl location
//->setVersion('1.7')//jQuery version, automatically 1.5 = 1.7.latest
//->setUiVersion('1.8')//jQuery UI version, automatically 1.8 = 1.8.latest
//->addStylesheet('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/ui-lightness/jquery-ui.css')//add the css
//->uiEnable() //enable ui
;
}

Secondly, you can specify the options in application.ini, as follows:

resources.Jquery.noconflictmode = false
resources.Jquery.version = 1.7.2
resources.Jquery.enable = true
resources.Jquery.ui_enable = false
resources.Jquery.ui_version = 1.8
resources.Jquery.cdn_ssl = false
resources.Jquery.render_mode = 255
; resources.Jquery.stylesheet = "/some/file.css"
; resources.Jquery.stylesheets.0 = "/some/file.css"

With the library inclusion and initialization done, its time to look for manner in which it can be added to view/layout:

echo $this->jQuery();

yes, you are done, the above code would include the following line(s) in your rendered html for the selected view:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Happy Coding ;)

Zend Framework application.ini Cheatsheet

While looking on for detailed information, on various configuration possible in Zend framework through application.ini, I came across this wonderful link which directed me to a GitHub respository which contains an awesome resource on all possible options.

https://github.com/feibeck/application.ini

Hats off to the author :)

Great work.

Empty Cart with Secure Checkout in Magento

Applying, secure URL to a Magento store, could have been like applying butter over bread, if we go by every post or tutorial available over the web. But it turned out to be a brain-exhausting experience for me.

The issue I faced was as follows:

  • user adds a product to cart.
  • when clicked on “Proceed to Checkout” button, Magento used to redirect user back to cart page with the message, “Shopping Cart empty”.
  • I used to loose login session, whenever i tried to move to HTTPS on URL.

I tried, every kind of tip and trick available, to fix this but to no relief.

Finally, I figured that the server had ‘Suhosin Extension‘ installed.

This extension used to encrypt the session values, and hence Magento was unable to find the session with the new encrypted name, so it brought all the trouble.

the workaround to this would be to add, the following line in .htaccess:

php_flag suhosin.session.encrypt Off

if you too have you session handled by filesystem, as i had, you can add the following too to resolve issues with user’s session being lost:

php_flag suhosin.session.cryptdocroot Off

 

Filter Product Collection for Multi Select Attribute in Magento

I came across this very interesting issue, when i was trying to filter product collection with value from Multi Select Attribute created, as it didn’t work as easy as it works for any other attribute, i.e.,

$product_collection->addAttributeToFilter('attribute_code',"some value")

The collection filters provides a filter condition finset, which helps us to filter attribute values inside a set.

First, we need to fetch the IDs for attribute’s various values, as follows:

$attributeOptionArray=array();
$opts_attr = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_code');
foreach ( $opts_attr->getSource()->getAllOptions(true, true) as $option){
$attributeOptionArray[$option['value']] = $option['label'];
}

Next we need to use this array of attribute values to filter across the product option:

$product_collection=Mage::getModel("catalog/product")->getCollection();
$product_collection->addAttributeToFilter("attribute_code",array('finset'=>array_search("Some Option's label",$attributeOptionArray)));

Explanation:

1. First, we load the product collection to a variable

2. We add “attributeToFilter“, in order to specify the filtering conditions.

3. With the array_search condition, we try to find the id corresponding to Option’s label.

4. Pass this id to filter with finset condition.

%d bloggers like this: