Magento uses caching extensively. If you also want to be able to save something to cache rather than reading from the database or some more expense resource like that, it’s simple:
$value = Mage::app()->loadCache(‘my_cache_key’);
If the key isn’t found, $value will be null.
To store a value in the cache is almost as easy:
Mage::app()->saveCache($data_to_store, ‘my_cache_key’, array(), $duration_in_seconds);
(The empty array in the call above is standing in for a parameter called “tags” which, to my knowledge, is not used. Someone please correct me on that if I’ve got it wrong.)
Sometimes I’m banging my head against the wall trying to figure out why my block isn’t loading right or some other layout element isn’t rendering. And I’ve always wished I could just see the layout as Magento is seeing it. So I finally dug in to see and it’s actually incredibly simple to output the layout for a given page in xml format.
echo Mage::app()->getLayout()->getNode()->asNiceXml('', 0);
The layout is just a Varien_Simplexml_Config. getNode buys you access to the private $_xml variable that holds the root node of the xml document. Then asNiceXml formats it nicely for the screen. If you’d rather output to a file (they can be large chunks of xml) supply a file name for the first parameter of asNiceXml and the xml will be written there, too.
Now back to why my block isn’t loading…
Sometimes Magento does something and it’s just not obvious why. We’ve all heard “why aren’t my products showing up?” more times than we can count. Magento’s object model for building queries is great, but sometimes you just need to see the sql that’s being run on the database in order to know what’s really going on.
In the past I’ve used the general log in mysql. That can be a great source of information, but, depending on the version of mysql, it can be a pain to set up. Other times I’ve inserted logging code into various db classes in order to print out queries that are being run, but I always have to find the right place again. But today I stumbled on this blog entry - http://blog.nexcess.net/2011/04/15/logging-database-queries-in-magento/ that spells out exactly how you can turn on some (as far as I know) undocumented logging functionality that is built right in to the Magento data access layer. Kudos to the author and thanks for sharing!
On the eve of my birthday, I must say, I have been inundated with coupons and special offers for my use, and as a consumer, I have taken advantage of almost all of them. Seriously, who doesn’t love celebrating their birthday?
I have received dozens of e-mails from many of the stores and brands I like to shop at, and many of them offer either an amazing coupon, or a free gift for my birthday.
I like to have all Magento data re-indexed every night via a cron job. That way I KNOW the indexes are up to date.
To create the cron job, add the following to your cron file to reindex every day at 4am
0 4 * * * php -f /shell/indexer.php reindexall
Note: If you get an error telling you you’re out of memory similar to:
PHP Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 7680 bytes) in …/app/code/core/Mage/Index/Model/Indexer.php on line 163
Try commenting out php_value memory_limit and php_value max_execution_time in your .htaccess file.
Recently we had to import a list of newsletter subscribers into Magento. We had already loaded all the customers, but now we needed to subscribe all of the customers that had subscribed on the customer’s old site on the new. The following code (simplified for display here) did the trick!
<?php
define('CRLF', '\r\n');
require_once("<path to your app folder>Mage.php");
Mage::app();
$email = "joe@blow.com";
// THE "EASY" WAY (but sends a confirmation email to the customer
$subscriber = Mage::getModel('newsletter/subscriber')->subscribe($email);
// THE "HARD" WAY (Doesn't send confirmation email to customer)
// load up the customer we want to subscribe
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail($email);
// if we found the customer
if ($customer->getId()){
// load up the subscriber if possible
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if (!$subscriber->getId()
|| $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED
|| $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setSubscriberEmail($email);
$subscriber->setSubscriberConfirmCode($subscriber->RandomSequence());
}
$subscriber->setStoreId(Mage::app()->getStore()->getId());
$subscriber->setCustomerId($customer->getId());
try {
$subscriber->save();
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
Latching onto magento events is very simple – however, after a lot of searching I couldn’t find a lot of resources documenting how to do it.
I’ll more than likely research magento events a bit more in a later post, but for reference here is a cheatsheet of dispatched magento events Credit: Branko Ajzele of Active Codeline. (a little outdated – from version 1.3)
Let’s keep it simple…
The namespace for my module will be “Ifuel”
The name of my module will be “Aftercheckout”
I will want to latch onto one of the many dispatched events during the checkout process (to name a few: checkout_controller_onepage_save_shipping_method, sales_order_place_after, checkout_type_onepage_save_order_after, checkout_onepage_controller_success_action). For reference about checkout events, check out Yireo’s Events with Magento Checkout. For this module, I want to execute my class after the order has been completely checked out – so I will latch onto the “checkout_onepage_controller_success_action” event.
Here is the bare bones of my config.xml file located at /app/code/local/Ifuel/Aftercheckout/etc/config.xml. Make sure to read the comments in the code, they’re helpful. P.s. don’t forget to declare your module in /app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Ifuel_Aftercheckout>
<version>0.1.0</version>
</Ifuel_Aftercheckout>
</modules>
<frontend>
<routers>
<aftercheckout>
<use>standard</use>
<args>
<module>Ifuel_Aftercheckout</module>
<frontName>aftercheckout</frontName>
</args>
</aftercheckout>
</routers>
</frontend>
<global>
<events>
<checkout_onepage_controller_success_action>
<!-- The name of the event you're latching
onto, all lowercase -->
<observers>
<sendaftercheckout>
<!-- Some description for your event
listener, doesn't matter what it is
as long as there are no spaces and
it's all lowercase -->
<type>singleton</type>
<class>aftercheckout/doaftercheckout</class>
<!-- the <frontName> of your module
(aftercheckout) slash the name of your Event
Listener (doaftercheckout), case sensitive.
In this case it will be located at
/app/code/local/Ifuel/Aftercheckout/Model
/Doaftercheckout.php -->
<method>sendSomethingAfterCheckout</method>
<!-- the public function name within your
class that will listen for this event,
case sensitive -->
</sendaftercheckout>
</observers>
</checkout_onepage_controller_success_action>
</events>
</global>
</config>
Now you’ll want to create your Listener. In my case it will be located at /app/code/local/Ifuel/Aftercheckout/Model/Doaftercheckout.php. Make sure to create a public function within your new class that you’ve defined in the <method> node of your config.xml file. The contents of my listener will look something like:
<?php
class Ifuel_Aftercheckout_Model_Doaftercheckout {
public function sendSomethingAfterCheckout() {
Mage::log("I am now able to execute something after checkout");
}
}
And that’s it. That’s the bare bones of latching onto a magento event. It’s that simple. If you have any questions, feel free to ask them in a comment.
Things you Should Know Before Developing With Magento
—————————————————————-
Now being more experienced with Magento we definitely feel the need to share our findings with the masses. We only wish we knew these things prior to trying to the hack the crap out of magento to make it do what we want.
Turning on Template Path Hints
If you’re new to Magento you will realize that it is quite a hassle to understand the logic behind the file structure and block structure of a magento layout when trying to customize your storefront. There is a very under-publicized built-in magento feature that we’d like to share with you that should help you on your journey. Magento actually has the ability to display hints showing where the different files of your layout is contained so that you can edit it.
To achieve this, Follow these simple steps…
Now when you navigate to your storefront you’ll see a bunch of red boxes displaying the underlying structure of the pages regarding templates and blocks.
Note: This should only be used in a development environment, considering this will make your storefront look hideous with big red blocks everywhere.
—————————————————————-
Never edit core files
Problem: When developing with Magento you will find it necesary to edit core files to achieve certain functionality. However, By editting a core file you are basically blocking yourself into a corner. DON’T DO IT! If you ever wish to upgrade at any point in the future you will not be able to because any changes that you have made in the core files will be overwritten. Never fear, there is a way around this.
Solution: The “local” folder (\app\code\local\) is your saviour. Say you need to edit the “Shipping.php” file located at “\app\code\core\Mage\Shipping\Model\Shipping.php” you can create the same basic directory structure in the local folder and copy and paste the Shipping.php file into the new directory. So, your new “local” Shipping.php file will be located at \app\code\local\Mage\Shipping\Model\Shipping.php. You will be able to safely edit anything and everything within that local file and never have to worry about it being overwritten during an upgrade. This trick works because Magento will look for files in a local directory before looking for a file in the core directory.
Note: The above solution will only overpower files that exist within the \app\code\core\ or \app\code\community\ directories. Also note that any files within your magento theme (\app\design\frontend\default\YOURTHEME\) or (\skin\frontend\default\YOURTHEME\) are not considered core files and you are free to edit them as you please.
These are just to name a few… Follow our blog to read more!
In this day and age of many online scams it is necessary for every successful eCommerce storefront to have SSL Authentication for online purchases. These SSL Certificates are usually only good for one specific URL and are very picky as to whether or not the URL has to have “www.” attached to it. Here is a simple solution that we’ve created to force appending “www.” to the URL when trying to access a “https://” (http secure) address.
Some things you must know:
First, Create an .htaccess file if you don’t already have one. Then enter one of the following solutions based on your needs.
# Redirect non-www to www
Options +FollowSymLinks
# turn mod_rewrite on
RewriteEngine On
# If https send to https://www.
RewriteCond %{HTTP_HOST} ^YOURDOMAIN.COM$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.YOURDOMAIN.COM/$1 [R=301,L]
# Redirect non-www to www
Options +FollowSymLinks
# turn mod_rewrite on
RewriteEngine On
# If https send to https://www.
RewriteCond %{HTTP_HOST} ^YOURDOMAIN.COM$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.YOURDOMAIN.COM/$1 [R=301,L]
# If http send to http://www.
RewriteCond %{HTTP_HOST} ^YOURDOMAIN.COM$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://www.YOURDOMAIN.COM/$1 [R=301,L]
With these examples it is necesary for you to make sure that you replace “YOURDOMAIN.COM” to reflect the actual domain name of the website you will be using this on. Once you have done this, save your .htaccess file and upload it to the base directory of your website.
Believe it or not a few search engines distinguish your website as two different websites http://www.yourdomain.com and http://yourdomain.com. It is often considered good practice to add this htaccess rewrite to any website you create so that you can rest assured that your website is not being indexed as two different websites.