SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
1
Magento Development & Custom Magento
Extension
Submitted by : Under the guidance of
KUMAR ROHIT Asst. Prof. Anirban Sarkar
13/CA/653 Head of Dept. Computer Application
Department of Computer Applications
National Institute of Technology,Durgapur
A Project Report Submitted In Partial Fulfillment of the Requirements For the Degree of
Master of Computer Applications
2
NATIONAL INSTITUTE OF TECHNOLOGY
MAHATMA GANDHI AVENUE
DURGAPUR, INDIA-713209
CERTIFICATE OF RECOMMENDATION
This is certified that the work contained in this document entitle, “Magento Development
& Custom Magento Extension”, has been carried out by Mr. Kumar Rohit under my
supervision and may be accepted in partial fulfillment of the undersign and is not submitted
elsewhere for any other degree.
_________________________ ___________________________
Asst. Prof. Anirban Sarkar Asst. Prof. Anirban Sarkar
Head of Department Assistant Professor
Computer Applications Computer Applications
National Institute of Technology National Institute of Technology
Durgapur Durgapur
3
Acknowledgement
The internship opportunity I had with Codilar Technologies Pvt. Ltd. was a great
chance for learning and professional development. Therefore, I consider myself as
a very lucky individual as I was provided with an opportunity to be a part of it. I am
also grateful for having a chance to meet so many wonderful people and
professionals who led me though this internship period.
Bearing in mind previous I am using this opportunity to express my deepest
gratitude and special thanks to the CEO Mr. Hasheed Hameed who in spite of
being extraordinarily busy with his duties, took time out to hear, guide and keep
me on the correct path and allowing me to carry out my project at their esteemed
organization and extending during the training.
I express my deepest thanks to Mr. Arshad M, Tech Head Magento Developer
for taking part in useful decision & giving necessary advices and guidance and
arranged all facilities to make life easier. I choose this moment to acknowledge his
contribution gratefully.
It is my radiant sentiment to place on record my best regards, deepest sense of
gratitude to Mr. Mahaveer Devabalan Co-Founder & COO, Mr. Tamzeed Qazi,
Lead Magento Developer for their careful and precious guidance which were
extremely valuable for my study both theoretically and practically.
I perceive as this opportunity as a big milestone in my career development. I will
strive to use gained skills and knowledge in the best possible way, and I will
continue to work on their improvement, in order to attain desired career objectives.
Hope to continue cooperation with all of you in the future,
Sincerely,
Kumar Rohit
Place: Bangalore
4
Abstract
Codilar is an Ecommerce development company strongly focusing on Magento.
Codilar started by Certified Magento developers. Our Magento Certified
Developers, solution specialists and creative designers can craft the perfect
solution for your eCommerce cravings.Our eCommerce Solution Specialists can
help you increase ROI by driving sales to your store. Our services include omni-
channel customer engagement, e-mail marketing, abandoned cart, reviews,
customer level analytics and more. Talk to our experts to get a free audit on your
SEO, Usability and Scalability performance. We can track down the bottlenecks
blocking you from boosting your conversion rates.
Specialties
eCommerce, Certified Magento Developers, Online Store, Magento
Development, Magento Custom module, Magento Theme Development,
Customer Engagement, OmniChannel Services, Magento Migration, Outsource
us
 Website
http://codilar.com
 Industry
Information Technology and Services
 Type
