Follow by Email

Wednesday, September 4, 2013

How to create payment methord module

Create a new module “HelloWorld” – in Magento

Create new module “HelloWorld” – in Magento


Do you want to create a new page in Magento ? or Do you want to create a new module in Magento ? If yes, Then ok, just spend 10 minutes and follow below steps.

Objectives: I will create a new page in Magento whose output will be “Hello World ! I am a Magento Guy..”.

Target: Create a new module called “HelloWorld”

Step 1: Module Declaration

Create app/etc/modules/M4U_HelloWorld.xml and write below code

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
         <modules>
                <M4U_HelloWorld>
                        <active>true</active>
                        <codePool>local</codePool>
                </M4U_HelloWorld>
         </modules>
</config>

Step 2: Module Configuration

a. Create a controller class app/code/local/M4U/HelloWorld/controllers/IndexController.php

1
2
3
4
5
6
7
8
class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
     $this->loadLayout(array('default'));
     $this->renderLayout();
    }
}

b. Create a Block class app/code/local/M4U/HelloWorld/Block/HelloWorld.php

1
2
3
4
class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
{
  // necessary methods
}

c. create configuration xml in app/code/local/M4U/HelloWorld/etc/config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0"?>
<config>
    <global>
        <modules>
                <m4u_helloworld>
                        <version>0.1.0</version>
                </m4u_helloworld>
        </modules>
    <blocks>
            <helloworld>
                <rewrite>
         <helloworld>M4U_HelloWorld_Block_HelloWorld</helloworld>
        </rewrite>
            </helloworld>
     </blocks>
 
        </global>
       <frontend>
                <routers>
                        <helloworld>
                                <use>standard</use>
                                <args>
                                      <module>M4U_HelloWorld</module>
                                      <frontName>helloworld</frontName>
                                </args>
                        </helloworld>
                </routers>
        <layout>
            <updates>
                <helloworld>
                      <file>helloworld.xml</file>
                </helloworld>
            </updates>
            </layout>
        </frontend>
</config>

Define Frontend Template :

1. Define page layout in app/design/frontend/M4U/default/layout/helloworld.xml

N.B: Use default instead of M4U as template location if you use default design packages. Means create file in app/design/frontend/default/default/layout/helloworld.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>
 
    <layout version="0.1.0">
 
        <helloworld_index_index>
            <reference name="root">
                <action method="setTemplate"><template>page/1column.phtml</template></action>
            </reference>
            <reference name="content">
                <block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
            </reference>
        </helloworld_index_index>
 
    </layout>

2. Create template file app/design/frontend/M4U/default/template/helloworld/helloworld.phtml and write down

N.B: Use default instead of M4U as template location if you use default design packages. Means create file in app/design/frontend/default/default/template/helloworld/helloworld.phtml

Hello World ! I am a Magento Guy..

Hey, new module is ready to run and hit browser with url http://127.0.0.1/projectname/index.php/helloworld/

and see result.

That’s it……..

Is it easy or not ? what you think ?

Coder, Please at first try yourself.

Ohh.., Still are you facing problem ? Lets download full module source code from left panel “BOX” section or let me know.

Note: I have upgraded HelloWorld module source code of BOX widget. So it will resolve the problem which was faced by some guys.




Tuesday, September 3, 2013

What is Mage in magento

Mage is a namespace and supper class in magento.

What are “magic methods” in Magento?

Magento uses __call(), __get(), __set(), __uns(), __has(), __isset(), __toString(), __construct(), etc. magic methods. You can find more details inside class Varien_Object
For more information about magic methods: http://php.net/manual/en/language.oop5.magic.php

How to make multi lingual store in magento

