Invalid command ‘Header’, perhaps misspelled or defined by a module not included in the server configuration

March 7, 2012  |  4 Comments  |  by Raj (MagePsycho)  |  Apache, Latest, Linux, Magento

I got the following error for one of the Magento e-commerce website:

Invalid command ‘Header’, perhaps misspelled or defined by a module not included in the server configuration

After googling it was found that related headers module was not enabled in Apache.

I was able to fix the issue simply by following two commands:
1. Enabling headers Module

ln -sf /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load

2. Restarting Apache

/etc/init.d/apache2 restart

Note: Above commands were run from Ubuntu 10.04.2 server

Hope this’s helpful.
Cheers

Usage of Magento event: core_block_abstract_to_html_after

March 4, 2012  |  1 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

Introduction

There are lots of events in Magento which are dispatched by default. Among them ‘core_block_abstract_to_html_after’ is the one.

Recently we have re-developed the Easy Template Path Hints extension using this event as the previous version of the extension has some confliction issues with other modules.

In this article we will demonstrate the implementation of ‘core_block_abstract_to_html_after’ event in Easy Template Path Hints Extension:

1. Register the ‘core_block_abstract_to_html_after’ event

app/code/local/MagePsycho/Easypathhints/etc/config.xml

...
<global>
...		
	<events>
		<core_block_abstract_to_html_after>
			<observers>
				<easypathhints_core_block_abstract_to_html_after>
					<class>easypathhints/observer</class>
					<method>setTemplatePathHints</method>
				</easypathhints_core_block_abstract_to_html_after>
			</observers>
		</core_block_abstract_to_html_after>
	</events>
...
</global>
...

2. Implementing the Observer

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Easypathhints
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Easypathhints_Model_Observer {

	public function setTemplatePathHints(Varien_Event_Observer $observer) {

		/* @var $block Mage_Core_Block_Abstract */
		$block				= $observer->getBlock();
		$transport			= $observer->getTransport();

		$fileName			= $block->getTemplateFile();
		$thisClass			= get_class($block);
		if($fileName){
			$preHtml = '<div style="position:relative; border:1px dotted red; margin:6px 2px; padding:18px 2px 2px 2px; zoom:1;">
<div style="position:absolute; left:0; top:0; padding:2px 5px; background:red; color:white; font:normal 11px Arial;
text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'"
onmouseout="this.style.zIndex=\'998\'" title="'.$fileName.'">'.$fileName.'</div>';
			$preHtml .= '<div style="position:absolute; right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial;
	text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'" onmouseout="this.style.zIndex=\'998\'"
	title="'.$thisClass.'">'.$thisClass.'</div>';

			$postHtml = '</div>';
		}else{
			$preHtml	= null;
			$postHtml	= null;
		}


		$html = $transport->getHtml();
		$html = $preHtml . $html . $postHtml;
		$transport->setHtml($html);
	}
}

Notes: From above code you can see that we can easily find the related $block object and $transport object which are very handy in order to find the block class, template file, modify the block output ($transport->setHtml()) etc.

Output of Easy Template Path Hints

Isn’t that cool way to modify the core code without touching it?
FYI, Event-Observer technique is always the best method for modifying core code.

Don’t forget to provide the feedback/comments if there’s any.
Happy E-commerce!!

How to change default sort order / dir in Magento search results?

March 4, 2012  |  6 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

Introduction

Today we will be discussing about how to change the defualt sort order / dir in magento search results (simple & advanced).

Regarding solution, some says you need to change the /app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php file in order to change the default sort order field and dir. But you don’t need to do so as there is an extremely easy way to do so.

Suppose say we are changing the default sort order field to ‘name’ and default sort direction to ‘asc’. Here goes the solution:

Changing default sort order / dir in simple search results:

1> Copy the following file to your custom theme folder
app/design/frontend/*/*/template/catalogsearch/form.mini.phtml
2> and insert the following two lines of code in between the

<form></form>

element of form.mini.phtml:

<input type="hidden" name="order" value="name" />
<input type="hidden" name="dir" value="asc" />

3> You’re done

Changing default sort order / dir in advanced search results:

1> Copy the following file to your custom theme folder
app/design/frontend/*/*/template/catalogsearch/advanced/form.phtml
2> and insert the following two lines of code in between the

<form></form>

element of form.phtml:

<input type="hidden" name="order" value="name" />
<input type="hidden" name="dir" value="asc" />

3> You’re done

Sort By Field: 'name', Dir: 'asc'

Hopefully this artice was useful.
Thanks for reading guys.

Release of newer version of jQuery Lightboxes 1.2.0

February 28, 2012  |  3 Comments  |  by Raj (MagePsycho)  |  jQuery, Latest, Magento

Introduction

Magento community is showing great love towards the Magento Extension: jQuery Lightboxes (probably the #1 lightbox extension for Magento e-Commerce), as a result we are on the way to make it much better with lots of new features.
Just we have released a newer version of jQuery Lightboxes i.e. 1.2.0.

Changelog

Version 1.1.0 – 1.2.0
– Tested Compatibility with Magento Version 1.6.2.0
– Upgraded jQuery Version (local + Google CDN) to 1.7.1
– Added Option to include Main Image to lightbox gallery of More Views
– Added Option to change the label for ‘More Views’
– Upgraded FancyBox to version 1.3.4
– Changed the default settings of FancyBox so that it looks even more sexier
– Added new Lightbox Type: PiroBox Extended (advanced form of Pirobox)
– Added new Lightbox Type: prettyPhoto
– Code Refinement

You can download the latest version of it either from
1> Official Site
Or
2> Magento-Connect

Try once and let me know your feedback/suggestions if there’s any.
Thanks a lot for loving this extension!

Demystifying PHP’s Array Key/Index

February 14, 2012  |  No Comments  |  by Raj (MagePsycho)  |  Latest, Magento, PHP

According to php.net manual:

An array in PHP is actually an ordered map. A map is a type that associates values to keys.

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. “8” will be interpreted as 8, while “08” will be interpreted as “08”). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

Well that’s just a bit of defination about Array and it’s key/index.
Let’s play with some practical examples.
Suppose say you are making a dropdown field for Opacities(0, 0.1, 0.2, … 0.9, 1) for your System > Configuration field (Magento) and using your source model as:

<?php
class MagePsycho_Custommodule_Model_System_Config_Source_Opacity
{
    public function toOptionArray()
    {
        $range = range(0, 1, 0.1);
        $array = array();
        foreach($range as $val){
            $array[$val] = $val;
        }
        return $array;
    }
}

When you try to load the page in System > Configuration for your module you will see that only there is only one value in dropdown i.e.

<option value="0">1</option>

You must be wondering where are the other values (0 – 0.9)?
Now the role of key/index comes into play.

As per above defination: An array key/index can only be an integer or a string type. But here index is a float type and php will convert it to an integer type and any float value: 0.1 – 0.9 will be converted to integer: 0.

But if you modify the above code as:

<?php
class MagePsycho_Custommodule_Model_System_Config_Source_Opacity
{
    public function toOptionArray()
    {
        $range = range(0, 1, 0.1);
        $array = array();
        foreach($range as $val){
            $array["$val"] = $val; //note the double quotes around the key
        }
        return $array;
    }
}

You must have noted the double quotes around the key and using the double quotes will populate all the values from 0 – 1.
This is because PHP tries to interpolate the variable inside the double quotes and return as string. And the key looks like: For example:

$array['0.9']

which is a string key not a float one.

Hope this helps you to get some understanding of proper usage of indexing in an Array.
Thanks for Reading!