Partnership
5
Table of contents
1 Introducing Magento ..................................................................... 06
What is Magento?..................................................................................... 06
Magento Frontend and Backend............................................................. 06
Frontend (The Web Store Front)....................................................... 07
Backend (The Back Office)................................................................ 07
Advanced Magento Customization Options ........................................... 08
Architectural diagrams........................................................................14
2 Magento Development Features .................................................. 15
Magento plugins structure........................................................................ 15
Create your own controller....................................................................... 18
Create a block........................................................................................... 20
Model and Database ................................................................................ 21
Collection and Forms ............................................................................... 22
Create a plugin in the backend................................................................ 23
Magento Admin grid ................................................................................. 24
Re-write a magento block........................................................................ 25
Re-write a magento model....................................................................... 26
Re-write a magento controller ................................................................. 27
Events and Observers.............................................................................. 28
The Helpers............................................................................................... 29
3 Live Project : Gypsyroute ..........................................................30
Design View .............................................................................................. 30
Admin View ............................................................................................... 31
4 Magento 1.x REST ......................................................................32
Introduction REST API .............................................................................. 32
Testing REST Resources.......................................................................... 34
5 Magento Extension: Marketplace..............................................37
Overview..................................................................................................... 37
Development:The Admin Panel ............................................................... 39
Data Flow Diagram .............................................................................42
6
Introduction to Magento
What is Magento?
Magento is a feature-rich eCommerce platform built on open-source technology that provides online
merchants with unprecedented flexibility and control over the look, content and functionality of their
eCommerce store. Magento’s intuitive administration interface features powerful marketing, search engine
optimization and catalog-management tools to give merchants the power to create sites that are tailored to
their unique business needs. Designed to be completely scalable and backed by Varien's support network,
Magento offers companies the ultimate eCommerce solution.
Magento Frontend and Backend
The following shows the two visible interfaces of Magento.
Frontend (The Web Store Front)
The frontend is the public view of your web store with which your customers can interact through a
standard web browser. It presents your products, product Categories and promotions and provides your
customers with ordering, payment and purchase tracking features.
An example is shown below:
7
Introduction to Magento
Backend (The Back Office)
The backend is the password-protected view of the back office features that enable a web store
administrator to setup the products and purchasing options and to manage Orders and all other store
functionalities. A view of the backend is shown below.
8
Introduction to Magento
Advanced Magento Customization
Options
The options described in this user guide can get you up and running with an extremely feature-rich and
attractive interface without the need for technical expertise.
If you would like to take your web store one step further, Magento provides a variety of advanced options
for customizing the look-and-feel and functionality of your site. These include:
• Multiple Websites
• Multiple Web Stores
• Multiple Web Store Views
• Multiple Languages
• Custom Themes and Skins
• Extension Modules
You may refer to http://www.magentocommerce.com for a description of these options, as well as for
access to resources that can assist you with their implementation.
Filtering the Display of Products
Typically, the first step of a customer when browsing through the web store may be to select the Category or
Sub-Category of the products of interest. The customer can then filter the products in that Category according
to the various Attributes of these products.
The SHOP BY area on the left side of the frontend page provides a variety of product Attributes that can be
used to access the products in that Category. The Attributes in a Category appear in groups, each with its
own title. In the frontend page shown above, the groups that appear are PRICE (which shows ranges of
prices), COLOR, MANUFACTURER and SHOE TYPE. Next to each Attribute is the number of products
that have that Attribute.
For example: The Apparel Category has three green products, as shown below:
9
Introduction to Magento
Searching
The top right of your web store page provides a search field in which a customer can type all or part of
the name of a product, or its Attributes, in order to find it, as shown below:
Search Field
Compare Products
10
Introduction to Magento
Grid/List View
Products can be displayed in a grid (table), as shown in the previous page or as a list, as shown below:
Recently Viewed
The RECENTLY VIEWED PRODUCTS area lists the products recently viewed by the customer, whether
they were purchased or not, and enables him/her to easily link back to them.
11
Introduction to Magento
Purchasing Functionality
My Cart
To add a product to your shopping cart, click on the Add to Cart icon of that product, as shown
below:
The My Cart area shown above displays a brief list of the products in your shopping cart. The
Recently Added items(s) area shows a list of the last three items added to your shopping cart.
Checking Out
12
Introduction to Magento
The My Cart area also provides a Checkout button that takes a customer to the first step of the one-
page checkout process, as shown below:
A customer can make a one-time purchase as a guest or can register and create an account. Customers that
have created an account can later track the status of their Order.
Tip: Magento also offers multi-shipping functionality that enables customers to ship items to more than one
address at the same time. This is a very useful feature for sending gifts, for example
Tip: Customers can also register and create an account by using the My Account or Log In link on the
top right of each page.
The following shows an example of checking out as a guest by selecting the Checkout as Guest
option in the step above:
Click Continue to see the next step in which the customer can enter their billing and identification
information.
13
Introduction to Magento
Click Continue to enter the Shipping Address, to specify the Shipping Method and to determine the
Payment Method.
The last step of the checkout process enables the customer to review the entire Order:
The customer can confirm the Order and receive an Order number, or they can change their mind and edit
their cart using the Edit Your Cart link. If they proceed with the Order and the payment is processed
successfully, then the following page is displayed.
14
Introduction to Magento
Architectural diagrams
Depending upon your role and purpose for learning more about Magento, there are several different
ways to view the Magento architecture. For example, a developer who wants to create new modules
or perhaps customize an existing module will want to understand the architecture of a module itself,
and how it fits into the larger view, with the Magento framework and other components. However, a
merchant who wants to quickly build an online store front wants to view the collection of components
from a higher level, and understand the components that impact the look, feel, and user interaction
components.
15
Magento Development Features
The structure of a Magento module
In a magneto module there are 2 parties, the « code » and « templates ».
code determines the actions that the module will be able to achieve the interraction with the database etc …
while templates are just the layout of the data sent by code.
Magento, the two parties will be placed at two different locations.
your « code » will be in
/app/code/local/monNameSpace/monNomDeModule/
while the « template » of the module will be in:
/app/design/frontend/monRepertoiredeTemplates/monTemplate (for the frontend)
And
/app/design/adminhtml/monRepertoiredeTemplates/monTemplate (for the backend)
The « code » of my template will contain the following elements:
Block : Where you go to the « controller » of your block
Controllers: the controllers of your module
Models: Performers of your module
Helper: of your helpers module
etc: the configuration of your module
sql: SQL query to execute to create your module during the instalation
Reminder on blocks:
Magento, a Block is a piece of your site that walks « alone », you can insert just anywhere. In fact, a block is a
kind of « mini controller » that you can add anywhere on your site.
According to the page of your site, you can assign to different templates depending on the view you want to call
it.
folder / app / code:
In this folder you will find three folders that contain:
Community: the plugins you have recovered (on magentoconnect for example).
Core: the default magento modules (especially not EDIT)
Local: the modules you have developed
16
Magento Development Features
Start by creating your module:
1 – Create the folder /app/code/local/Pfay/Test/
This create your namespace Pfay and your module Test .
2 – Enable the module in Magento:
Add a file Pfay_all.xml /app/etc/module/
And insert the following code in it
1
2
3
4
5
6
7
8
9
<?xml version = "1.0"?>
<config>
<modules>
<Pfay_Test>
<active> true </active>
<codePool> local </codePool>
</Pfay_Test>
</modules>
</config>
This file is used to declare all your modules in the Pfay namespace, you have to put it between the tags
« modules » declaration of your plugin one after the other.
1
2
3
4
<Pfay_Test>
<active> true </active>
<codePool> local </codePool>
</Pfay_Test>
Pfay_Test allows you to declare the namespace of your plugin Test Pfay.
active: true = the plugin is active
false = the plugin is not enabled
codePool: The module will be in the local file because it is a module that we create ourselves (remember the
lesson 1).
Magento will therefore find the files in that module in the /app/code/ local/Pfay/Test/
ahead and create the controller:
go to /app/code/local/Pfay/Test/
1 – Create the file /app/code/local/Pfay/Test/controllers/
2 – Inside create a IndexController.php which will contain:
1
2
3
4
5
6
7
8
9
10
11
class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction ()
{
echo 'test index';
}
public function mamethodeAction ()
{
echo 'test mymethod';
}
}
17
Magento Development Features
Now if you go to yoursite.com/test/index
What’s happening?
Actually … you can not find the page, there’s an error because you have not already declare your controller in the
plugin.
Config.xml file to configure your plugin:
In the same way that you have declared your plugin for Magento to take into account Pfay_All.xml, the
file config.xml of your plugin will allow you to you declare the controller of your plugin.
Mangento will check Pfay_All.xml and find your file, it will go in your /app/code/local/Pfay/Test/etc/ and reads the
config.xml file to see or pick up your controller.
So create your file Test , a record etc/ with in config.xml .
config.xml contains:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0"?>
<config>
<modules>
<Pfay_Test>
<version>1.0.0</version>
</Pfay_Test>
</modules>
<frontend>
<routers>
<routeurfrontend>
<use>standard</use>
<args>
<module>Pfay_Test</module>
<frontName>test</frontName>
</args>
</routeurfrontend>
</routers>
</frontend>
</config>
Explanation:
1
2
3
4
5
<modules>
<Pfay_Test>
<version>1.0.0</version>
</Pfay_Test>
</modules>
In this section, we declare the identity of the module for magento module to verify that what you declare in your
Pfay_All.xml is the good plugin and if the version number allow you to eventually have an update for your plugin.
18
Magento Development Features
create a block
Step 1: Call the layout in the controller
Remind , your plugin is located in /app/code/local/Pfay/Test/. The plugin name is Test and it is in the
namespace Pfay
Now open your controller:
/app/code/local/Pfay/Test/controllers/IndexController.php
And modify it like this:
1
2
3
4
5
6
7
8
9
10
11
12
class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function mamethodeAction()
{
echo ‘test mamethode’;
}
}
As you can see, I added in my action index , two small lines. These lines will search what to display when you
get on this action (by going to the URL yoursite.com/test/index for example).
1
2
$this->loadLayout(); //Va chercher les elements à afficher
$this->renderLayout(); //Affiche les elements
Step 2: Declare the items to be displayed using the Layout.
To start before you can use the Layout, (and if you read the previous tutorials you begin to predict the continued
…) it must bedeclare, with a declaration Magento is going to be able to use it. To do this go into our
file config.xml (located in the directory /etc of your module).
Open it and add it in <frontend> and after <routers> :
1
2
3
4
5
6
7
<layout>
<updates>
<test>
<file>test.xml</file>
</test>
</updates>
</layout>
19
Magento Development Features
This litte xml code will allow you to declare your file test.xml as the layout of your plugin test (because it is in your
config.xml of your Test plugin)
Now declare the folder or will find the Blocks:
In <config> and bitter <frontend> now add:
1
2
3
4
5
6
7
<global>
<blocks>
<test>
<class>Pfay_Test_Block</class>
</test>
</blocks>
</global>
Important Note:
In Magento when you declare your classes, the name of your class is the path to this file, so our
controller Pfay_Test_IndexController is at the following address:
/app/code/local/Pfay/Test/IndexController.php
So you deduce that the names of your blocks will be the type Pfay_Test_IndexController_MonBlock
layout:
The layout is part of the « template », we declared it in our config.xml module as named test.xml
Now go to the directory layout of your Magento theme.
In our example, the template currently used in magento will be in the following
directory: appdesignfrontendpfaytheme .
We will therefore create a Layout test.xml in the directory appdesignfrontendpfaythemelayout
Once this file is created, edit it and copy it:
1
2
3
4
5
6
7
8
9
10
11
12
<layout version="0.1.0">
<default>
<reference name="content">
</reference>
</default>
<routeurfrontend_index_index>
<reference name="content">
<block type="test/monblock" name="afficher_monbloc"
template="test/afficher.phtml" />
</reference>
</routeurfrontend_index_index>
</layout>
20
Magento Development Features
<routeurfrontend_index_index>
...
</routeurfrontend_index_index>
To indicate which blocks a magento add to wich page, we have created the layout!
This layout define the pages, in this pages we add blocks. One page is written like this: « router name »_ »name
of the controller »_ »action name »
So if you want to access our action yoursite.com/test/index we have already defined in the config.xml that the
name of the router will be routeurfrontend , we know that the controller called in this case is the
controller IndexController.php , and that the method wich will be called is indexAction() . The page will be in the
layout represented by the tag: routeurfrontend_index_index
And we will add in this file, the line:
1 <block type="test/monblock" name="afficher_monbloc" template="test/afficher.phtml" />
Step 3: Create the block
Now, we have to create the block « monblock « ,so go to the folder you specified in config.xml to store blocks
(app/code/local/Pfay/Test/Block) and create the Monblock.php file in it.
This file will contain:
1
2
3
4
5
6
7
8
<?php
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
return ‘informations about my block !!’ ;
}
}
This file is just a class of type Block (Mage_Core_Block_Template) with a method methodblock() which returns a
sentence (String)
Step 4: Create the template
Now go to the directory appdesignfrontendpfaythemetemplate
And create a directory « test » with a file inside afficher.phtml.
This file will contain:
[Php]
echo $this->methodblock();
?>
21
Magento Development Features
Step 1: Create the table
We will say that our plugin is an address book, we want the block to display all the book addresses one after the other of the
form:
Name Surname telephone_number
Example:
John Smith 0000000000
John Smith 0000000000
John Smith 0000000000
John Smith 0000000000
We will start by creating a table in the database, we will call this table pfay_test
So execute the following query:
1
2
3
4
5
6
CREATE TABLE `magento`.`pfay_test` (
`id_pfay_test` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`nom` VARCHAR( 50 ) NOT NULL ,
`prenom` VARCHAR( 50 ) NOT NULL ,
`telephone` VARCHAR( 20 ) NOT NULL
);
Step 2: Declare the model in config.xml
You know, Magento use the MVC model, it is necessary to create a model in order to interact with our database.
To create a template, we will first declare the model in the config.xml file of our plugin.
Go therefore etc/config.xml and add in <global> after the declaration <blocks> :
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
<models>
<test>
<class>Pfay_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
</test>
<test_mysql4>
<class>Pfay_Test_Model_Mysql4</class>
<entities>
<test>
<table>pfay_test</table>
</test>
</entities>
</test_mysql4>
</models>
<!-- allow the plugin to read and write -->
<resources>
<!-- connection to write -->
<test_write>
<connection>
<use>core_write</use>
</connection>
</test_write>
<!-- connection to read ->
<test_read>
<connection>
<use>core_read</use>
</connection>
</test_read>
</resources>
<!-- -/- -->
In :
22
Magento Development Features
Model, Collections, Forms …
1 Mage::getModel('test/test');
With this model you can store messages and nicknames and you can retrieve an object in your database. If I want to get for
example the « test » object with the identifier 2, you will do:
1
Mage::getModel('test/test')->load('2');
Now we want to retrieve all objects of this type that have been registered in our database. To do this we will use what is called
acollection we will get through the method getCollection from our model.
1 $macollection = Mage::getModel('test/test')->getCollection();
With this collection, we can make operation on our database such as:
1
2
3
4
5
$macollection = Mage::getModel('test/test')->getCollection();
//sorted by ID
$macollection->setOrder('id_pfay_test','asc');
//only select those withe the phone number 03 20 58 74 89
$macollection->addFilter('telephone', '0320587489');
1 – Sort data when viewing
So open Monbloc.php located in app/code/local/Pfay/Test/Block and change this line:
1
2
3
$collection = Mage::getModel('test/test')
->getCollection()
->setOrder('id_pfay_test','asc');
2 – Add the Form
Edit the Template afficher.phtml located in appdesignfrontendpfaythemetemplatetest as this:
<form action="<?php echo Mage::getUrl('test/index/save') ?>" method="post">
<fieldset>
<ul>
<li>
<label for="nom">Nom</label>
<input type="text" id="nom" name="nom" />
</li>
<li>
<label for="prenom">Prenom</label>
<input type="text" id="prenom" name="prenom" />
</li>
<li>
<label for="nom">Telephone</label>
<input type="text" id="telephone" name="telephone" />
</li>
<li>
<input type="submit" value="Save" />
</li>
</ul>
</fieldset>
</form>
<?php
echo $this->methodblock();
?>
Explanation: This is a form, it send the informations to the method save of your controller IndexController of your module Test .
The url will be found using the method getUrl.
controller IndexController module Test(/app/code/local/Pfay/test/controllers /) and add the following method:
1
2 public function saveAction()
23
Magento Development Features
Magento and administration
The controllers will be found in /app/code/local/Pfay/Test/controllers/Adminhtml/
For the backoffice part, you’ll search your templates and your layout in /app/design/adminhtml/default/Pfay/,
we start by editing the config.xml file in our plugin, go to /app/code/local/Pfay/Test/etc/.
After <frontend> and before <global> add:
<admin>
<routers>
<test>
<use>admin</use>
<args>
<module>Pfay_Test</module>
<frontName>admintest</frontName>
</args>
</test>
</routers>
</admin>
<adminhtml>
<layout>
<updates>
<test>
<file>test.xml</file>
</test>
</updates>
</layout>
<menu>
<test translate="title" module="adminhtml">
<title>My plugins</title>
<sort_order>100</sort_order>
<children>
<set_time>
<title>Adress book</title>
<action>admintest/adminhtml_index</action>
</set_time>
</children>
</test>
</menu>
</adminhtml>
Create the controller of the admin part
Create the folder appcodelocalPfayTestcontrollersAdminhtml and the file IndexController.php in it which will contain:
class Pfay_Test_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
Add the form in the admin interface
<?xml version="1.0" ?>
<layout version="0.1.0">
<test_adminhtml_index_index>
<reference name="content">
<block type="test/monblock" name="afficher_monbloc"
template="test/afficher.phtml" />
</reference>
</test_adminhtml_index_index>
</layout>
24
Magento Development Features
The Grid Container
Create a folder: appcodelocalPfayTestBlockAdminhtml and put in a file Grid.php. This is the file which (as its
name suggests) will contain your Grid. It contains:
<?php
class Pfay_Test_Block_Adminhtml_Grid extends
Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
//where is the controller
$this->_controller = 'adminhtml_test';
$this->_blockGroup = 'test';
//text in the admin header
$this->_headerText = 'Adressbook management';
//value of the add button
$this->_addButtonLabel = 'Add a contact';
parent::__construct();
}
}
The Grid
You indicate that the controller’s grid will be in the test folder, create the folder then create a
folder:appcodelocalPfayTestBlockAdminhtmlTest and create a file Grid.php in it containing :
<?php
class Pfay_Test_Block_Adminhtml_Test_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('contactGrid');
$this->setDefaultSort('id_pfay_test');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('test/test')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('id_pfay_test',
array(
'header' => 'ID',
'align' =>'right',
'width' => '50px',
'index' => 'id_pfay_test',
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
25
Magento Development Features
The Form Container
So create a file edit.php in the same directory:appcodelocalPfayTestBlockAdminhtmlTest
This file edit.php will contain the following code:
<?php
class Pfay_Test_Block_Adminhtml_Test_Edit extends
Mage_Adminhtml_Block_Widget_Form_Container{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
//vwe assign the same blockGroup as the Grid Container
$this->_blockGroup = 'test';
//and the same controller
$this->_controller = 'adminhtml_test';
//define the label for the save and delete button
$this->_updateButton('save', 'label','save reference');
$this->_updateButton('delete', 'label', 'delete reference');
}
/* Here, we're looking if we have transmitted a form object,
to update the good text in the header of the page (edit or add) */
public function getHeaderText()
{
if( Mage::registry('test_data')&&Mage::registry('test_data')->getId())
{
return 'Editer la reference '.$this->htmlEscape(
Mage::registry('test_data')->getTitle()).'<br />';
}
else
{
return 'Add a contact';
}
}
}
Create Form
Now create the folder:appcodelocalPfayTestBlockAdminhtmlTestEditin the file and form.php
class Pfay_Test_Block_Adminhtml_Test_Edit_Form extends
Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(
array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()-
>getParam('id'))
),
'method' => 'post',
)
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
Insert our block in Layout
Then in the layout.xml your theme of directors, change your block by:
<block type="test/adminhtml_grid" name="test" />
26
Magento Development Features
Create your « modified » Block
create in /app/code/local/Pfay/MyProduct/Block/Product/file view.php.
Class Pfay_MonProduit_Block_Product_View extends Mage_Catalog_Block_Product_View
{
public function afficherLesInfos()
{
return 'les infos complementaires';
}
}
Edit the config.xml of your module MyProduct and put in it:
<?xml version="1.0"?>
<config>
<modules>
<Pfay_MonProduit>
<version>1.0.0</version>
</Pfay_MonProduit>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_view>
Pfay_MonProduit_Block_Catalog_Product_View
</product_view>
</rewrite>
</catalog>
</blocks>
</global>
</config>
With these tags, we say we will configure a block of Magento’s core called « catalog » and we will rewrite () the
« product_view » block of this module.
Rewrite a model
class Pfay_MonProduit_Model_Customer extends Mage_Customer_Model_Customer
{
public function getName()
{
$name = '';
if ($this->getPrefix()) {
$name .= $this->getPrefix() . ' ';
}
$name .= $this->getFirstname();
if ($this->getMiddlename()) {
$name .= ' ' . $this->getMiddlename();
}
$name .= ' ' . $this->getLastname();
if ($this->getSuffix()) {
$name .= ' ' . $this->getSuffix();
}
return $name.'22222222';
}
}
27
Magento Development Features
Then the same principle, edit config.xml and add
<models>
<customer>
<rewrite>
<product> Pfay_MonProduit_Model_Customer</product>
</rewrite>
</customer>
</models>
Now, if you use getName() method on an Customer object, you will have the string « 22222222 » after the name.
Rewrite Controller Magento
require_once "Mage/Checkout/controllers/CartController.php";
class Pfay_Checkout_CartController extends Mage_Checkout_CartController
{
# Rewrite of indexAction
public function indexAction() {
die('your method has been rewrited !!');
}
}
Then your class is taken into account instead of the « classic » CartController of magento, set in the config.xml of
your module:
<?xml version="1.0" encoding="UTF-8"?>
<config>
...
<frontend>
<routers>
<checkout>
<args>
<modules>
<Pfay_Checkout before="Mage_Checkout">Pfay_Checkout</Pfay_Checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Yes just it and it works! In fact he said « just take the controller Pfay_Checkout in my module before
Mage_Checkout like that it loads my class, then this class extends the previous require_once class … and now it
works.
28
Magento Development Features
Events and Observers in magento
Event:
We can consider an event as a kind of flag that rises when a specific situation happens for example when the
user presses the « pay » button of your website, it is an event. Your order has been registered? Here it is an
event too. Etc …
Observer:
An Observer is also called « Listener » he will listen to your program to detect events. When there is an event
taking place, it will perform an action.
Create an Observer
An Observer in magento is in the directory Model of your plugin and extends the class Varien_Event_Observer
(class which I think is clear …), go in /app/code/local/Pfay/Test/Model/ folder and create your Observer.php
<?php
class Pfay_Test_Model_Observer extends Varien_Event_Observer
{
public function __construct()
{
}
public function saveCmsPageObserve($observer)
{
$event = $observer->getEvent();
$model = $event->getPage();
print_r($model->getData());
die('test');
}
}
?>
Ensures that the Observer is declared in Magento
Now we have created our Observer , we need to declare it in Magento, how it’s done?
<strong><events>
<cms_page_prepare_save>
<observers>
<Pfay_Exemple_Model_Observer>
<type>singleton</type>
<class>exemple/observer</class>
<method> saveCmsPageObserve</method>
</ Pfay_Exemple_Model_Observer>
</observers>
</cms_page_prepare_save>
</events>
</strong>
<strong> <cms_page_prepare_save> </strong>
<strong> <observers>
<Pfay_Exemple_Model_Observer>
<type>singleton</type>
<class>exemple/observer</class>
<method> saveCmsPageObserve</method>
</ Pfay_Exemple_Model_Observer>
</observers>
</strong>
29
Magento Development Features
What is a Helper
As the name implies a « helper » is something that is right for you ! It is an object that will contain practical
functions for you and you can call it from anywhere, you just load your helper to use it. For example:
$helper = Mage::helper('monhelper');
Note that this call is actually equivalent to:
$helper = Mage::helper('monhelper/data');
Indeed, it is the default helper « data » wich is called.
Create your own Helper
it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks>
<helpers>
<test>
<class>Pfay_Test_Helper</class>
</test>
</helpers>
Then creates the folder and the file app/code/local/Pfay/Test/Helper/data.php function wich will contains the
function bytwo($ nbr) with a number as an argument and returns that number multiplied by two. A helper is an
object that extends the core class Mage_Core_Helper_Abstract.
<?php
class Pfay_Test_Helper_Data extends Mage_Core_Helper_Abstract{
public function bytwo($nbr){
return $nbr*2;
}
}
Now in/app/code/local/Pfay/Test/Block/monblock.php we change the function by:
<?php
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
$helper = Mage::helper('test');
return '2*2 = '.$helper->bytwo(2);
}
}
Now when we go on yoursite.com/index.php/test/index/, we see that it works!
30
Gypsyroute
31
Gypsyroute
32
Magento 1.x REST
Introduction to API
Overview
What is REST API? To make it simple, REST API defines a set of functions to which the
developers can perform requests and receive responses. The interaction is performed via the
HTTP protocol. An advantage of such an approach is the wide usage of HTTP. That is why REST
API can be used practically for any programming language.
Common characteristics of Magento REST API resources are as follows: (magentohost is your
domain)
 You access the resource by sending an HTTP request to the Magento API server. The server