Add language pack as store view
MAGENTO only has English language pack installed by default. In this case we’re going to install Chinese (China) language pack as store view, for example.
Steps:
1.  First, you need to download the language pack (http://www.magentocommerce.com/langs/list/) and then unpack it to your MAGENTO folder. It copies two folders: one goes to app\design\frontend\default\default\locale and the other one to app\locale.
2.  Go to System > Configuration. On the left top corner in Current Configuration Scope dropdown box, you can see Default Config, and Main Store in English, French, and German store view.
3.  Now let’s go and add the Chinese store view. Under the dropdown box, there’s Manage Stores link or you can directly go to System > Manage Stores. Click the Create Store View link on top right, and type these in store view information form:
Store: Main Store
Name: Chinese
Code: chinese
Status: Enabled
Sort order: 0 (optional, 0 will put in first place in dropdown box, 1 will put second, and so on)
Here, you can also disable any store view by disabling its status.
4.  Save, go back to System > Configuration. In Current Configuration Scope dropdown box, you now see there’s Chinese store view. Click that link.
On Locale options tab on the left, uncheck the “use website” checkbox then change the locale to Chinese (China). Save.
Now you can have your website in Chinese.
You can do this step to configure other existing store views, too.
Hope it helps!

How to create magento own theme


Create a directory under (app > design > frontend) copy these files from Magento’s default package (catalog.xml, catalogsearch.xml, checkout.xml, cms.xml, customer.xml, newsletter.xml, page.xml, wishlist.xml) The page.xml file will be the default page layout .xml file used for every page in your site. Other layout .xml files will modify (or “update”) this base layout.  And create other template directory.

After that   System → Configuration Click on Design, in the Package section enter your_new_theme_name in Current package name. your theme will be activated

How Magento ORM works?

ORM stands for Object Relational Mapping. It’s a programming technique used to convert different types of data to Objects and vice versa.
In Magento, ORM is shown as Model (based on Zend Framework’s Zend_Db_Adapter), which further breaks down to two types of Models.
- First is the “simple” i.e. Regular Models which is nothing but flat table or our regular table structure.
- Second Model is EAV (Entity Attribute Value), which is quite complicated and expensive to query.
All Magento Models interacting with database are inherited from Mage_Core_Model_Abstract class, which is further inherited from Varien_Object.
Difference between two Models is, Simple Model is inherited from Mage_Core_Model_Resource_Db_Abstract class,
while EAV is inherited from Mage_Eav_Model_Entity_Abstract.
For those who don’t know what EAV is, please read my 3rd answer below.
So, to end up this question,
when you want to get some data in Magento, you call it like this:
Mage::getModel('module/model')->load(1);
where 1 is the primary key id for some Regular/Simple table, while in EAV so many tables are joined to fetch just single row of data.

How Magento's MVC architecture works

MVC stands for Model-View-Controller. Any application that separates it’s data access, business logic and user interface is called MVC. There can be two types of MVC: convention-based and configuration-based. Example, cakePHP is convention-based, i.e. you just need to follow the instructions of the core system to get your module ready in just few lines. Magento is configuration-based, i.e. you need to specify each and every thing to your module’s config file in order to get it work. Magento has controllers (for request/response routing), Block (for rendering content), Model (for business logic), Resource/Mysql4 (for database operations), etc (for module-specific configuration files), Helper (for common functions), sql (for setup scripts), layout (for connecting block with templates for each controller action) and template/.PHTML file (for Presentation i.e. View).
It work like below
1. When you enter the URL  like http://www.pcds.co.in/frontname/controller/method/param1/value1/param2/value2), this URL is intercepted by one PHP file called index.php which instantiates Magento application
2. Magento application instantiates Front Controller object
3. Further, front controller instantiates Router objects (specified in module’s config.xml, global tag)
4. Now, Router is responsible to “match” the frontname which is in our URL
5. If “match” is found, it sees controller name and method name in the URL, which is finally called.
6. Now depending on what is written in action name (method name), it is executed. If any models are called in it, the controller method will instantiate that model and call the method in it which is requested.
7. Then the controller action (method) instantiate the Layout object, which calls Block specified for this action (method) name (Each controller action name have block and template file associated with it, which can be found at app/design/frontend or adminhtml/namespace/module/layout/module.xml file, name of layout file (module.xml) can be found in config.xml of that module, in layout updates tag).
8. Template file (.phtml) now calls the corresponding block for any method request. So, if you write $this->methodName in .phtml file, it will check “methodName” in the block file which is associated in module.xml file.
9. Block contains PHP logic. It references Models for any data from DB.
10. If either Block, Template file or Controller need to get/set some data from/to database, they can call Model directly like Mage::getModel(‘modulename/modelname’).

what is Difference between Magento function getSingleton() and getModel()

The difference between Mage:getSingleton() and Mage::getModel() is that the former one does not create an object if the object for same class is already created, while the later creates new objects every time for the class when it’s called.
Mage::getSingleton() uses the “singleton design pattern” of PHP. If the object is not created, it will create it.
Mage::getSingleton() is mostly used when you want to create an object once, modify it and later fetch from it. Popular example is session, you first create a session object, and then add/remove values from session across different pages, so that it retains your values (e.g. cart values, logged in customer details, etc.) and doesn’t create new session object losing your last changes.
Mage::getModel() is used when you want to have the fresh data from the database. Example is when you want to show records from database.

Sunday, June 16, 2013

What is Helper in Magento

Magento's Helper classes contain utility methods that will allow you to perform common tasks on objects and variables. For example:
$helper = Mage::helper('catalog');
You'll notice we've left off the second part of the grouped class name. Each Module has a default Data Helper class. The following is equivalent to the above:
$helper = Mage::helper('catalog/data');
Most Helpers inherit form Mage_Core_Helper_Abstract, which gives you several useful methods by default.

$translated_output =  $helper->__('Magento is Great'); //gettext style translationsif($helper->isModuleOutputEnabled()): //is output for this module on or off?

What is Singleton in Magento

First, before we get to Magento, it's important to understand that PHP has a radically different process model than Java.  A PHP singleton (regardless of Magento's involvement) is a single instance of a class per HTTP Request.  A PHP program isn't persistent in memory the same way a Java program is, so adjust your expectations of a "singleton" accordingly.  

