Please Note: This post is more of an anecdotal post as opposed to a tutorial. There are no code samples and the actual code that I discuss may not be 100% accurate as I am typing this at the end of a 12 hour work day. I wanted to get this story out so if there are people out there with a similar problem, they can solve it quickly. If you need more solid code samples or guidance, feel free to comment on this post or email me at jpenn (at) bouncr.com and I will do my best to help you. Continue Reading…
New Happenings Coming Soon
I have been relatively quiet about my day job in the past. I was unsure if they would have an appreciation for sharing my knowledge and experiences. However, I have discussed my blog with the company I work for and they have given me their blessing to share more knowledge and some source code (with some modifications).
Continue Reading…
Getting and Setting Session Variables in Magento
Working in the Magento ecosystem can seem daunting at first. Even experienced PHP programmers have a steep learning curve to work through before they really get the sense that they know what they are doing.
One thing that is very easy to do in regular PHP programming is setting a session variable. All you really have to do is set the variable in $_SESSION and you are good to go. In Magento, you probably could do it this way, but you would probably get this feeling that you are doing it incorrectly (and you would be correct).
Luckily, setting session variables in Magento is only difficult until you see it once, and then it is just as easy as setting the variable in $_SESSION. As long as you know how to set variables on an object, you can set a session variable. Look at the following code:
Mage::getSingleton(‘customer/session’)->setMyCoolSessionValue(“variable”);
This piece of code sets the session variable my_cool_session_value to the value of “variable”. Getting the value back out, all you have to do is replace “set” with “get” and it will return the value.
Magento Debugging Basics
Magento is a big system. If you are developing for Magento it is inevitable that you are going to either need to trace down a big or just figure out how a particular process works.
The best thing to do is to start with the controller that is triggered when the process starts. If you are not able to figure out which controller you are starting from, I suggest you buy Commerce Bug by Alan Storm. It will take all the guess work out of that for you as well as tell you a bunch of other information about the page you are currently viewing.
Now that you know where to start, lets talk about the functions and commands I find most useful when debugging Magento.
echo “captured”; exit(); – This does exactly what it looks like. Echos out the word “captured” and stops execution. This is helpful so that you can verify that the code you think is running, is in fact running. It is also a good to use in conjunction with the other commands so you can stop the process without loading the layout or template of the site.
echo get_class($obj); – This will simply tell you the name of the class that $obj is. Since class names in Magneto (and the Zend Framework for that matter) tell you where the actual file is located, this is very helpful so you can follow the next step of the code.
print_r(get_class_methods($obj)); – get_class_methods returns a list of explicitly defines methods that $obj currently has. I say explicitly defines because it does not display functions that come from Magento’s magic functions (all the nice getters and setters).
debug_stacktrace() – This function is good when you know the end point of the code you want to debug but want to see where it was called from. This displays a complete stack trace of the code from the point this function is called.
print_r($obj->debug()); – Nearly every single object in Magento extends Varien_Object. One of the methods that Varien_Object has is debug. This function will show you the data that this object currently holds.
Those are the very basics, but you can trace Magento code very easily with these few functions. What other functions do you use to debug Magento?
Have Your Magento Caches Update Themselves
Magento has a lot of caches. They are (in my opinion) the biggest reason Magento is usable as a platform (If you think Magento runs slowly now – turn them off for a little bit).
One thing that I as a programmer find odd is that the caches in Magento are intelligent enough to flag themselves as invalidated, however they generally require that they be refreshed by hand. This can be especially irritating when you are running Magento Enterprise Edition and your full page cache is constantly being invalidated due to product and category updates.
In order to improve our site performance I wanted to find a practical way to run with the full page cache on all the time without requiring me to babysit it 24 hours a day. I figured I had to find the code that was triggered when you told the admin panel to update the cache.
It turns out that like most things in Magento, unless you know how it works, it is not immediately obvious where this code is located. One tool I have found invaluable is Commerce Bug by Alan Storm. This tool told me that the cache admin page is controller by the controller that is located at:
/app/code/core/Mage/Adminhtml/controllers/CacheController.php
Now in this controller you will find a number of actions. The one that is going to help us is massRefreshAction(). Inside you will see that there is code that actually refreshes the cache
$tags = Mage::app()->getCacheInstance()->cleanType($type);
This is close but not quite what we need yet. The $type is a string code for the cache. We don’t want to always refresh the cache. Just the ones that have been flagged as invalidated.
Looking deeper into the ->getCacheInstance() class that is returned shows us an interesting function that is available called ->getInvalidatedTypes(). BINGO! That is exactly what we are looking for.
Looking at what this function returns we find an array of objects that contain the codes of caches that have been invalidated. Now all we have to do is iterate through this list and refresh those caches.
The final script will look like the following:
$types = Mage::app()->getCacheInstance()->getInvalidatedTypes();
foreach($types as $type) {
Mage::app()->getCacheInstance()->cleanType($type->getId());
}
Now that we have a script that is refreshing the caches, we just need this script to run every so often and it will refresh any caches as required. I set it to run every 2 minutes, but your mileage may vary.