replies with a response that contains either the data you requested, or the status indicator, or
even both.
 All resources are located at https://magentohost/api/rest/.
 All resources may return different HTTP status codes (e.g., HTTP Status Code 200 for
success response or HTTP Status Code 400 for the bad request).
 You request a particular resource by adding a particular path to the base URL that specifies
the resource.
Overall Capabilities
Magento REST API allows managing a number of features, namely:
 Managing customers.
 Managing customer addresses.
 Managing products.
 Retrieving sales orders.
 Managing inventory.
Authentication
Magento REST API uses 3-legged OAuth 1.0a protocol to authenticate the application to access
the Magento service.
Output Formats
The REST API supports the response in two formats, which are XML and JSON.
33
Magento 1.x REST
Preparing REST API for the Third-Party Application
1. Register the third-party application (Consumer) in Magento Admin Panel.
2. The third-party application will utilize the provided consumer credentials to call Magento store
for getting the access token to access the data.
34
Magento 1.x REST
Retrieving the List of Products
1. From the Method drop-down list, select the GET option.
2. In the URL field, enter the following URL: http://magentohost/api/rest/products. You can limit
the number of products returned in the response. To set the limit to 4, enter the following
URL:http://magentohost/api/rest/products?limit=4
3. Click Send. Information about all products will be displayed in the response body. Example is
as follows:
35
Magento 1.x REST
Retrieving the list of customers
1. In the Method group of options, select the GET option.
2. In the URL field, enter the following URL: http://magentohost/api/rest/customers.
3. Click Send request. Information about all customers will be displayed in the response body.
Note that only Admin type of the user can retrieve the list of customers. Example is as follows:
36
Magento 1.x REST
Creating a customer address
1. In the Method group of options, select the POST option.
2. In the URL field, enter the following
URL: http://magentohost/api/rest/customers/:id/addresses where the ":id" value is the customer
ID in the system.
3. In the Body table, on the Raw input tab, enter the data required for customer address
creation.
4. Click Send request. If the address is created, the 200 OK HTTP status code will be returned.
Example is as follows:
37
Magento Extension:Marketplace
38
Magento Extension:Marketplace
39
Magento Extension:Marketplace
40
Magento Extension:Marketplace
41
Magento Extension:Marketplace
42
Magento Extension:Marketplace
43
Magento Extension:Marketplace

