How to find current url in Magento

May 27, 2012  |  6 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

You must be wondering we can simply use:

$currentUrl = Mage::helper('core/url')->getCurrentUrl()

or

$currentUrl = Mage::getUrl('*/*/*', array('_current' => true));

in order to find the current url.

But it does’t always work as expected.
Try to use the above code in a page which has some missing css, js, images etc.(You can use Firebug in order to find such error which are so called Network error.). You will see that above code captures the url for missing file(js, css, images etc.) which is obviously not the required current url.

You can simply fix above error by using the following code:

if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array('cms_index_noRoute', 'cms_index_defaultNoRoute'))) {
	$currentUrl = Mage::helper('core/url')->getCurrentUrl();
}

Cheers!

Debugging technique in Magento: Process of elimination

May 27, 2012  |  No Comments  |  by Raj (MagePsycho)  |  Latest, Magento

Introduction:

Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected.

Source: Wikipedia

Nobody is perfect so don’t expect an application to be perfect. As a developer you may face many bugs during your application development, some are easy to trace & fix and some are hard to trace & fix.

Suppose say you got an strange error which doesn’t give any hint of what it could be then what will you do?
Of course you will try to isolate the error, but how? Answer: By Process of Elimination

Steps:

Here are some tips for debugging in Magento by process of elimination (I would even say the renaming/commenting techniques):
1. Rename the current theme folder and check if error is eliminated or not
If error is eliminated then try to isolate the error by renaming the xml layout or .phtml template files
else go ahead with step 2.

2. Rename the app/code/local folder and check if error is eliminated or not
If error is eliminated then try to isolate the error by disabling the local modules one by one
else go ahead with step 3.

Some Notes:
Even you disable all the local modules using following code in app/etc/local.xml:

<disable_local_modules>true</disable_local_modules>

or
using following code in app/etc/modules/*.xml

<active>false</active> 

This will help you but not much. Suppose consider the case that you have core class overridden in app/code/local/Mage directory.
Do this kind of overriding require any xml declarations? Answer: Nope.
So in this case app/code/local folder renaming can be helpful instead of disabling the module via xml file.

3. Rename the app/code/community folder and check if error is eliminated or not
If error is eliminated then try to isolate the error by disabling the local modules one by one
else go ahead with scratching your head 😉

Suppose, for example, you find that the the main culprit was some community module. You should try to comment the different section of config.xml of that module. This will help you in locating the main culprit file. Once the culprit file is found then you can narrow it down by commenting different section of the code until you find that main single line of code. Once the main culprit line/code is found then it’s upto you how to fix.

Hope this article was useful.
Thanks for reading!

Clear abandoned cart items during login in Magento

May 2, 2012  |  4 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

You may have noticed that old cart items (abandoned) gets merged with the current one when customer tries to login.
It’s good in terms of sales but sometimes your merchant simply may not want this feature.
In this case, we can easily clear the abandoned cart items and stop being merged with the current cart with the following steps:

1. Hooking into the event: load_customer_quote_before
– Add the following code in file ‘app/code/local/MagePsycho/Module/etc/config.xml’:

<frontend>
	<events>
		<load_customer_quote_before>
			<observers>
				<module_load_customer_quote_before>
					<class>module/observer</class>
					<method>clearAbandonedCarts</method>
				</module_load_customer_quote_before>
			</observers>
		</load_customer_quote_before>
	</events>
</frontend>

2. Implementing logic in observer method
– Add the following code in file ‘app/code/local/MagePsycho/Module/Model/Observer.php’:

<?php
class MagePsycho_Module_Model_Observer
{
	public function clearAbandonedCarts(Varien_Event_Observer $observer)
	{
		$lastQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
        if ($lastQuoteId) {
            $customerQuote = Mage::getModel('sales/quote')
                ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
            $customerQuote->setQuoteId($lastQuoteId);			
            $this->_removeAllItems($customerQuote);

        } else {
            $quote = Mage::getModel('checkout/session')->getQuote();
            $this->_removeAllItems($quote);
        }
	}

	protected function _removeAllItems($quote){
        foreach ($quote->getAllItems() as $item) {
            $item->isDeleted(true);
            if ($item->getHasChildren()) {
				foreach ($item->getChildren() as $child) {
					$child->isDeleted(true);
				}
			}
        }
        $quote->collectTotals()->save();
    }
}

That’s it.

[EDIT]
Now you can download this trick as a Magento Module from [here]

Happy Coding!