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"?>
	<!-- turn on our module, required for install support -->
		<!--turn on models -->

	<!-- utrn on models-->


	<!-- trun on database connections -->

		<!-- setup is needed for automatic installation-->
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"?>

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"?>

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

Magento Database Design

Posted: August 26, 2011 in Uncategorized

The Magento database is a very complex one with over 200 tables (250 to be exact) and used a resource.


Resources which are usually defined under any global xml tag of any config file  play the role of managing Magento database connections.

 Magento Models  

All models extand the base Mage_Core_Model_Absract class and this class is very helpful when models save  themselves to the database. How do models save themselves ? Good question I thought you’d never ask ! Well when saving,  the model calls up its own resource singleton and pass itself($this) to the resource’s  save method and thereafter the resource is scripted to collect any values from the models  internal_data array which and prepare an insert or update statement using those values.

EAV Design

The Entity attribute Value design is very different from tradition database designs in that it works in a  vertical modeling instead of a horizontal modeling. This design allows for you to have meta-data about the  attribute which makes it define each attribute in more detailed manner.


Magento defines Entities as being a core thing that is being modeled in other words they are objects that can  be defined like a product which can be defined using attributes and by default Magento comes with 26 entity  types installed. Entities also extend resources. An entity basically works with models to save data to a number of different tables.

Entity Attributes

Entity attributes serve as instructions for saving and loading attribute information and have a lot more  meta-data then a normal object property. They specify as to which table the data will be  saved and are used for displaying information on the frontend.


When working with databases there comes a time when you have to write sql queries to retrieve data from the database, and since we have already discussed entities which are designed to load one item/record at a time, this then means we have a problem since we would have to know the records primary ID value to load it.

So what if you want to search the database to match a criteria ? A simple select statement with a where clause using a join is probably at the tip of your mind right !. Well since we are dealing with entities things are not all cut and dry because not all data that belongs to an entity is stored in one Table, “but that could be solved by the JOIN” you say ! yes it could but another problem to consider is that if we upgrade Magento to a newer version we are not guaranteed it will stay the same and this is the essence of collections .

Collections come in two varieties

  • Resource Collections
  • Entity Collections