Weitere ähnliche Inhalte

Ähnlich wie ProjectDoc

Ähnlich wie ProjectDoc (20)

Reasons to hire a Magento Development Company for businesses
Reasons to hire a Magento Development Company for businessesReasons to hire a Magento Development Company for businesses
Reasons to hire a Magento Development Company for businesses
 
Why you choose Magento as your ecommerce platform?
Why you choose Magento as your ecommerce platform? Why you choose Magento as your ecommerce platform?
Why you choose Magento as your ecommerce platform?
 
Website Security Certification The Key to Keeping Your Website Safe
Website Security Certification The Key to Keeping Your Website SafeWebsite Security Certification The Key to Keeping Your Website Safe
Website Security Certification The Key to Keeping Your Website Safe
 
GROWMMERCE
GROWMMERCEGROWMMERCE
GROWMMERCE
 
A 12-point Cheat Sheet to Hire a Magento Developer
A 12-point Cheat Sheet to Hire a Magento DeveloperA 12-point Cheat Sheet to Hire a Magento Developer
A 12-point Cheat Sheet to Hire a Magento Developer
 
7 Things To Consider Before Selecting Any Magento Development Company
7 Things To Consider Before Selecting Any Magento Development Company7 Things To Consider Before Selecting Any Magento Development Company
7 Things To Consider Before Selecting Any Magento Development Company
 
Right time to raise your magento e commerce marketing game
Right time to raise your magento e commerce marketing gameRight time to raise your magento e commerce marketing game
Right time to raise your magento e commerce marketing game
 
Top tips to boost the conversion rate of a magento 2 e commerce store
Top tips to boost the conversion rate of a magento 2 e commerce storeTop tips to boost the conversion rate of a magento 2 e commerce store
Top tips to boost the conversion rate of a magento 2 e commerce store
 
E-commerce development specialist
E-commerce development specialistE-commerce development specialist
E-commerce development specialist
 
