Converting multi-select field to checkbox in advanced search form of Magento

January 19, 2012  |  3 Comments  |  by Raj (MagePsycho)  |  Latest, Magento, PHP

Introduction:

Have you ever thought of modifying the default multi-select field to checkbox field in advanced search of Magento?
If not then here we will be discussing about how to do which is damn easy and much user friendlier.

Steps:

1> Copy the following file to your working theme:
app/design/frontend/[interface]/[theme]/template/catalogsearch/advanced/form.phtml

2> Open the form.phtml (from above) and find the following line just after the case ‘select’: ?>

<div class="input-box">
	<?php echo $this->getAttributeSelectElement($_attribute) ?>
</div>

and replace it by the following code

<?php if(in_array($_attribute->getAttributeCode(), array('manufacturer'))): ?>
<div class="input-box">
	<?php
		 $options = $_attribute->getSource()->getAllOptions(false);
		 foreach($options as $_option):
			 $isChecked = in_array($_option['value'], $this->getRequest()->getParam($_attribute->getAttributeCode())) ? ' checked="checked"' : null;
			 ?>
	<input type="checkbox" name="<?php echo $_attribute->getAttributeCode(); ?>[]" value="<?php echo $_option['value']; ?>"<?php echo $isChecked; ?> /> <?php echo $_option['label']; ?><br />
	<?php
		 endforeach;
	?>
</div>
<?php else: ?>
<div class="input-box">
	<?php echo $this->getAttributeSelectElement($_attribute); ?>
</div>
<?php endif; ?>

Note: Here we have customized the display for manufacturer attribute, similarly you can customize for other attributes. Just you need to add the attribute code(for example: color) in the array as:

<?php if(in_array($_attribute->getAttributeCode(), array('manufacturer', 'color'))): ?>

3> Try to refresh the advanced search page: http://your-magento-url/catalogsearch/advanced
You will see some good results 🙂

Manufacturer in default format (Before)

Customized Manufacturer Display(After)


Note: You can use some css in order to break the checkboxes in multi-columns for better display.
Hope you liked this article 🙂

Utilizing debug_backtrace() function for Magento debugging

January 14, 2012  |  5 Comments  |  by Raj (MagePsycho)  |  Latest, Magento, PHP

Introduction

As promised, back with the 2nd utility function for Magento (printDebugBacktrace).
This function will help you to print the backtrace. As you know that Magento has so many classes and flow/interlinking of those classes’ methods are really vague. So this function will help you to debug/trace in such cases.
printDebugBacktrace function looks like:

public static function printDebugBacktrace($title = 'Debug Backtrace:') {
	$output		= "";
	$output .= "<hr /><div>" . $title . '<br /><table border="1" cellpadding="2" cellspacing="2">';

	$stacks		= debug_backtrace();

	$output .= "<thead><tr><th><strong>File</strong></th><th><strong>Line</strong></th><th><strong>Function</strong></th>".
		"</tr></thead>";
	foreach($stacks as $_stack)
	{
		if (!isset($_stack['file'])) $_stack['file'] = '[PHP Kernel]';
		if (!isset($_stack['line'])) $_stack['line'] = '';

		$output .=  "<tr><td>{$_stack["file"]}</td><td>{$_stack["line"]}</td>".
			"<td>{$_stack["function"]}</td></tr>";
	}
	$output .=  "</table></div><hr /></p>";
	return $output;
}

You need to put the above method in app/Mage.php.
Note: As you see that above method utilizes the php’s debug_backtrace() method. But manually calling that function will give you very large string due to large no of complex properties & nested objects in magento classes and will be very difficult in tracing. So recommended to use the above mentioned utility function instead.

Usage

Suppose say you are trying to figure out the flow of Mage_Sales_Model_Quote_Address_Total_Shipping::collect() method.
Go to that class method and put the following line

public function collect(Mage_Sales_Model_Quote_Address $address)
{
	echo Mage::printDebugBacktrace(); exit; //add this line just after the opening
	//Mage::log(Mage::printDebugBacktrace(), null, 'backtrace.log'); //or you can even log the backtrace

And when you load the page (my cart) in frontend then you will get the following output:

Output: printDebugBacktrace


Isn’t that very helpful to know the flow and debug accordingly?
Good Luck with Magento Debugging.

EDIT:

After this article was written, found that we can do the very same thing using the following in built method:
Varien_Debug::backtrace()
Whose argument are as:

	
/**
* Prints or return a backtrace
*
* @param bool $return      return or print
* @param bool $html        output in HTML format
* @param bool $withArgs    add short argumets of methods
* @return string|bool
*/
public static function backtrace($return = false, $html = true, $withArgs = true)

So now you can simply call as

echo Varien_Debug::backtrace(true, true); exit;
//or
Mage::log(Varien_Debug::backtrace(true, true), null, 'backtrace.log');