AddAttributeToFilter is probably the most useful method of a collection because this method takes an attribute code and a condition (as demonstrated below).
$product = Mage :: getModel(‘catalog/products’) -> getcollection();
foreach($products as $_prod) {
In the example above we have a condition that is a simple string, which can also be an array. You can see in the second line of code that we are using the method  “addAttributeToFilter” which takes the attribute code ‘9999’.
During this Magento tour we will discuss in further details the database, the modules and more.


Google Webmaster Tools

Posted: August 19, 2011 in Google

Google has quite a lot of products from Google talk to Google Mail and a lot more. One tool I would like to briefly talk about is Google webmaster tools which basically is a tool Google offer you to be able to see how Google views your website. Within this tool there are a lot of features that provide you with different information about your sites performance. Every time you Google search, Google has what they call Google-bots that crawl sites looking for your search term on every site. If you have a webmaster tools account then Google will send you an email if they ever come across any error while crawling your site. Webmaster also provides a report that lists all search queries where your site came up on the results page.

Webmasters has a set of reports it provides the user to analyse their site performance.


Sitemap’s are your way of telling Google about your website pages including those that Google might not already know about

Search queries

This gives you a list of all search terms that your  site get listed for in Google Search Results Page including their impressions and Average positions

Crawling stats

A statistics report after Google crawls your site

HTML suggestions

This report deals with you content. When Google-bots crawl your site any issues found with your content gets listed on this report.

Links to your site

A list of sites that link back to your site with page they point back to on your site


A list of common keywords Google found when crawling your site

Internal links

A list of all internal links found on your site

Subscriber stats

Here you find a list of all users who have subscribed to your feeds using any Google product.

Webmaster Tools is Google’s way of communicating with your about your site to better manage your organic traffic and land on better Google Search Result pages.


Magento Modules

Posted: August 11, 2011 in Magento, PHP

Magento consists of modules and as a result modules are the core of Magento. When you consider every action that takes place in Magento both Front/Back-end, they all go through a module.  Modules act as containers for one or more of the following : Settings, Database schemas,   rendering objects, Utility helpers, data models, or action controllers.Modules can be made up of all six of these or just one.

Magento has come up with a way to have a well designed code base and still have it extendable by creating Code Pools which are folders/directories that contain modules in Magento and they consist of three code pools local, community and core. All custom made modules you develop can be installed in the local code pool, all modules distributed with the base  Magento are in the core code pool and the community code pool was initially for third party modules but they can now be installed on both local and community.

Models are also a very important aspect of Magento as they are the muscles. They mainly help with moving data from the database to the program itself and they are also responsible for manipulating the data.

Blocks help with Magento’s templating scheme. They are a HTML and PHP mixed file with phtml extension and each block controls one template file. All blocks extend the base class Mage_Core_Block_Template which extends Mage_Core_Block_Abstract.

Controllers in Magento are the starting point for all the business logic. Controllers extend the base class Mage_Core_Controller_Varien_Action which has three important classes :

  • dispatch($action)
  • preDispatch()
  • postDispatch()
Helpers are pretty much standard in every thing you do or find and the same is applied to Magento as they are a way to restructure an existing body of code out of the core classes.
The two major methods of helpers to look out for are :
  1. __ (two underscores)
  • This method is a translation helper
  1. htmlEscape
Config files
There are three different config files available (config.xml, system.xml and convert.xml) and they are found in the etc folder under the main directory all of which are XML. config.xml is the only config file that directly affects your modules behavior.

CSS Layouts

Posted: August 3, 2011 in Uncategorized

We recently touched on the introduction to CSS where we discussed the basics of what cascading style sheets are and the concepts used. Today we advance to another section on CSS where we discuss the types of layouts normally used in everyday life by designers.

CSS has four types of layouts commonly known by designers which are :


Liquid layouts unlike fixed layout expand to fill the browser as you re-size it. Columns will typically have widths declared in percentages.


  • Adapts to suit the viewing space


  • As the browser screen resolution increases it becomes harder to manage sentences that fit the horizontal page on large screen resolution computers


Things start getting interesting when it comes to the elastic as the names suggests because here you can define minimum and maximum width  using pixels, ems or percentage


  • The advantage about this one is how the layout can scale when text is resized

Variable fixed width

This layout is great because it adjusts automatically to best accommodate the users window size

Fixed width

This layout always has its total width including the width of its columns defined using static width measurements, normally pixels. A fixed width layout remains its set width regardless of whatever you do to it and does not stretch to fill the browser window.


  • With a fixed windows you are sure and certain that your design will not be compromised should the browser be re-sized especially if you have carefully measured internal elements like banners,carefully positioned text or images.


  • What ever width you set for your design will be served to everyone meaning if you set a 780-pixel it will probably look good on a 800*600screen resolution however if the resolution is 1280*1024 then the problems start kicking in and you design quality is compromised
On other news CSS3 (will be discussed further on this blog) is now on the shelves (on the net to be exact) so manipulating these layouts has become a whole new experience for designers.
The great thing about CSS is the fact that you rely less on the html and it gives you full control over your payout properties and positioning of text and images on your page.
With that in mind choose carefully what layout method you implement for you site and Happy cascading to all !!


Posted: June 23, 2011 in Uncategorized

Here’s a quick intro to CSS (Cascading Style sheets)

Previously i wrote about html5 which is great but how ever as great as html5 may be alone it is not much because css makes html stand out. Now to be able to make html5 look wonderful CSS is needed. Ofcourse if talking about html5 CSS3 is mentioned most often but before CSS3 you need to know where CSS comes from its ground roots. Just as you need to know the roots to figure out a tree CSS goes before CSS3 which is most recent.

CSS came about to complement html and make it look presentable to the web because html alone was just too basic for anyone to notice.

HTML (Hyper Text Markup Language) is a web language commonly used to create websites in using a combination of lists, forms, text, tables etc. It was then that CSS came about because  they wanted the tables to be colorful, the text to have different fonts and sizes, pages to have background images etc. Now with this in mind a few methods where then invented to be able to apply CSS to (X)HTML.

The following examples will make use of this html structure

Inline Styles

This method makes use of CSS attributes within the document

<p style=”color: #F00″>Paragraph</p>

This is not the best example you will find because it hinders with the html presentation of your document when you have more content on your page to style

Embedded Style

This method continues to make styling within the document but this time grouped in the head section of your document

<style type=”text/css”>p {color: #F00;}</style>

This method is more exceptional at times compared to inline but again will clutter the html when you need more pages and more styling.

External Styles

This method makes use of CSS styling from a separate file with .css extension (default.css)

This is how you link to the file from the head of your html document

<link rel=”stylesheet” type=”text/css” href=”default.css” /> provided you created a file called default.css ofcourse.

Within the default.css file insert the following code : p {color: #F00;}

This is the most common used method by designers because it separates the html from the css makes your html presentable and css grouped together all in one file.

# All three example do the same thing using different methods.

Now that we know all three method things should go much faster. External style are a great way to design websites and have another feature to them to make life even easier and its called Import rule. This is was not meant to be part of the html side of things but rather to help import other style sheets to one master style sheet instead of rewriting the style and calling another style sheet file.

Create another style sheet (eg external.css) and add some a few CSS rules

Open your master.css file and add the following : @import url(“external.css”);

What this does is it imports all the rules on your external css to default.css.

With these three methods all in place we are ready start getting to know CSS a bit better. On the next post we are going to be speaking a bit more on what CSS can do for your site and eventually get into the more advanced concepts of CSS.

See you then !!

Magento Promotions

Posted: June 15, 2011 in Uncategorized

To all those who have had the opportunity to play around with Magento i’m sure you would agree with me when I say its a playground with mysteries. Currently leading in its league the features available are out of this world. Business owners are well aware of how promotions help attract more customers and keep existing customers.

Magento offers three types of promotions.

  • Tier Pricing
  • Catalog Price rules
  • Shopping cart Price rules
Tier pricing is process where you sell your products to the public in bulks at a discount price. A common example would be selling a T-shirt for R25 then offer a promotion where the customer can buy four T-shirts for R80 meaning they have R5 discount on each T-shirt. This approach is very common in wholesalers and helps sell more stock in a short space of time
To set a Tier price on a product in your store in the Magento admin Panel,  navigate to Catalog > Manage Products.
Locate the product you want to add Tier pricing and Click edit
Find the Price tab and click on it
You will see Tier Price on the tab and edit accordingly
Catalog price rules are rules used on the price of each product before the product is added to the shopping cart.
These rules are commonly used for
  • Store wide sales
  • Customer groups
To set up a Catalog price rule in the Magento admin, navigate to Promotions > Catalog Price Rules
At the top of the right hand corner click Add New Rule
Rule Information tab
  1. Enter the Name of the rule
  2. Enter the description of the rule
  3. Status is Inactive by default – set to active when you feel every thing is ready on working
  4. Customer Group select all
  5. Add a start date for the rule
  6. Add a end date for the rule
  7. Priority is used when you have more than one rule active – This way you tell Magento which rule takes precedence over the others.
Conditions tab
This is where it gets a bit tricky
Magento offers and if condition to specify how you want your rules to be applied
If All of these conditions are True
Price  equals or greater than  30
This condition says if the price of the product is equal to 30 or greater than 30 (30 and above) then apply this rule to that product.
Actions tab
  1. Apply – Specify whether you want the rule applied to a fixed amount or percentage of the original price
  2. Discount Amount – Enter the amount of discount to be deducted to the price of the product
  3. Stop Further Rules Processing – Disables other rules applied apart from this one depending on priority (Rule Information tab number 7)
After this you can either save and Apply if you want the rule to be applicable immediately of  Save rule.
Shopping cart price rules are applied to the shopping basket once you have decided on what product you would like to purchase from the site.
If your basket is over R120 then you get 5% discount off the whole basket.
To set up a shopping cart price rule in the Magento admin, navigate to Promotions > Shopping cart price rules
At the top right corner Click Add new Rule
Rule Information tab
  1. Enter the name of the rule
  2. Enter description of the rule
  3. Status is Inactive by default – set to active when you feel every thing is ready on working
  4. Customer Group select all
  5. Coupon – coupon can be used to a basket as an extra token of appreciation to special customers
  6. Uses per coupon – The total amount of times the coupon can be used (can also be left blank)
  7. set start date
  8. set end date
  9. Priority is used when you have more than one rule active – This way you tell Magento which rule takes precedence over the others.
  10. Public RSS feed – Would you like customer to be notified about any new rules
Conditions tab
Magento offers and if condition to specify how you want your rules to be applied
If ANY of these conditions are True
Total Items Quantity  is  10
This condition says if any of these conditions (the total number of items in the basket amounts to ten) then apply this condition to the basket.
Actions tab
  1. Discount amount – Enter the amount of discount awarded to the basket
  2. Maximum Qty discount is Applied – If you would like to limit the discount to a certain number of items in the cart example 5 ,then only 5 of the items in the basket will recieve discount the rest will be charged at normal rates
  3. Apply to shipping amount – Should the amount be applied to the shipping cost
  4. Free shipping – specify the type of shipping
  5. Stop further rules from processing – Stop all other rules to be applied to the basket
  6. Apply – Apply the type of discount to be applied to the basket
  7. Apply a price rule only to cart items matching the following conditions
  • You can define a condition only if the conditions met in the Conditions tab are all true
Labels tab
  • Add label to the rule
You can Apply these three rules to your store. There are more details which are not described in this post but will be added shortly about the more detailed information about setting up promotions like calculating your tax and so forth.