Anatomy of a Magento Extension

Anatomy of a
Magento Extension
Jon Saverda
© 2014 Irish Titan
Understanding Extension Flow
•
•
•
Local
Community
Core (Please do not edit these)
© 2014 Irish Titan
Starting your extensions
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Irishtitan_Example>
<codePool>local</codePool>
<active>true</active>
</Irishtitan_Example>
</modules>
</config>
© 2014 Irish Titan
It Works!
© 2014 Irish Titan
Finally getting started
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Irishtitan_Fabric>
<version>0.1.0</version>
</Irishtitan_Fabric>
</modules>
</config>
© 2014 Irish Titan
Setting up a controller
<config>
<modules>
.....
<frontend>
<routers>
<irishtitan_example>
<use>standard</use>
<args>
<module>Irishtitan_Example</module>
<frontName>example</frontName>
</args>
</irishtitan_example>
</routers>
</frontend>
.....
</modules>
</config>
© 2014 Irish Titan
The controller file
class Irishtitan_Example_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo 'You now have text on the page!!!';
}
}
© 2014 Irish Titan
Blocks
<global>
<blocks>
<irishtitan_example>
<class>Irishtitan_Example_Block</class>
</irishtitan_example>
</blocks>
</global>
© 2014 Irish Titan
/* Updated controller */
class Irishtitan_Example_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$block = $this->getLayout()>createBlock('irishtitan_example/example');
$this->getLayout()->getBlock('content')->insert($block);
$this->renderLayout();
}
}
class Irishtitan_Example_Block_Example extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate('irishtitan_example/example.phtml');
}
© 2014 Irish Titan }
© 2014 Irish Titan
Model
Model - This is where you place most of your business logic and also represents an entity in the db
● Models need four files in order to work properly
○ The model file
○ The model resource file
○ Model Collection File
○ Installation script
<models>
<irishtitan_example>
<class>Irishtitan_Example_Model</class>
</irishtitan_example>
</models>
© 2014 Irish Titan
SQL
$installer = $this;
$installer>startSetup();
$installer->run("
DROP TABLE IF EXISTS `{$this->getTable(example)}`;
CREATE TABLE `fabric` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`example_name` varchar(255) NOT NULL,
`example_img` varchar(1000) DEFAULT NULL,
`description` text,
`active` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
");
$installer->endSetup();
© 2014 Irish Titan
Helpers
<helpers>
<irishtitan_example>
<class>Irishtitan_Example_Helper</class>
</irishtitan_example>
</helpers>
class Irishtitan_Example_Helper_Data extends Mage_Core_Helper_Abstract
{
public function exampleHelper()
{
return 'Here is a example of the helper';
}
}
<?php echo Mage::helper('irishtitan_example')->exampleHelper(); ?>
© 2014 Irish Titan
© 2014 Irish Titan
Events / Observers
<adminhtml>
<events>
<catalog_product_save_after>
<observers>
<irishtitan_example>
<class>Irishtitan_Example_Model_Observer</
class>
<method>sendProductToThirdParty</method>
</irishtitan_example>
</observers>
</catalog_product_save_after>
</events>
</adminhtml>
© 2014 Irish Titan
class Irishtitan_Example_Model_Observer
{
public function
sendProductToThirdPartyAction($observer)
{
/* Here is the code to send a product to a
third party */
$product = $observer->getEvent()->getProduct();
}
}
Resources
http://www.silksoftware.com/magento-module-creator/
http://magicento.com/
© 2014 Irish Titan