iFUEL INTERACTIVE

A NY Interactive Agency and a Division of Agency212

See you on Flickr See you on Twitter See you on FaceBook Subscribe

Posts Tagged ‘tutorial’

Create a Module to Latch onto Magento Event
August 18th, 2010  |  3 COMMENTS  |  Development, Magento
Tags: , , , , , , , ,

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.

Tips and Tricks: Developing with Magento
April 5th, 2010  |  1 COMMENT  |  Development, Magento
Tags: , , , , , , ,

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.

Magento's Built-In Template Path Hints

To achieve this, Follow these simple steps…

  • Log in to your administration panel. Go to Sytem > Configuration.
  • From the left navigation column on top there will be “Current Configuration Scope”. In the drop down menu select “Main Website”
  • Then navigate to Developer > Advance, also in the left navigation column.
  • Under “Debug” you’ll see Template Path Hints. Select Yes and click the Save Config button.

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!

Categories
Archives
Tags
From Our Twitter (@ifuel)
Our Interactive Agency on Facebook
Copyright iFuel Interactive © 2010. All Rights Reserved.
Going up? Click here.A NY INTERACTIVE AGENCY