Tag: magento

Custom Magento2 System Configuration

In magento 2 series we already know how to create module and admin menu. If you miss previous article, read from here. Today We discuss about magento 2 system configuration. So lets start..

How to adding a system config file
As like magento 1, system configuration are stored inside system.xml file. Location of this file is

app/code/Excercise/HelloWorld/etc/adminhtml/system.xml

Adding a new Tab
The first thing we’re going to do is a add a custom “Tab” to the System Configuration. Tabs are left navigation in admin section Stores -> Configuration. Lets create our new tab “Hello World”. Inside system.xml file code should be following.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="helloworld" translate="label" sortOrder="100">
            <label>Hello World</label>
        </tab>
    </system>
</config>

Adding A New Section
Now we create a new section of helloworld tab. Now code should be following

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="helloworld" translate="label" sortOrder="100">
            <label>Hello World</label>
        </tab>
        <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Hello World Configuration</label>
            <tab>helloworld</tab>
            <resource>Excercise_HelloWorld::test_config</resource>
        </section>
    </system>
</config>

Adding Groups and Fields

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="helloworld" translate="label" sortOrder="100">
            <label>Hello World</label>
        </tab>
        <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Hello World Configuration</label>
            <tab>helloworld</tab>
            <resource>Excercise_HelloWorld::test_config</resource>
            <group id="active_display" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Hello World Configuration Options</label>
                <field id="scope" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enable Helloworld Controller</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Adding ACL
In magento 1 acl are stored inside adminhtml. But in magento 2 acl are stored acl.xml file and located

app/code/Excercise/HelloWorld/etc/adminhtml/acl.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Adminhtml::admin">
                <resource id="Magento_Adminhtml::stores">
                    <resource id="Magento_Adminhtml::stores_settings">
                        <resource id="Magento_Adminhtml::config">
                            <resource id="Excercise_HelloWorld::test_config" title="Hello World Section" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

Don’t forget clearing magento cache. And goto Stores -> Configuration, you can see a new “Hello World Configuration” tab appear.

How to create admin menu in magento 2

My previous tutorial show you to create module in magento 2, Click here. Today I discuss about magento 2 admin menu.

Suppose module called “SR” and namesapce called “Weblog”.

In Magento 1, menu configuration are locate inside adminhtml.xml but in Magento 2 configuration is locate menu.xml file, location is

app/code/SR/Weblog/etc/adminhtml/menu.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="SR_Weblog::blogpost" title="Weblog blogpost" module="SR_Weblog" sortOrder="300" parent="Magento_Backend::content_elements" action="weblog/blog" resource="SR_Weblog::blogpost"/>
    </menu>
</config>

Done!

Clear magento cache, and go to admin panel. You can change the menu, your new menu is appended with(Content -> Weblog blogpost).

Now you create a admin controller, file location is

app/code/SR/Weblog/Controller/Adminhtml/Blog/Index.php
<?php
namespace SR\Weblog\Controller\Adminhtml\Blog;

class Index extends \Magento\Backend\App\Action
{
    /**
     * Index action
     *
     * @return void
     */
    public function execute()
    {
        die('Hello World!');
    }
}

Create Router
app/code/SR/Weblog/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="weblog" frontName="weblog">
            <module name="SR_Weblog" before="Magento_Adminhtml" />
        </route>
    </router>
</config>

Clear magento cache

Now you browse Content ->Weblog blogpost, yes it’s working.

Creating a Custom Module in Magento 2

In magento2 is fully new structure, truly fast, more structured,use jQuery as a default javascript library. Today I create a new module for magento2.

In Magento 1 we had module declaration files. These lived in