Why are merchants connecting with the Magento Connect extension?
Why are merchants connecting with the Magento Connect extension?Why are merchants connecting with the Magento Connect extension?
Why are merchants connecting with the Magento Connect extension?
 
A-Concise-Guide-to-Magento-Development-Services 1.pdf
A-Concise-Guide-to-Magento-Development-Services 1.pdfA-Concise-Guide-to-Magento-Development-Services 1.pdf
A-Concise-Guide-to-Magento-Development-Services 1.pdf
 
Magento vs big commerce a detailed comparison guide - ziffity
Magento vs big commerce  a detailed comparison guide - ziffityMagento vs big commerce  a detailed comparison guide - ziffity
Magento vs big commerce a detailed comparison guide - ziffity
 
Drive More Traffic on Your Magento 2 eCommerce Store with Best Magento 2 SEO ...
Drive More Traffic on Your Magento 2 eCommerce Store with Best Magento 2 SEO ...Drive More Traffic on Your Magento 2 eCommerce Store with Best Magento 2 SEO ...
Drive More Traffic on Your Magento 2 eCommerce Store with Best Magento 2 SEO ...
 
Know Why Magento Development Services Are The Best For Ecommerce Business.pptx
Know Why Magento Development Services Are The Best For Ecommerce Business.pptxKnow Why Magento Development Services Are The Best For Ecommerce Business.pptx
Know Why Magento Development Services Are The Best For Ecommerce Business.pptx
 
Magento Community Vs. Magneto Enterprise Edition - Which is Best?
Magento Community Vs. Magneto Enterprise Edition - Which is Best?Magento Community Vs. Magneto Enterprise Edition - Which is Best?
Magento Community Vs. Magneto Enterprise Edition - Which is Best?
 
ToolsTrade.com Project Final pres
ToolsTrade.com Project Final presToolsTrade.com Project Final pres
ToolsTrade.com Project Final pres
 
Magento b2 b portals
Magento b2 b portalsMagento b2 b portals
Magento b2 b portals
 
Autoyou Market Place version 1.3
Autoyou Market Place version 1.3Autoyou Market Place version 1.3
Autoyou Market Place version 1.3
 
E-commerce solutions on Magento 2.0 by Matsuu
E-commerce solutions on Magento 2.0 by MatsuuE-commerce solutions on Magento 2.0 by Matsuu
E-commerce solutions on Magento 2.0 by Matsuu
 
Magento development
Magento developmentMagento development
Magento development
 

