16
© 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

Embed Size (px)

Citation preview

Page 1: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Anatomy of a Magento Extension

Jon Saverda

Page 2: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Understanding Extension Flow

• Local • Community • Core (Please do not edit these)

Page 3: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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>

Page 4: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

It Works!

Page 5: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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>

Page 6: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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>

Page 7: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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!!!';}

}

Page 8: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Blocks<global> <blocks> <irishtitan_example> <class>Irishtitan_Example_Block</class> </irishtitan_example> </blocks></global>

Page 9: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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');}

}

Page 10: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Page 11: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

ModelModel - 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>

Page 12: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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();

Page 13: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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(); ?>

Page 14: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Page 15: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 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>

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();

}}

Page 16: © 2014 Irish Titan Anatomy of a Magento Extension Jon Saverda

© 2014 Irish Titan

Resourceshttp://www.silksoftware.com/magento-module-creator/http://magicento.com/