app/etc/modules/*.xml

But in magento2 there is no “app/etc/modules/” for module declaration. In magento2 a new module configuration file, located in

app/code/Packagename/Modulename/etc/module.xml

In magento2, there is no codepool any more. So you can forgot about codepool concept. lets start to create our first module called “Weblog” and namesapce called “SR”.

Step 1. Create a module configuration file, location is

app/code/SR/Weblog/etc/module.xml

 

</pre>
<pre><?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SR_Weblog" setup_version="2.0.0">
    </module>
</config>

Step 2. Create a composer.json file inside module in following location
app/code/SR/Weblog/composer.json

{
    "name": "sr/module-weblog",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-store": "100.0.0",
        "magento/module-backend": "100.0.0",
        "magento/module-media-storage": "100.0.0",
        "magento/framework": "100.0.0",
        "lib-libxml": "*"
    },
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "SR\\Weblog\\": ""
        }
    }
}

Step 3. Create registration.php following location SR/Weblog/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'SR_Weblog',
    __DIR__
);

Step 4. Register module. app/etc/config.php can be updated manually by adding the extension to the array of modules with a value of 1 indicating that it is enabled. The following shows adding the new module to the end of the file.

 modules' => array (
  'Magento_Core' => 1,
    ...    
  'Magento_Wishlist' => 1,
  'SR_Weblog' => 1
),

If we had pre-installed Magento 2 instance, then we need to run setup update:

php bin/magento setup:upgrade

Clear cache and go to admin->Sores->Configuration->advanced.Here list your new module and look like
SR_Weblog

Perfect, Our first Weblog module creation is done!

Now I create a controller for displaying something 🙂

Let’s start…

In magento 1, router config is declare inside config.xml file, but in magento2 need to new file which is routes.xml.
So create a new file routes.xml, location is

app/code/SR/Weblog/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="weblog" frontName="weblog">
            <module name="SR_Weblog" />
        </route>
    </router>
</config>

Configuration is done!

So, now create a new controller file. In magento 1 all controller are locate controllers folder, but magento2 , here no controllers folder. All controller are locate “Controller” folder.

So, Create our index controller inside “Controller” folder, location is

app/code/SR/Weblog/Controller/Index/Index.php

namespace SR\Weblog\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        die('Hello World!');
    }
}

Clear cache, and run, (url should be: http://something/weblog)

That’s it!

How to add custom block after items block into order view page in magento

In order view page, add a custom block after any type of block. Here I show you, how you can create a block after items block. You can visit my previous post overwrite sales order view. So lets start.

Suppose namespace is “Exercise” and module name is “CustomSales”

Step 1 : Create a module config file into app/etc/modules/Exercise_CustomSales.xml

<config>
    <modules>
        <Exercise_CustomSales>
            <active>true</active>
            <codePool>local</codePool>
        </Exercise_CustomSales>
    </modules>
</config>

Step 2: Create a config file under app/code/local/Exercise/CustomSales/etc/config.xml

<config>
    <modules>
        <Exercise_CustomSales>
            <version>1.7.0.2</version>
        </Exercise_CustomSales>
    </modules>
    <adminhtml>
        <layout>
            <updates>
                <customsales>
                    <file>exercise_customsales.xml</file>
                </customsales>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <blocks>
            <customsales>
                <class>Exercise_CustomSales_Block</class>
            </customsales>
        </blocks>
    </global>
</config>

Step 3: Create a block app/code/local/Exercise/CustomSales/Block/Sales/Order/View/Custom.php

class Exercise_CustomSales_Block_Sales_Order_View_Custom extends Mage_Core_Block_Template
{
    
}

Step 4: Create a layout config file app/design/adminhtml/default/default/layout/exercise_customsales.xml

<layout version="0.1.0">
    <adminhtml_sales_order_view>
        <reference name="order_items">
            <action method="setTemplate"><template>customsales/sales/order/view/items.phtml</template></action>
            <block type="customsales/sales_order_view_custom" name="customsales_sales_order_view_custom" template="customsales/sales/order/view/custom.phtml"/>
        </reference>
    </adminhtml_sales_order_view>
</layout>

Step 4: Copy sales/order/view/items.phtml into customsales/sales/order/view/items.phtml
Open this file and add one line code, look like,

<div class="grid np">
......
......
</div>
<br />
<?php echo $this->getChildHtml('customsales_sales_order_view_custom');?>

Step 5: Create a new file customsales/sales/order/view/custom.phtml, you can add your custom content. Clear cache and go to order view page.

Enjoy magento code!

Adding links to top menu in magento

In magento frontend, top menu is fact for customer need. When you add some menu without category then is a fact to add menu. Here I add a menu programmatically with top menu. Following step can add a menu.

Suppose Namespace is “Exercise” and module name is “FrontendMenu” and CodePool is “local”

Step 1: Create a module config file into app/etc/modules/Exercise_FrontendMenu.xml

<config>
    <modules>
        <Exercise_FrontendMenu>
            <active>true</active>
            <codePool>local</codePool>
        </Exercise_FrontendMenu>
    </modules>
</config>

Step 2: Create a config file under app/code/local/Exercise/FrontendMenu/etc/config.xml

<config>
    <modules>
        <Exercise_FrontendMenu>
            <version>0.1.0</version>
        </Exercise_FrontendMenu>
    </modules>
    <global>
        <blocks>
            <frontendmenu>
                <class>Exercise_FrontendMenu_Block</class>
            </frontendmenu>
        </blocks>
        <models>
            <frontendmenu>
                <class>Exercise_FrontendMenu_Model</class>
            </frontendmenu>
        </models>
    </global>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before>
                <observers>
                    <frontendmenu>
                        <class>frontendmenu/observer</class>
                        <method>addToTopmenu</method>
                    </frontendmenu>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>
    </frontend>
</config>

Step 3 : Create a observer into app/code/local/Exercise/FrontendMenu/Model/Observer.php

class Exercise_FrontendMenu_Model_Observer
{
    public function addToTopmenu(Varien_Event_Observer $observer)
    {
        $menu = $observer->getMenu();
        $tree = $menu->getTree();
        // create a menu tree node
        $node = new Varien_Data_Tree_Node(array(
            'name'   => 'Products',
            'id'     => 'products',
            'url'    => Mage::getUrl('products') // Link url
        ), 'id', $tree, $menu);
        // add node to menu
        $menu->addChild($node);
    }
}

That’s it! Magento is always beautiful 🙂

Get Ready For Magento Certified Developer Exam

By 2nd February, 2014 I gave my magento certification exam and pass with score 69 of 70. I share some information about preparation exam.

For passing or getting good score in this exam, you must have very decent experience in Magento.

1. Get a study guide under resource tab http://www.magentocommerce.com/certification/

2. I went through the Fundamentals of Magento videos available on Magento website.
http://www.magentocommerce.com/training/on-demand

3. Here some excellent tutorial that great help of exam.
http://alanstorm.com/category/magento

http://blog.belvg.com/category/magento-news/developer-certification

Best of luck!

How to rewrite one page checkout success block

We suppose create a new module, name is MyModule and NameSpace is Posixtech.

Step 1: Create a xml file inside this location app/etc/modules/Posixtech_MyModule.xml.

example :

<config>
	<modules>
		<Posixtech_MyModule>
			<active>true</active>
			<codePool>local</codePool>
		</Posixtech_MyModule>
	</modules>
</config>

Step 2: Create a config file inside this location app/code/local/Posixtech/MyModule/etc/config.xml
example :

<?xml version="1.0"?>
<config>
	<modules>
		<Posixtech_MyModule>
			<version>1.0.1</version>
		</Posixtech_MyModule>
	</modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Posixtech_MyModule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
        <layout>
            <updates>
                <mymodule>
                    <file>mymodule.xml</file>
                </mymodule>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <mymodule>
                <class>Posixtech_MyModule_Block</class>
            </mymodule>
            <checkout>
                <rewrite>
                    <onepage_success>Posixtech_MyModule_Block_Onepage_Success</onepage_success>
                </rewrite>
            </checkout>
        </blocks>
    </global>
</config>

Step 3: Then we have to create this block (into our module) that should be exdends from Mage_Checkout_Block_Onepage_Success, inside the location app/code/local/Posixtech/MyModule/Block/Onepage/Success.php

example:
class Posixtech_MyModule_Block_Onepage_Success extends Mage_Checkout_Block_Onepage_Success
{
// Write your custom methods
// All parent’s methods also will work
}

Step 4: Create a new xml file for frontend and location is app/design/frontend/base/default/layout/mymodule.xml
example:

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_onepage_success translate="label">
        <label>One Page Checkout Success</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="content">
            <reference name="checkout.success">
                <action method="setTemplate"><template>mymodule/success.phtml</template></action>
            </reference>
        </reference>
    </checkout_onepage_success>
</layout>

Step 5: Create a file inside this location app/design/frontend/base/default/template/mymodule/success.phtml

You can customize phtml file content without editing base file.

That’s it!