How to Learn Magento Development Step by step

how install magento theme and how magento indexing works and how magento search works and how install magento on localhost, how to magento aws
Prof.SteveBarros Profile Pic
Prof.SteveBarros,United Kingdom,Teacher
Published Date:28-07-2017
Your Website URL(Optional)
Comment
Documentation for Magento Developers A Getting Started Guide to Developing Magento Extensions. Mark Sanborn, Mark Lehm, Ryan Leising, and Richard FowlerChapter 1 Introduction As you probably know Magento is one of the best open source ecommerce platforms out there. It is packed full of great features and allows for almost unlimited flexibility and customization; however, the Magento code base is vast and there is very little current documentation on developing custom extensions. Prior to this book many developers come into the Magento world and immediately find themselves puzzled by a shortage of documentation. The process that many developers take at the beginning usually begins by Google search which almost always turns up a stale blog post on how to implement a specific customization to Magento. Upon further searching developers quickly realize that there is a lack of good guides on how to get started creating a new extension. This is where we come in. We want to take away that two week pain period that Magento developers often face and replace it with a guide that will introduce you to everything you need to build your extension paired with a quick “Hello World” extension walk-through that will enable you to build your first extension in a matter of minutes. See: Chapter 3: Your First Extension About the Authors Mark Sanborn Mark is able to use his developer skills and business knowledge to see pain points and develop simple solutions to complex problems. Mark Sanborn has been writing developer tutorials on his blog to an audiance of a million viewers for over five years. Mark also developed the product, RocketShipIt, that makes integrating custom shipping rates, tracking, and labels on the web easy. 6Mark Lhem Web/UI designer, in-house developer and systems administrator, Mark Lehm is a web design professional offering broad and diverse technical and project leadership talents in full life cycle design and development of corporate websites and web marketing tools. Richard Fowler Richard has extensive knowledge in content, SEO/SEM and UX. He is a content strategy and user experience design veteran with expertise in integrating brand strategy, information architecture, SEO and SEM to advance clients’ business goals. What This Book is Not This book is not intended to be an ultimate reference to every object, class, function, and feature of Magento. Rather it is a no nonsense concise guide to get you developing Magento extensions as fast as possible. We believe you will be better off knowing how to access this information through code than to look it up in a reference guide that may become stale or obsolete when Magento releases a new version. Assumptions We are going to assume you are familiar with the command line, PHP, XML, Apache and setting up a basic Magento installation. Feedback If any of the examples in this book are unclear, you find a typo, or just wish the book contained a chapter on subject X please don’t hesitate to email us at: supportvonnda.com supportvonnda.com. 7Chapter 2 Prepping Your Magento Enviornment for Development What we hope to accomplish in this section is to turn your Magento installation into an effeciant development enviornment. This enviornement will make your life easier by turning off caching turning off caching so you can see changes immediately, turning on error reporting turning on error reporting so you can see what went wrong, turning on logging turning on logging so you can capture valuable debug information, and other helpful things like extending the admin session extending the admin session so you don’t have to constantly log in and a whole lot more. Installing Magento We are going to assume you have the latest version of Magento installed. Start by downloading and installing the latest release of Magento from magentocommerce.com. MySQL Tools Apart from the actual MySQL server, it is useful to have a GUI client to inspect Magento’s database tables from time to time. The tool we most often use around the office is Sequel Pro www.sequelpro.com. You can also use phpMyAdmin over Sequel Pro if you are used to that. 8Setting the Time The first thing we want to do is make sure that our Magento installation and server have the approriate date and time. If your server time is too far into the future or past it breaks a If your server time is too far into the future or past it breaks a core functionality in Magento and in some cases prevents you from being able to log in core functionality in Magento and in some cases prevents you from being able to log in. Make sure your date/time is properly set by opening a terminal on the server hosting Magento and type, date. If your date or time is off, this command will get it back in sync if you have ntpdate ntpdate on your system: sudo ntpdate us.pool.ntp.org 9Turn on Symlinks Magento expects your extension files to be scattered around in different folders. This not only causes developer frustration it makes keeping your extensions under version control a pain. Later in the book we will show you how to easily manage your extensions with version control via symlinks. For this to work we need to turn on symlinks. To turn symlinks on for template files: System System Configuration Configuration Developer Developer Template Settings Template Settings Make sure Allow Symlinks is set to Yes Yes Turn Caching Off Nothing is more frustrating than making code changes expecting a different result only to find out that caching has been turned on the whole time. When developing your extensions you should always have caching turned off always have caching turned off. Login to the backend, go to: System System Cache Management Cache Management Select All Select All Disable Disable Submit Submit Click Flush Magento Cache 10Turning Error Reporting On By default Magento tries to suppress error messages. In a production enviornment this is a good thing; however, for developing we need error reporting enabled. Open up index.php and uncomment: ini_set('display_errors', 1); Then set Magento to Developer Mode by changing: if (isset(_SERVER'MAGE_IS_DEVELOPER_MODE')) Mage::setIsDeveloperMode(true); to this: //if (isset(_SERVER'MAGE_IS_DEVELOPER_MODE')) Mage::setIsDeveloperMode(true); // Note: You could also set the _SERVER variable but we found this method unreliable. Turn Logging On We also need to have logging turned on. System System Configuration Configuration Developer Developer Expand the Log Settings Log Settings. Change to Enabled Enabled and leave the names default. Extend the Session Timeout Magento has a default session timeout of 15 minutes 15 minutes which means you will be logged out of the admin backend every 15 minutes which is quite annoying during development. Extend the timeout by going to: System System Configuration Configuration 11System System Advanced Advanced Admin Admin Expand Security Security Default is 900 seconds which is 15 minutes. Set Session Lifetime (seconds) to 86400. This will log you out of your current session but next login will stay connected for 24 hours. Depending on your server configuration you may also need to set your maxlifetime session expiration time in your php.ini file for this to have an effect. php_value session.gc_maxlifetime 86400 Create a Restore Point with Magentomatic In this section we will breifly introduce Magentomatic and make a backup of your Magento installation so you have a clean database of a fresh Magento installation if you ever need to roll back for any reason. Magentomatic is a tool developed by Vonnda to automatically manage common developer tasks in Magento like: • adding an admin user • backing up the database • restoring the database • deleting users/products/orders • setting config values • clearing the cache • verifying integrity of core files • and more Make a Database Backup Make sure you have Magentomatic extracted into your Magento installation. Change directories to magentomatic php magentomatic.php backup 12Chapter 3 Your First Extension Now that you’ve gotten a start on basic setup and configuration of your magento installation, we’re going to throw you right into the thick of it and guide you through building your first extension. Don’t worry, there will be explanations along the way, and later chapters delve deeper into the inner workings. Hello World We are first going to initialize our extension manually so you can see how the process works. Later we will show you how we can use a tool to create these folders and files automatically. Step 1: Create a “Mycompany” folder under app/code/local app └── code └── local └── Mycompany Step 2: Create a “Myfirstmodule” folder. app └── code └── local └── Mycompany └── Myfirstmodule Step 3: Create the following folders inside (case matters): Block, controllers, etc, Helper, Model, sql. app └── code └── local └── Mycompany └── Myfirstmodule ├── Block ├── controllers ├── etc 14├── Helper ├── Model └── sql Step 4: Under the etc folder create the file, config.xml config.xml. app └── code └── local └── Mycompany └── Myfirstmodule ├── Block ├── controllers ├── etc │ └── config.xml ├── Helper ├── Model └── sql Step 5: Fill config.xml config.xml with: ?xml version="1.0"? ?xml version="1.0"? config modules Mycompany_Myfirstmodule version0.1.0/version /Mycompany_Myfirstmodule /modules /config Step 6: Under, etc/modules create the file, Mycompany_Myfirstmodule.xml Mycompany_Myfirstmodule.xml app ├── code │ └── local │ └── Mycompany │ └── Myfirstmodule │ ├── Block │ ├── controllers │ ├── etc │ │ └── config.xml │ ├── Helper │ ├── Model │ └── sql └── etc └── modules └── Mycompany_Myfirstmodule.xml 15Step 7: Fill Mycompany_Myfirstmodule.xml Mycompany_Myfirstmodule.xml with: ?xml version="1.0"? ?xml version="1.0"? config modules Mycompany_Myfirstmodule This activates the module, flip to false to deactivate activetrue/active codePoollocal/codePool Remember we talked about code pools /Mycompany_Myfirstmodule /modules /config Step 8: Make sure Magento is loading your extension, System Configuration Advanced If you see the name of your module listed it means you have followed the above steps correctly and Magento has loaded it. Congratulations your module is installed Write Your First Controller Step 1: Modify your config.xml config.xml file in your module’s etc folder to include a new frontend router: ?xml version="1.0"? ?xml version="1.0"? config modules Mycompany_Myfirstmodule 16version0.1.0/version /Mycompany_Myfirstmodule /modules frontend routers myfirstmodule usestandard/use args moduleMycompany_Myfirstmodule/module frontNamemyfirstmodule/frontName /args /myfirstmodule /routers /frontend /config Step 2: Create a new file, IndexController.php IndexController.php under your controllers folder and fill it with this: class Mycompany_Myfirstmodule_IndexController extends Mage_Core_Controller_Front_Action public function indexAction() echo 'Hello World'; Note: Our function has the name indexAction, the first part ‘index’ will be used as part of the url. Step 3: Go to http://yourmageinstallation/myfirstmodule/index/index/ Go to http://yourmageinstallation/myfirstmodule/index/index/ Function name minus 'Action' Controller name minus 'Controller.php' frontName in config.xml Note: Since this is our index controller you could also reach this url here: http://yourmageinstallation/myfirstmodule/ http://yourmageinstallation/myfirstmodule/ 17GET and POST Variables You can gain access to both get and post variables with: params = this-getRequest()-getParams(); So if we were to change our “Hello World” message in the IndexControler.php file to: public function indexAction() print_r(this-getRequest()-getParams()); echo 'Hello World'; and go to the following url: http://yourmageinstallation/ http://yourmageinstallation/ myfirstmodule/?myvariable=22&another=something myfirstmodule/?myvariable=22&another=something , we would see: Array ( myvariable = 22 another = something ) Hello World Notice what happens when you go to this url: http://yourmageinstallation/myfirstmodule/ http://yourmageinstallation/myfirstmodule/ index/index/id/22 index/index/id/22 Array ( id = 22 ) Hello World This means we can also pass get variables to Magento with clean seo friendly urls Go ahead and create a customer on the frontend and look up your newly created customer’s ID. 18Lets now modify our controller slightly: public function indexAction() params = this-getRequest()-getParams(); customer = Mage::getModel('customer/customer')-load(params'id'); echo 'Hello, '. customer-getName(); Now go to the url substituing the id with your newly created customer’s id: Mine: http://yourmageinstallation/myfirstmodule/index/index/id/6 http://yourmageinstallation/myfirstmodule/index/index/id/6 Congratulate yourself, you now have a working url that accepts get parameters and loads your first Magento customer object. When developing your extensions you will primarily deal with customer, order, and product objects. Writing Output to the Log So you have loaded your first Magento customer object but it is not fair, we told you what to put in your controller. How were you supposed to know that customer-getName(); would produce the customer’s name? What else can we do with customer objects? 19We would like to take this opportunity and introduce the Magento log and show you how you can determine what data you have access to. Make sure your logs are turned on from Prepping Your Magento Enviornment for Prepping Your Magento Enviornment for Development Development chapter. Modify your controller and add: Mage::log(customer); public function indexAction() params = this-getRequest()-getParams(); customer = Mage::getModel('customer/customer')-load(params'id'); Mage::log(customer-getData()); echo 'Hello, '. customer-getName(); Open up var/log/system.log and scroll to the bottom: When developing you will probably want to have this refresh automatically when new output is appended to the log. On Unix like systems you can use tail tail -f var/log/system.log Explore Magento Objects/Snippits Take this time now to play around with some of the snippits in the Recipes chapter and try to output various magento objects from the controller or output them to the log. Example public function indexAction() customer = Mage::getModel('customer/customer')-load(params'id'); print_r(customer-getData()); // outputs customer data Mage::log(get_class_methods(customer)); // writes available customer methods to log A Simple Admin Interface We are now going to add a super simple admin interface to our extension. In later chapters we will explore the admin area in more detail. Create the file system.xml under the etc folder: 20?xml version="1.0"? ?xml version="1.0"? config tabs mycompany_extensions translate="label" labelMy Companies Extensions/label sort_order210/sort_order /mycompany_extensions /tabs sections mymodule translate="label" labelMy Module/label tabmycompany_extensions/tab frontend_typetext/frontend_type sort_order1000/sort_order show_in_default1/show_in_default show_in_website1/show_in_website show_in_store1/show_in_store /mymodule /sections /config At this point we have a menu section and tab for our extension in the backend; however, if we were to try to view it we would see a 404 error. This is Magento’s way of saying that we don’t have permission to view this section. It is one of the common idiosyncrasies of working with Magento extensions. To add the appropriate permissions add the file adminhtml.xml under the etc folder. ?xml version="1.0"? ?xml version="1.0"? config acl resources all titleAllow Everything/title /all admin children system 21children config children mymodule translate="title" titleMy Module/title sort_order100/sort_order /mymodule /children /config /children /system /children /admin /resources /acl /config Now logout to make sure Magento refreshes permissions and you should see your module and admin section. We are now ready to add configuration settings for our extension. Add in the following xml into system.xml under configsectionsmymodule groups Group of config options general translate="label" Title of group labelGeneral/label frontend_typetext/frontend_type Sort order sort_order10/sort_order Visibility options show_in_default1/show_in_default show_in_website1/show_in_website show_in_store1/show_in_store Expand group area by default expanded/ Fields go here fields 22enabled translate="label" Label for field labelEnabled/label Type of field frontend_typeselect/frontend_type source_modeladminhtml/system_config_source_yesno/source_model sort_order1/sort_order show_in_default1/show_in_default show_in_website1/show_in_website show_in_store1/show_in_store /enabled /fields /general /groups At this point you should have a General group with a single field called enabled You can access the value of this field using getStoreConfig(). The path will follow your xml under the sections element. Mage::getStoreConfig('myfirstmodule/general/enabled'); For fields that are boolean Magento also gives us the function getStoreConfigFlag() Lets modify our controller to include our new field: class Mycompany_Myfirstmodule_IndexController extends Mage_Core_Controller_Front_Action public function indexAction() // Check to see if module is enabled if (Mage::getStoreConfigFlag('myfirstmodule/general/enabled')) params = this-getRequest()-getParams(); customer = Mage::getModel('customer/customer')-load(params'id'); Mage::log(customer-getData()); echo 'Hello, '. customer-getName(); 23 else echo 'This module is disabled.'; Now try toggling the enabled field in the admin backend and see how your controller responds. You now have a small taste for all of the essential bits that make up a full Magento module. These next chapters will be devoted to diving into each section in more detail. 24Chapter 4 Understanding the Magento Directory Structure Code Pools Magento Extensions are located in one of three code pools. The code pools are: core core, local local, and community community. All of the modules distributed with the base Magento are in the core code pool. All of the custom modules that you develop can be installed in the local code pool. Although you can install any module in local as well as community it is best to put third- party modules under community and change their xml file accordingly to keep third party modules from overriding the files you are trying to override in local. Magento will first look in local local, then community community, then core core. app/ └── code ├── community ├── core └── local Sample app directory folder app/ ├── code │ └── local │ └── Yourcompany │ └── Yourmodule │ ├── Block │ ├── controllers │ │ └── IndexController.php │ ├── etc │ │ └── config.xml │ ├── Helper │ ├── Model │ └── sql └── etc └── modules └── Yourcompany_Yourmodule.xml 25

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.