How To Develop Magento 2 Extension To Escalate Business Success
With the advent of the World Wide Web, an assortment of opportunities for all types of business entities are created. Among myriad of software and services, e-commerce has drawn the attention of millions, perhaps because it has full potential that results in bringing business success.From different e-commerce frameworks in existence, Magento alone securely powers thousands of websites from which 14,500 e-commerce stores are ranked in the Alexa top 1 million websites.There are distinct advantages of using Magento, like
- tight control over website appearance
- rich functionalities
- -compatibility with MYSQL cluster (easily maintainable large size database)
- -utter flexibility and many more.
Ok, let us move forward to understand taxonomy of Magento and Magento 2 plugin development
In a market, already available are a variety of Magento extensions that help any business
- to refine its communications with customers
- to raise a list of qualified leads
- -increase subscribers and do many wonders.
Take for example, if your browser through websites like Binpress, Xtendify etc. you will be discovered tons of Magento marketplace extensions, like for
- magento 2 search extension
- magento 2 shipping extension
- magento 2 payment extension
- magento 2 multivendor extension
- magento 2 menu extension
- magento 2 slider extension etc. that will serve your purpose.
Based on your elaborate requirements, if no pre-given extension delivers acceptable outcomes, you can build one on your own that provides business with updated data points in real time and exactly fits your system.
As we know, in any subject matter, the devil is in details, but before I start explaining the methodology to develop Magento 2 extensions, I suppose that
- you host a website that utilizes Magento 2 framework
- you have basic literacy of at least any one programming language like C or C++
- you are not developer strictly, but well-versed with using windows operating system as an end user
Anyone, but more specifically e-commerce store owners who do not want to try standard plugins or cannot afford to purchase beyond-price extensions. Therefore, here I am describing a quick yet complete procedure or step by step guide of Magento extensions development process
First, we are going to traverse through important directories of Magento website that we need to heed out before start writing of actual code.
We shall assess contents of few folders – pre-given by Magento software one by one but make sure you don’t alter existing content given in them – as it may adversely affect your Magento web portal.
I assume you have a local website setup on your computer. If you don’t have, I request you to go through an article that teaches how to create local Magento website environment.
Go to your web server directory on shared hosting by logging into cPanel → File manager. In your existing setup, you will have a root directory and beneath it, following useful files and folders will be already present which come to our rescue to develop any complex feature with minimum efforts.
shell, skin and var
• Files: .htaccess, cron.sh,.htaccess.sample, LICENSE.html, mage,
LICENSE.txt, favicon.ico, LICENSE_AFL.txt, get.php php.ini.
sample, RELEASE_NOTES.txt, api.php, index.php, index.php.sample,
cron.php, and install.php
In the thick of directory bunch, our main business is to do with app directory, which lies at the heart of Magento extension development process.
In hierarchy shown in above picture, the folder code contains three subfolders, namely
- core and
which all three are also termed as code pools.
Create the 1st folder of your new extension
Straightway, go to the following path and create with GUI of your operating system, 2 more folders:
Registration of new module
Magento, at its core, is a collection of XML files.
To notify Magento / Magento 2.0 system that treats your newly created folder under app/code/community/ as an extension, navigate to app/etc/modules and add an XML file.
For example, app/etc/modules/maximum_orders.xml indicates that name of our new extension is maximum_orders.xml
Add multi-language feature
If you think the usage of your extension would span regions and languages, go to app/local/ and add entries in accordance with the preference of languages you want, like
Files related to Website Theme
In order to successfully build extensions that visually manifest themselves to the users on the frontend, 2 diverse folders combined play role to create user interfaces:
- app/design : it stores PHP template files & XML layout configuration files
To conclude, below five directories shall be frequently visited in our journey to create extensions:
/app/design/adminhtml/default/default/ for backend area
Before we initiate generating a real-world demo version of maximum_orders extension, I suggest you once again take a look back at above filesystem hierarchy and browse similar pages on internet that provide granular level details on role of every folder.
One of the challenges we face during the building of a new module development is a discovery of useful functionalities. That is, we have to first go through screens of any existing similar extension that is with missing features. Only after looking at the screens of already developed extensions, we can sort out our needs.
Magento extensions are great for making user-friendly sites.
From simple layouts and navigation too complicated shopping and check out features – everything is just a matter of few clicks – if you can find out or develop a right extension.
Day-by-day, online shopping is getting a mature state that can compete with a real-world storefront. The positive side is that customers can use different currencies and languages when purchasing from a shop, while vendors get required information about their customers.
I request you to try your hands on Magento directory structure before going further.
Keep in touch with MagentoGuys, a top Magento 2 extension development company to know more about e-commerce, CMS and highly sought after technology tools.
1 MINUTE BREAK
“Worldwide, a stake of digital commerce is 20% of total commerce.”
“In March 2017, the well-known Frost & Sullivan business consulting firm awarded Magento the 2017 Global E-commerce Platforms Product Line Strategy Leadership Award, for its delivery of a custom experience to shoppers.”
Suppose you have to purchase some Magento extension that fits 55% of your needs and needs further customization.
There are countless Magento extensions to choose from, free or paid.
How do you know which one brings maximum benefits to your internet store? How will you decide which one to buy and which one to try for free?
Weighing two alternative approaches
When the cost of developing a new extension significantly comes near to purchasing an existing, it is best to pursue the former option. Because it will be lifetime free for you to use. Not only that, you become an owner of newly created Magento 2 extension, hence you can sale it, upgrade it or modify extension as how you want it to function.
Ok, let us continue our tour on how to generate new Magento 2 extension on your own to enhance e-commerce website.
If you successfully completed hands-on through above stuff, it is time to use what we have learned so far. We will build a meaningful and usable extension for limiting maximum orders.
5 steps we have completed already.
We start off by defining our extension namespace file.
The filename should be unique. So for that, we will combine company name with the name of the extension itself for no-duplicate identification, possibly.
For example, app/etc/modules/MagentoGuys_MaxOrderAmount.xml
Since we want to distribute our plugin/extension free of cost to other merchants or to the public, we put it under community folder. If you desire to keep it private and sale for certain fees in future, keep it beneath different code pool directory. If you don’t know what is a codebook directory, please refer first part of this article series.
After creating above kind XML file with GUI or command line, add below code snippet in MagentoGuys_MaxOrderAmount.xml
When this much work is done, navigate to the Magento backend or admin panel.
Go to System | Configuration | Advanced | Advanced. You will
see your extension appeared on the Disable Module Output list.
Once Magento is able to see your extension, we need to create another XML file that defines settings for this extension.
It includes settings like
- Routers and many other things.
Let us create file for that purpose under app/code/community/MagentoGuys/MaxOrderAmount/etc/config.xml
Add below code lines in it with a difference of your company and extension name.
Now Build the admin user interface that displays configuration options
Since the beginning of this text, you are observing that it is all about creating XML files and adding valid XML tags. This means that with XML skills alone, you can create Magento extensions conveniently.
For the purpose of generating configurations interface that a non-coder person can access through Magento admin panel, create one more XML file:
Add in it following code content:
This is literally a big chunk of XML code, but nothing to scare of.
What is needed is to understand new XML elements. Most of above tags are self-explanatory, they are English like.
Few tags are combined or “accommodate” other tags before their closing tag.
For example, covers
This is because Magento is a multi-store e-commerce framework. Therefore, you are allowed to manage and set default options for multiple websites from single admin panel. You can instruct Magento with help of these combined tags that this particular default value should be applied to multi-store level or not.
Using below tags, you can narrow down the scope so that settings adjusted are applied to individual or single website only, as we have done in above snippet with 1
From STEP-8, we have a system.xml file ready now.
Log in to Magento and go to System | Configuration | Sales to confirm that Magento displays it.
Interestingly, we are also required to set values for configuration options of system.xml file in config.xml file. Such as values for maximum order amount, minimum order amount etc. Let us do that.
We are near to complete first-class Magento 2 extension that autonomously informs the visitor that you cannot put an order of more than certain(in our case – 15001500) amount in a single transaction.
And to finish our work, we will now have to utilize a back-end programming language now, namely PHP. We ought to create a helper file in PHP that reads both these XML files- system.xml and config.xml – at the time of rendering or loading of the website. So that Magento website becomes aware of details of this extension and reacts accordingly.
But before writing helper, we are required to create its assisted class group in config.xml file so that helper itself can be initiated when Magento website is launching or rendering.
Edit the config.xml file by adding the following to it:
Finally, we will look into how to create PHP helper file so that Magento 2 extension starts exhibiting new features.
Magento extensions development is an everytime hot topic that every business owner – whether one-roof, small or enterprise vendor – consistently likes to be updated of. Quintessentially, it saves eCommerce business from starvation, especially in recession time.
I request you to try your hands on above steps given before going further.
“Despite retailers making big mobile investments in 2016, basic functions like page load times(47%) and site/app navigation(31%) still disappoint buyers.”
Although the text of this article is not devoted to discussing Magento website price bargaining, let us briefly know about Magento website costs.
How much does it take to build a Magento website?
To give some direct answer, a very basic Magento shop that sells less than 6,000 products will cost around USD $500- $700 if you hire Indian Magento developer.
Cost break-up is another important aspect. You should discuss with a developer before finalizing the deal.
Like it includes domain name and hosting charges or is it a cost solely for customization.
What I suggest if you want to become E-commerce website owner
Since I am into a field of software and web development for more than a decade, I have some clear short-cuts. They will combine cut off 100s of dollars. Rather I would say that these shortcuts will, in fact, help to design your taste website which is paramount than saving money.
- Buy a Magento theme of your industry/sector from sites such as ThemeForest, TemplateMonster etc.
- Purchase domain name & hosting services on your own
- Upload bought theme zip file on shared hosting
- Unzip it with cPanel File manager
- Create a Google doc and list changes you want in a pre-given theme
- Share this Google doc file with the developer and ask a number of hours, per hour rate
- If you cannot find economic rate designers/developers in the local area, hire from sites like Freelancer, Upwork or PeoplePerHour
And above methodology is just perfect to fend too much outgoing money from your hand.
Ok, let us continue our tour on how to generate new Magento 2 extension on your own to enhance e-commerce website. With Do It Yourself(DIY) approach.
We are building a meaningful and usable extension for limiting maximum orders. If you successfully completed hands-on through the first and second part, it is time to go further to the ultimate publishing of Magento extension.
10 steps we have completed until now.
Now, that we have class group written in config.xml file, we can go ahead to create our helper.
Create a Data.php file as per below path.
In it, add following code content.
return parent::isModuleEnabled($moduleName );
}public function getSingleOrderTopAmount($store = null)
}public function getSingleOrderTopAmountMsg($store = null)
Helper file is NOT straightforward, at least for a non-programmer.
Don’t try to comprehend above coding.
As a layman to the software industry, simply copy and paste.
Interestingly, developers whom you order to generate a new extension also don’t have to code from scratch.
To implement any business logic, code snippets, often times get available from the internet that does perform similar functionality. All they need is to do some here and there to derive exact feature.
Here, primarily take care that values of all XML_PATH_* constants are pointing to real paths without deviation or character mistakes. Otherwise, the website will return error web pages.
For instance, these values must be same as that of config.xml and system.xml element tree.
Now that all configuration options are in place, it is time to add the event that will be triggered when specific amount item is added to shopping cart.
Add the event observer to our config.xmlfile .
Code for event observer is given below.
I fully accept that creating a new Magento extension required knowledge of XML and PHP programming languages. If you have daily 2 hours, you can spare one hour for learning to code and remaining for making an extension.
Event observer name is sales_quote_save_before
There are multiple tags under which event observer can be put. It all means the different scope of a firing of an event.
We have written even beneath config/frontend so that it affects both end users and administrator of a website also. Thus it results in restricting everyone from ordering extravagant amount order.
When a customer adds a product to the cart, Magento internally creates
an instance of the Mage_Sales_Model_Quote object. This object
further contains a collection of the Mage_Sales_Model_Quote_Item objects.
When transaction amount surpasses maximum amount, Magento will fail to raise a sales_quote_save_before event. Consequently, it will stop products getting added to cart, so it perfectly fits in our case.
The grand final task is to create the
app/code/community/MagentoGuys/MaxOrderAmount/Model/Observer.php file that will compare input value with validation amount.
}$quote = $observer->getEvent()->getQuote();if ((float)$quote->getGrandTotal() >
>getSingleOrderTopAmount(), true, false);Mage::getSingleton(‘checkout/session’)->addError(
Code within the enforceSingleOrderLimit() method is pretty self explanatory. First, Once quote object is grabbed from an event, we can compare its grand total value with the value set for the maximum amount.
We are done. Test functionality yourself.
This extension is an example of an average class Magento extension that
requires basic skills of PHP and XML.
I hope this blog has become useful to you. We got in touch with essential files of Magento. So from onwards, although how to code might be far miles away, you at least know where to code and what folders to inspect.
MagentoGuys is a leading Magento eCommerce development Company in India. Since its inception, Magento developers at the company have worked on a diverse set of projects and delivered innovative Magento solutions for various industry sectors. Here we share tips and tricks for building outstanding e-commerce and regular websites by writing quality technical blogs.