ProjectDoc

  • 1. 1 Magento Development & Custom Magento Extension Submitted by : Under the guidance of KUMAR ROHIT Asst. Prof. Anirban Sarkar 13/CA/653 Head of Dept. Computer Application Department of Computer Applications National Institute of Technology,Durgapur A Project Report Submitted In Partial Fulfillment of the Requirements For the Degree of Master of Computer Applications
  • 2. 2 NATIONAL INSTITUTE OF TECHNOLOGY MAHATMA GANDHI AVENUE DURGAPUR, INDIA-713209 CERTIFICATE OF RECOMMENDATION This is certified that the work contained in this document entitle, “Magento Development & Custom Magento Extension”, has been carried out by Mr. Kumar Rohit under my supervision and may be accepted in partial fulfillment of the undersign and is not submitted elsewhere for any other degree. _________________________ ___________________________ Asst. Prof. Anirban Sarkar Asst. Prof. Anirban Sarkar Head of Department Assistant Professor Computer Applications Computer Applications National Institute of Technology National Institute of Technology Durgapur Durgapur
  • 3. 3 Acknowledgement The internship opportunity I had with Codilar Technologies Pvt. Ltd. was a great chance for learning and professional development. Therefore, I consider myself as a very lucky individual as I was provided with an opportunity to be a part of it. I am also grateful for having a chance to meet so many wonderful people and professionals who led me though this internship period. Bearing in mind previous I am using this opportunity to express my deepest gratitude and special thanks to the CEO Mr. Hasheed Hameed who in spite of being extraordinarily busy with his duties, took time out to hear, guide and keep me on the correct path and allowing me to carry out my project at their esteemed organization and extending during the training. I express my deepest thanks to Mr. Arshad M, Tech Head Magento Developer for taking part in useful decision & giving necessary advices and guidance and arranged all facilities to make life easier. I choose this moment to acknowledge his contribution gratefully. It is my radiant sentiment to place on record my best regards, deepest sense of gratitude to Mr. Mahaveer Devabalan Co-Founder & COO, Mr. Tamzeed Qazi, Lead Magento Developer for their careful and precious guidance which were extremely valuable for my study both theoretically and practically. I perceive as this opportunity as a big milestone in my career development. I will strive to use gained skills and knowledge in the best possible way, and I will continue to work on their improvement, in order to attain desired career objectives. Hope to continue cooperation with all of you in the future, Sincerely, Kumar Rohit Place: Bangalore
  • 4. 4 Abstract Codilar is an Ecommerce development company strongly focusing on Magento. Codilar started by Certified Magento developers. Our Magento Certified Developers, solution specialists and creative designers can craft the perfect solution for your eCommerce cravings.Our eCommerce Solution Specialists can help you increase ROI by driving sales to your store. Our services include omni- channel customer engagement, e-mail marketing, abandoned cart, reviews, customer level analytics and more. Talk to our experts to get a free audit on your SEO, Usability and Scalability performance. We can track down the bottlenecks blocking you from boosting your conversion rates. Specialties eCommerce, Certified Magento Developers, Online Store, Magento Development, Magento Custom module, Magento Theme Development, Customer Engagement, OmniChannel Services, Magento Migration, Outsource us  Website http://codilar.com  Industry Information Technology and Services  Type Partnership
  • 5. 5 Table of contents 1 Introducing Magento ..................................................................... 06 What is Magento?..................................................................................... 06 Magento Frontend and Backend............................................................. 06 Frontend (The Web Store Front)....................................................... 07 Backend (The Back Office)................................................................ 07 Advanced Magento Customization Options ........................................... 08 Architectural diagrams........................................................................14 2 Magento Development Features .................................................. 15 Magento plugins structure........................................................................ 15 Create your own controller....................................................................... 18 Create a block........................................................................................... 20 Model and Database ................................................................................ 21 Collection and Forms ............................................................................... 22 Create a plugin in the backend................................................................ 23 Magento Admin grid ................................................................................. 24 Re-write a magento block........................................................................ 25 Re-write a magento model....................................................................... 26 Re-write a magento controller ................................................................. 27 Events and Observers.............................................................................. 28 The Helpers............................................................................................... 29 3 Live Project : Gypsyroute ..........................................................30 Design View .............................................................................................. 30 Admin View ............................................................................................... 31 4 Magento 1.x REST ......................................................................32 Introduction REST API .............................................................................. 32 Testing REST Resources.......................................................................... 34 5 Magento Extension: Marketplace..............................................37 Overview..................................................................................................... 37 Development:The Admin Panel ............................................................... 39 Data Flow Diagram .............................................................................42
  • 6. 6 Introduction to Magento What is Magento? Magento is a feature-rich eCommerce platform built on open-source technology that provides online merchants with unprecedented flexibility and control over the look, content and functionality of their eCommerce store. Magento’s intuitive administration interface features powerful marketing, search engine optimization and catalog-management tools to give merchants the power to create sites that are tailored to their unique business needs. Designed to be completely scalable and backed by Varien's support network, Magento offers companies the ultimate eCommerce solution. Magento Frontend and Backend The following shows the two visible interfaces of Magento. Frontend (The Web Store Front) The frontend is the public view of your web store with which your customers can interact through a standard web browser. It presents your products, product Categories and promotions and provides your customers with ordering, payment and purchase tracking features. An example is shown below:
  • 7. 7 Introduction to Magento Backend (The Back Office) The backend is the password-protected view of the back office features that enable a web store administrator to setup the products and purchasing options and to manage Orders and all other store functionalities. A view of the backend is shown below.
  • 8. 8 Introduction to Magento Advanced Magento Customization Options The options described in this user guide can get you up and running with an extremely feature-rich and attractive interface without the need for technical expertise. If you would like to take your web store one step further, Magento provides a variety of advanced options for customizing the look-and-feel and functionality of your site. These include: • Multiple Websites • Multiple Web Stores • Multiple Web Store Views • Multiple Languages • Custom Themes and Skins • Extension Modules You may refer to http://www.magentocommerce.com for a description of these options, as well as for access to resources that can assist you with their implementation. Filtering the Display of Products Typically, the first step of a customer when browsing through the web store may be to select the Category or Sub-Category of the products of interest. The customer can then filter the products in that Category according to the various Attributes of these products. The SHOP BY area on the left side of the frontend page provides a variety of product Attributes that can be used to access the products in that Category. The Attributes in a Category appear in groups, each with its own title. In the frontend page shown above, the groups that appear are PRICE (which shows ranges of prices), COLOR, MANUFACTURER and SHOE TYPE. Next to each Attribute is the number of products that have that Attribute. For example: The Apparel Category has three green products, as shown below:
  • 9. 9 Introduction to Magento Searching The top right of your web store page provides a search field in which a customer can type all or part of the name of a product, or its Attributes, in order to find it, as shown below: Search Field Compare Products
  • 10. 10 Introduction to Magento Grid/List View Products can be displayed in a grid (table), as shown in the previous page or as a list, as shown below: Recently Viewed The RECENTLY VIEWED PRODUCTS area lists the products recently viewed by the customer, whether they were purchased or not, and enables him/her to easily link back to them.
  • 11. 11 Introduction to Magento Purchasing Functionality My Cart To add a product to your shopping cart, click on the Add to Cart icon of that product, as shown below: The My Cart area shown above displays a brief list of the products in your shopping cart. The Recently Added items(s) area shows a list of the last three items added to your shopping cart. Checking Out
  • 12. 12 Introduction to Magento The My Cart area also provides a Checkout button that takes a customer to the first step of the one- page checkout process, as shown below: A customer can make a one-time purchase as a guest or can register and create an account. Customers that have created an account can later track the status of their Order. Tip: Magento also offers multi-shipping functionality that enables customers to ship items to more than one address at the same time. This is a very useful feature for sending gifts, for example Tip: Customers can also register and create an account by using the My Account or Log In link on the top right of each page. The following shows an example of checking out as a guest by selecting the Checkout as Guest option in the step above: Click Continue to see the next step in which the customer can enter their billing and identification information.
  • 13. 13 Introduction to Magento Click Continue to enter the Shipping Address, to specify the Shipping Method and to determine the Payment Method. The last step of the checkout process enables the customer to review the entire Order: The customer can confirm the Order and receive an Order number, or they can change their mind and edit their cart using the Edit Your Cart link. If they proceed with the Order and the payment is processed successfully, then the following page is displayed.
  • 14. 14 Introduction to Magento Architectural diagrams Depending upon your role and purpose for learning more about Magento, there are several different ways to view the Magento architecture. For example, a developer who wants to create new modules or perhaps customize an existing module will want to understand the architecture of a module itself, and how it fits into the larger view, with the Magento framework and other components. However, a merchant who wants to quickly build an online store front wants to view the collection of components from a higher level, and understand the components that impact the look, feel, and user interaction components.
  • 15. 15 Magento Development Features The structure of a Magento module In a magneto module there are 2 parties, the « code » and « templates ». code determines the actions that the module will be able to achieve the interraction with the database etc … while templates are just the layout of the data sent by code. Magento, the two parties will be placed at two different locations. your « code » will be in /app/code/local/monNameSpace/monNomDeModule/ while the « template » of the module will be in: /app/design/frontend/monRepertoiredeTemplates/monTemplate (for the frontend) And /app/design/adminhtml/monRepertoiredeTemplates/monTemplate (for the backend) The « code » of my template will contain the following elements: Block : Where you go to the « controller » of your block Controllers: the controllers of your module Models: Performers of your module Helper: of your helpers module etc: the configuration of your module sql: SQL query to execute to create your module during the instalation Reminder on blocks: Magento, a Block is a piece of your site that walks « alone », you can insert just anywhere. In fact, a block is a kind of « mini controller » that you can add anywhere on your site. According to the page of your site, you can assign to different templates depending on the view you want to call it. folder / app / code: In this folder you will find three folders that contain: Community: the plugins you have recovered (on magentoconnect for example). Core: the default magento modules (especially not EDIT) Local: the modules you have developed
  • 16. 16 Magento Development Features Start by creating your module: 1 – Create the folder /app/code/local/Pfay/Test/ This create your namespace Pfay and your module Test . 2 – Enable the module in Magento: Add a file Pfay_all.xml /app/etc/module/ And insert the following code in it 1 2 3 4 5 6 7 8 9 <?xml version = "1.0"?> <config> <modules> <Pfay_Test> <active> true </active> <codePool> local </codePool> </Pfay_Test> </modules> </config> This file is used to declare all your modules in the Pfay namespace, you have to put it between the tags « modules » declaration of your plugin one after the other. 1 2 3 4 <Pfay_Test> <active> true </active> <codePool> local </codePool> </Pfay_Test> Pfay_Test allows you to declare the namespace of your plugin Test Pfay. active: true = the plugin is active false = the plugin is not enabled codePool: The module will be in the local file because it is a module that we create ourselves (remember the lesson 1). Magento will therefore find the files in that module in the /app/code/ local/Pfay/Test/ ahead and create the controller: go to /app/code/local/Pfay/Test/ 1 – Create the file /app/code/local/Pfay/Test/controllers/ 2 – Inside create a IndexController.php which will contain: 1 2 3 4 5 6 7 8 9 10 11 class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction () { echo 'test index'; } public function mamethodeAction () { echo 'test mymethod'; } }
  • 17. 17 Magento Development Features Now if you go to yoursite.com/test/index What’s happening? Actually … you can not find the page, there’s an error because you have not already declare your controller in the plugin. Config.xml file to configure your plugin: In the same way that you have declared your plugin for Magento to take into account Pfay_All.xml, the file config.xml of your plugin will allow you to you declare the controller of your plugin. Mangento will check Pfay_All.xml and find your file, it will go in your /app/code/local/Pfay/Test/etc/ and reads the config.xml file to see or pick up your controller. So create your file Test , a record etc/ with in config.xml . config.xml contains: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version="1.0"?> <config> <modules> <Pfay_Test> <version>1.0.0</version> </Pfay_Test> </modules> <frontend> <routers> <routeurfrontend> <use>standard</use> <args> <module>Pfay_Test</module> <frontName>test</frontName> </args> </routeurfrontend> </routers> </frontend> </config> Explanation: 1 2 3 4 5 <modules> <Pfay_Test> <version>1.0.0</version> </Pfay_Test> </modules> In this section, we declare the identity of the module for magento module to verify that what you declare in your Pfay_All.xml is the good plugin and if the version number allow you to eventually have an update for your plugin.
  • 18. 18 Magento Development Features create a block Step 1: Call the layout in the controller Remind , your plugin is located in /app/code/local/Pfay/Test/. The plugin name is Test and it is in the namespace Pfay Now open your controller: /app/code/local/Pfay/Test/controllers/IndexController.php And modify it like this: 1 2 3 4 5 6 7 8 9 10 11 12 class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this->loadLayout(); $this->renderLayout(); } public function mamethodeAction() { echo ‘test mamethode’; } } As you can see, I added in my action index , two small lines. These lines will search what to display when you get on this action (by going to the URL yoursite.com/test/index for example). 1 2 $this->loadLayout(); //Va chercher les elements à afficher $this->renderLayout(); //Affiche les elements Step 2: Declare the items to be displayed using the Layout. To start before you can use the Layout, (and if you read the previous tutorials you begin to predict the continued …) it must bedeclare, with a declaration Magento is going to be able to use it. To do this go into our file config.xml (located in the directory /etc of your module). Open it and add it in <frontend> and after <routers> : 1 2 3 4 5 6 7 <layout> <updates> <test> <file>test.xml</file> </test> </updates> </layout>
  • 19. 19 Magento Development Features This litte xml code will allow you to declare your file test.xml as the layout of your plugin test (because it is in your config.xml of your Test plugin) Now declare the folder or will find the Blocks: In <config> and bitter <frontend> now add: 1 2 3 4 5 6 7 <global> <blocks> <test> <class>Pfay_Test_Block</class> </test> </blocks> </global> Important Note: In Magento when you declare your classes, the name of your class is the path to this file, so our controller Pfay_Test_IndexController is at the following address: /app/code/local/Pfay/Test/IndexController.php So you deduce that the names of your blocks will be the type Pfay_Test_IndexController_MonBlock layout: The layout is part of the « template », we declared it in our config.xml module as named test.xml Now go to the directory layout of your Magento theme. In our example, the template currently used in magento will be in the following directory: appdesignfrontendpfaytheme . We will therefore create a Layout test.xml in the directory appdesignfrontendpfaythemelayout Once this file is created, edit it and copy it: 1 2 3 4 5 6 7 8 9 10 11 12 <layout version="0.1.0"> <default> <reference name="content"> </reference> </default> <routeurfrontend_index_index> <reference name="content"> <block type="test/monblock" name="afficher_monbloc" template="test/afficher.phtml" /> </reference> </routeurfrontend_index_index> </layout>
  • 20. 20 Magento Development Features <routeurfrontend_index_index> ... </routeurfrontend_index_index> To indicate which blocks a magento add to wich page, we have created the layout! This layout define the pages, in this pages we add blocks. One page is written like this: « router name »_ »name of the controller »_ »action name » So if you want to access our action yoursite.com/test/index we have already defined in the config.xml that the name of the router will be routeurfrontend , we know that the controller called in this case is the controller IndexController.php , and that the method wich will be called is indexAction() . The page will be in the layout represented by the tag: routeurfrontend_index_index And we will add in this file, the line: 1 <block type="test/monblock" name="afficher_monbloc" template="test/afficher.phtml" /> Step 3: Create the block Now, we have to create the block « monblock « ,so go to the folder you specified in config.xml to store blocks (app/code/local/Pfay/Test/Block) and create the Monblock.php file in it. This file will contain: 1 2 3 4 5 6 7 8 <?php class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template { public function methodblock() { return ‘informations about my block !!’ ; } } This file is just a class of type Block (Mage_Core_Block_Template) with a method methodblock() which returns a sentence (String) Step 4: Create the template Now go to the directory appdesignfrontendpfaythemetemplate And create a directory « test » with a file inside afficher.phtml. This file will contain: [Php] echo $this->methodblock(); ?>
  • 21. 21 Magento Development Features Step 1: Create the table We will say that our plugin is an address book, we want the block to display all the book addresses one after the other of the form: Name Surname telephone_number Example: John Smith 0000000000 John Smith 0000000000 John Smith 0000000000 John Smith 0000000000 We will start by creating a table in the database, we will call this table pfay_test So execute the following query: 1 2 3 4 5 6 CREATE TABLE `magento`.`pfay_test` ( `id_pfay_test` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `nom` VARCHAR( 50 ) NOT NULL , `prenom` VARCHAR( 50 ) NOT NULL , `telephone` VARCHAR( 20 ) NOT NULL ); Step 2: Declare the model in config.xml You know, Magento use the MVC model, it is necessary to create a model in order to interact with our database. To create a template, we will first declare the model in the config.xml file of our plugin. Go therefore etc/config.xml and add in <global> after the declaration <blocks> : 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 <models> <test> <class>Pfay_Test_Model</class> <resourceModel>test_mysql4</resourceModel> </test> <test_mysql4> <class>Pfay_Test_Model_Mysql4</class> <entities> <test> <table>pfay_test</table> </test> </entities> </test_mysql4> </models> <!-- allow the plugin to read and write --> <resources> <!-- connection to write --> <test_write> <connection> <use>core_write</use> </connection> </test_write> <!-- connection to read -> <test_read> <connection> <use>core_read</use> </connection> </test_read> </resources> <!-- -/- --> In :
  • 22. 22 Magento Development Features Model, Collections, Forms … 1 Mage::getModel('test/test'); With this model you can store messages and nicknames and you can retrieve an object in your database. If I want to get for example the « test » object with the identifier 2, you will do: 1 Mage::getModel('test/test')->load('2'); Now we want to retrieve all objects of this type that have been registered in our database. To do this we will use what is called acollection we will get through the method getCollection from our model. 1 $macollection = Mage::getModel('test/test')->getCollection(); With this collection, we can make operation on our database such as: 1 2 3 4 5 $macollection = Mage::getModel('test/test')->getCollection(); //sorted by ID $macollection->setOrder('id_pfay_test','asc'); //only select those withe the phone number 03 20 58 74 89 $macollection->addFilter('telephone', '0320587489'); 1 – Sort data when viewing So open Monbloc.php located in app/code/local/Pfay/Test/Block and change this line: 1 2 3 $collection = Mage::getModel('test/test') ->getCollection() ->setOrder('id_pfay_test','asc'); 2 – Add the Form Edit the Template afficher.phtml located in appdesignfrontendpfaythemetemplatetest as this: <form action="<?php echo Mage::getUrl('test/index/save') ?>" method="post"> <fieldset> <ul> <li> <label for="nom">Nom</label> <input type="text" id="nom" name="nom" /> </li> <li> <label for="prenom">Prenom</label> <input type="text" id="prenom" name="prenom" /> </li> <li> <label for="nom">Telephone</label> <input type="text" id="telephone" name="telephone" /> </li> <li> <input type="submit" value="Save" /> </li> </ul> </fieldset> </form> <?php echo $this->methodblock(); ?> Explanation: This is a form, it send the informations to the method save of your controller IndexController of your module Test . The url will be found using the method getUrl. controller IndexController module Test(/app/code/local/Pfay/test/controllers /) and add the following method: 1 2 public function saveAction()
  • 23. 23 Magento Development Features Magento and administration The controllers will be found in /app/code/local/Pfay/Test/controllers/Adminhtml/ For the backoffice part, you’ll search your templates and your layout in /app/design/adminhtml/default/Pfay/, we start by editing the config.xml file in our plugin, go to /app/code/local/Pfay/Test/etc/. After <frontend> and before <global> add: <admin> <routers> <test> <use>admin</use> <args> <module>Pfay_Test</module> <frontName>admintest</frontName> </args> </test> </routers> </admin> <adminhtml> <layout> <updates> <test> <file>test.xml</file> </test> </updates> </layout> <menu> <test translate="title" module="adminhtml"> <title>My plugins</title> <sort_order>100</sort_order> <children> <set_time> <title>Adress book</title> <action>admintest/adminhtml_index</action> </set_time> </children> </test> </menu> </adminhtml> Create the controller of the admin part Create the folder appcodelocalPfayTestcontrollersAdminhtml and the file IndexController.php in it which will contain: class Pfay_Test_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout(); $this->renderLayout(); } } Add the form in the admin interface <?xml version="1.0" ?> <layout version="0.1.0"> <test_adminhtml_index_index> <reference name="content"> <block type="test/monblock" name="afficher_monbloc" template="test/afficher.phtml" /> </reference> </test_adminhtml_index_index> </layout>
  • 24. 24 Magento Development Features The Grid Container Create a folder: appcodelocalPfayTestBlockAdminhtml and put in a file Grid.php. This is the file which (as its name suggests) will contain your Grid. It contains: <?php class Pfay_Test_Block_Adminhtml_Grid extends Mage_Adminhtml_Block_Widget_Grid_Container { public function __construct() { //where is the controller $this->_controller = 'adminhtml_test'; $this->_blockGroup = 'test'; //text in the admin header $this->_headerText = 'Adressbook management'; //value of the add button $this->_addButtonLabel = 'Add a contact'; parent::__construct(); } } The Grid You indicate that the controller’s grid will be in the test folder, create the folder then create a folder:appcodelocalPfayTestBlockAdminhtmlTest and create a file Grid.php in it containing : <?php class Pfay_Test_Block_Adminhtml_Test_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('contactGrid'); $this->setDefaultSort('id_pfay_test'); $this->setDefaultDir('DESC'); $this->setSaveParametersInSession(true); } protected function _prepareCollection() { $collection = Mage::getModel('test/test')->getCollection(); $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('id_pfay_test', array( 'header' => 'ID', 'align' =>'right', 'width' => '50px', 'index' => 'id_pfay_test', )); return parent::_prepareColumns(); } public function getRowUrl($row) { return $this->getUrl('*/*/edit', array('id' => $row->getId())); } }
  • 25. 25 Magento Development Features The Form Container So create a file edit.php in the same directory:appcodelocalPfayTestBlockAdminhtmlTest This file edit.php will contain the following code: <?php class Pfay_Test_Block_Adminhtml_Test_Edit extends Mage_Adminhtml_Block_Widget_Form_Container{ public function __construct() { parent::__construct(); $this->_objectId = 'id'; //vwe assign the same blockGroup as the Grid Container $this->_blockGroup = 'test'; //and the same controller $this->_controller = 'adminhtml_test'; //define the label for the save and delete button $this->_updateButton('save', 'label','save reference'); $this->_updateButton('delete', 'label', 'delete reference'); } /* Here, we're looking if we have transmitted a form object, to update the good text in the header of the page (edit or add) */ public function getHeaderText() { if( Mage::registry('test_data')&&Mage::registry('test_data')->getId()) { return 'Editer la reference '.$this->htmlEscape( Mage::registry('test_data')->getTitle()).'<br />'; } else { return 'Add a contact'; } } } Create Form Now create the folder:appcodelocalPfayTestBlockAdminhtmlTestEditin the file and form.php class Pfay_Test_Block_Adminhtml_Test_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $form = new Varien_Data_Form( array( 'id' => 'edit_form', 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()- >getParam('id')) ), 'method' => 'post', ) ); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); } } Insert our block in Layout Then in the layout.xml your theme of directors, change your block by: <block type="test/adminhtml_grid" name="test" />
  • 26. 26 Magento Development Features Create your « modified » Block create in /app/code/local/Pfay/MyProduct/Block/Product/file view.php. Class Pfay_MonProduit_Block_Product_View extends Mage_Catalog_Block_Product_View { public function afficherLesInfos() { return 'les infos complementaires'; } } Edit the config.xml of your module MyProduct and put in it: <?xml version="1.0"?> <config> <modules> <Pfay_MonProduit> <version>1.0.0</version> </Pfay_MonProduit> </modules> <global> <blocks> <catalog> <rewrite> <product_view> Pfay_MonProduit_Block_Catalog_Product_View </product_view> </rewrite> </catalog> </blocks> </global> </config> With these tags, we say we will configure a block of Magento’s core called « catalog » and we will rewrite () the « product_view » block of this module. Rewrite a model class Pfay_MonProduit_Model_Customer extends Mage_Customer_Model_Customer { public function getName() { $name = ''; if ($this->getPrefix()) { $name .= $this->getPrefix() . ' '; } $name .= $this->getFirstname(); if ($this->getMiddlename()) { $name .= ' ' . $this->getMiddlename(); } $name .= ' ' . $this->getLastname(); if ($this->getSuffix()) { $name .= ' ' . $this->getSuffix(); } return $name.'22222222'; } }
  • 27. 27 Magento Development Features Then the same principle, edit config.xml and add <models> <customer> <rewrite> <product> Pfay_MonProduit_Model_Customer</product> </rewrite> </customer> </models> Now, if you use getName() method on an Customer object, you will have the string « 22222222 » after the name. Rewrite Controller Magento require_once "Mage/Checkout/controllers/CartController.php"; class Pfay_Checkout_CartController extends Mage_Checkout_CartController { # Rewrite of indexAction public function indexAction() { die('your method has been rewrited !!'); } } Then your class is taken into account instead of the « classic » CartController of magento, set in the config.xml of your module: <?xml version="1.0" encoding="UTF-8"?> <config> ... <frontend> <routers> <checkout> <args> <modules> <Pfay_Checkout before="Mage_Checkout">Pfay_Checkout</Pfay_Checkout> </modules> </args> </checkout> </routers> </frontend> </config> Yes just it and it works! In fact he said « just take the controller Pfay_Checkout in my module before Mage_Checkout like that it loads my class, then this class extends the previous require_once class … and now it works.
  • 28. 28 Magento Development Features Events and Observers in magento Event: We can consider an event as a kind of flag that rises when a specific situation happens for example when the user presses the « pay » button of your website, it is an event. Your order has been registered? Here it is an event too. Etc … Observer: An Observer is also called « Listener » he will listen to your program to detect events. When there is an event taking place, it will perform an action. Create an Observer An Observer in magento is in the directory Model of your plugin and extends the class Varien_Event_Observer (class which I think is clear …), go in /app/code/local/Pfay/Test/Model/ folder and create your Observer.php <?php class Pfay_Test_Model_Observer extends Varien_Event_Observer { public function __construct() { } public function saveCmsPageObserve($observer) { $event = $observer->getEvent(); $model = $event->getPage(); print_r($model->getData()); die('test'); } } ?> Ensures that the Observer is declared in Magento Now we have created our Observer , we need to declare it in Magento, how it’s done? <strong><events> <cms_page_prepare_save> <observers> <Pfay_Exemple_Model_Observer> <type>singleton</type> <class>exemple/observer</class> <method> saveCmsPageObserve</method> </ Pfay_Exemple_Model_Observer> </observers> </cms_page_prepare_save> </events> </strong> <strong> <cms_page_prepare_save> </strong> <strong> <observers> <Pfay_Exemple_Model_Observer> <type>singleton</type> <class>exemple/observer</class> <method> saveCmsPageObserve</method> </ Pfay_Exemple_Model_Observer> </observers> </strong>
  • 29. 29 Magento Development Features What is a Helper As the name implies a « helper » is something that is right for you ! It is an object that will contain practical functions for you and you can call it from anywhere, you just load your helper to use it. For example: $helper = Mage::helper('monhelper'); Note that this call is actually equivalent to: $helper = Mage::helper('monhelper/data'); Indeed, it is the default helper « data » wich is called. Create your own Helper it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks> <helpers> <test> <class>Pfay_Test_Helper</class> </test> </helpers> Then creates the folder and the file app/code/local/Pfay/Test/Helper/data.php function wich will contains the function bytwo($ nbr) with a number as an argument and returns that number multiplied by two. A helper is an object that extends the core class Mage_Core_Helper_Abstract. <?php class Pfay_Test_Helper_Data extends Mage_Core_Helper_Abstract{ public function bytwo($nbr){ return $nbr*2; } } Now in/app/code/local/Pfay/Test/Block/monblock.php we change the function by: <?php class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template { public function methodblock() { $helper = Mage::helper('test'); return '2*2 = '.$helper->bytwo(2); } } Now when we go on yoursite.com/index.php/test/index/, we see that it works!
  • 32. 32 Magento 1.x REST Introduction to API Overview What is REST API? To make it simple, REST API defines a set of functions to which the developers can perform requests and receive responses. The interaction is performed via the HTTP protocol. An advantage of such an approach is the wide usage of HTTP. That is why REST API can be used practically for any programming language. Common characteristics of Magento REST API resources are as follows: (magentohost is your domain)  You access the resource by sending an HTTP request to the Magento API server. The server replies with a response that contains either the data you requested, or the status indicator, or even both.  All resources are located at https://magentohost/api/rest/.  All resources may return different HTTP status codes (e.g., HTTP Status Code 200 for success response or HTTP Status Code 400 for the bad request).  You request a particular resource by adding a particular path to the base URL that specifies the resource. Overall Capabilities Magento REST API allows managing a number of features, namely:  Managing customers.  Managing customer addresses.  Managing products.  Retrieving sales orders.  Managing inventory. Authentication Magento REST API uses 3-legged OAuth 1.0a protocol to authenticate the application to access the Magento service. Output Formats The REST API supports the response in two formats, which are XML and JSON.
  • 33. 33 Magento 1.x REST Preparing REST API for the Third-Party Application 1. Register the third-party application (Consumer) in Magento Admin Panel. 2. The third-party application will utilize the provided consumer credentials to call Magento store for getting the access token to access the data.
  • 34. 34 Magento 1.x REST Retrieving the List of Products 1. From the Method drop-down list, select the GET option. 2. In the URL field, enter the following URL: http://magentohost/api/rest/products. You can limit the number of products returned in the response. To set the limit to 4, enter the following URL:http://magentohost/api/rest/products?limit=4 3. Click Send. Information about all products will be displayed in the response body. Example is as follows:
  • 35. 35 Magento 1.x REST Retrieving the list of customers 1. In the Method group of options, select the GET option. 2. In the URL field, enter the following URL: http://magentohost/api/rest/customers. 3. Click Send request. Information about all customers will be displayed in the response body. Note that only Admin type of the user can retrieve the list of customers. Example is as follows:
  • 36. 36 Magento 1.x REST Creating a customer address 1. In the Method group of options, select the POST option. 2. In the URL field, enter the following URL: http://magentohost/api/rest/customers/:id/addresses where the ":id" value is the customer ID in the system. 3. In the Body table, on the Raw input tab, enter the data required for customer address creation. 4. Click Send request. If the address is created, the 200 OK HTTP status code will be returned. Example is as follows: