Archive for September, 2011

Custom Modules in Magento

Posted: September 8, 2011 in Uncategorized

We have been talking about Magento the recommended eCommerce solution and today we are learning on how to create a custom module. Let me first remind you about the Mage directory (short for Magento) found under the app/ directory which was earlier discussed in the intro of Magento which functions to simply add naming consistency to all classes under it.

 

Create a new module called “NewProduct”in a folder called “Company in App/code/local/

Here is the default structure to create when creating anew module

Under App/code/local/Company

Folder – “ModuleName”

  • Folder “Block”
  • Folder “controller”
  • Folder “Model”
  • Folder “Helper”
  • Folder “etc” – inside folder – config.xml file
  • Folder “sql”
When create a module remember that there are two parts of a module that are not self contained namely
  • Design (Template files and layout settings)
  • XML file which tells Magento  to urn-on your module
These are the basics for a working model.
<!--?xml version="1.0"?>
<config>
	<!-- turn on our module, required for install support -->
	<modules>
		<Company_NewProduct>
			<version>0.1.0</version>
		</Company_NewProduct>
	</modules>
	
	<global>
		<!--turn on models -->
		<models>
			<NewProduct>
				<class>Company_NewProduct_Model</class>
			</NewProduct>
		</models>

	<!-- utrn on models-->

	<blocks>
		<NewProduct>
			<class>Company_NewProduct_Model</class>
		</NewProduct>
	<blocks>

	<!-- trun on database connections -->

	<resources>
		<!-- setup is needed for automatic installation-->
		<NewProduct_setup>
			<use>default_setup</use>
		</NewProduct_setup>
		<NewProduct_write>
			<use>default_write</use>
		</NewProduct_write>
		<NewProduct_read>
			<use>default_read</use>
		</NewProduct_read>
	</resources>
	<global>
</config>
Create a file that will turn on the module this file will be created in App/etc/Modules/ folder named Company_Module.xml
<?xml version="1.0"?>
<config>
	<modules>
		<Company_ModuleName>
			<active>true</active>
			<codePool>local</codePool>
		</Company_ModuleName>
	</modules>
</config>

For your module to be turned on Magento would have to scan your  App/code/local/Company/etc/config.xml file which means we have to let Magento know about this in the top level file in the etc/Modules folder so lets save a file in App/etc/Modules/ called Company_All.xml and save the following code in it

<?xml version="1.0"?>
<config>
	<modules>
		<Company_NewProduct>
			<active>true</active>
			<codePool>local</codePool>
		</Company_NewProduct>
	</modules>
</config>

Now that you have your new module the system will then scan your module’s “etc” folder for any of the following xml files. 

  • config.xml (defines models, resources, and other settings for basic functionality)
  • system.xml (defines admin menus and default settings for forms)
  • convert.xml (works with the dataflow component for importing/exporting data)
  • install.xml (only used for one-time Magento installation)

In the second part of custom modules we discuss templated,layouts and overriding blocks