Next, it's important to understand that Magento is a framework built on top of PHP, using PHP, and in many cases the original Magento developers wanted to push things towards a more Java like architecture.  So, you're going to see things that look familiar, are familiar, but likely differ in some major way from what you're used to because they still need to hew to PHP's version of the universe.

Magento uses a factory pattern to instantiate Helpers, Blocks, and "Model" classes.  The string

core/session

is a class alias.  This alias is used to lookup a class name in Magento's configuration. In short, this string is converted into path expressions that search Magento's configuration files to derive a classname, based on the context (helper, block, model) it was called in. For a longer version, see my Magento's Class Instantiation Autoload article.

The concept of a "Model" is a little fuzzy in Magento.  In some cases models are used as domain, or service models.  In other cases they're used as a more traditional middleware database persistence models.  After working with the system for a few years, I think the safest way to think about Models is they're Magento's attempt to do away with direct class instantiation.

There's two ways to instantiate a model class.

Mage::getModel('groupname/classname');
Mage::getSingleton('groupname/classname');

The first form will get you a new class instance.  The second form will get you a singleton class instance.  This particular Magento abstraction allows you to create a singleton out of any Magento model class, but only if you stick to Magento's instantiation methods.  That is, if you call

Mage::getSingleton('groupname/classname');

then subsequent calls to

Mage::getSingleton('groupname/classname');

will return that singleton instance.  (This is implemented with a registry pattern). However, there's nothing stopping you from directly instantiating a new instance of the class with either

$o = Mage::getModel('groupname/classname');
$o = new Mage_Groupname_Model_Classname();

Which brings us to sessions.  PHP's request model, like HTTP, was originally designed to be stateless.  Each request comes into the system with, and only with, information from the user.  As the language (and the web) moved towards being an application platform, a system that allowed information to be persisted was introduced to replace the homegrown systems that were cropping up.  This system was called sessions.  PHP sessions work by exposing a super global $SESSIONS array to the end-user-programmer that allow information to be stored on a per web-user basis.  Sessions are implemented by setting a unique ID as a cookie on the user end, and then using that cookie as a lookup key (also standard practice for web applications)

In turn, the Magento system builds an abstraction on top of PHP's session abstraction.  In Magento, you can create a "session model" that inherits from a base session class, set data members on it, and save/load those data members just as you would with a database persistence model.  The difference is information is stored in the session instead of the database store. When you see

core/session
customer/session

these are two different session models, with each one storing different data. One belongs to the Mage_Core module, the other belongs to the Mage_Customer model.  This systems allows modules to safely set and manipulate their own session data, without accidentally stepping on another module's toes, and provide logical class methods for manipulating that data.

Hopefully that answers the questions you asked, as well as the ones you didn't.