Menu
Top 7 Mistakes that Magento Development Should Avoid

Magento is an open-source e-commerce platform written in PHP. The software was originally developed by Varien Inc., a US private company headquartered in Culver City, California, with assistance from volunteers.

A simple mistake can lead to performance bottlenecks and code performance issues can have an immediate critical impact on the business.

This article provides a high-level overview of the most common mistakes that developers make while building an e-commerce store and issues that can impact performance & scalability etc of the Magento PHP code.

Magento Usage Statistics

Magento Usage Statistics

What is Magento Development?

Magento is a free eCommerce development platform that helps developers to create eCommerce websites with all advanced features. In fact, it comes with the exclusive purpose of helping people develop eCommerce platforms that aren’t even developers. This advanced development platform provides users the access to track the content and operations of their online store.

Read More: Boost Your Magento Store in Holidays to Increase Productivity

Steps to Follow while Magento Development

Magento development is the most achievable eCommerce development approach in the present era. Easy steps make it even more comfortable for online traders to build their eCommerce websites. So, the steps that include in your Magento development service are:

  1. Choosing an attractive domain name
  2. Getting the Magento installation package
  3. Choosing a template
  4. Importing products
  5. Filling out the settings

7 Mistakes that Magento Development Should Avoid

1. SQL Queries Inside a Loop

Developers often load Magento models in a loop to process some of the properties of the product. Below is an example of how they iterate over the array of product IDs to load a product model and process it in the loop:

foreach ($this->getProductIds() as $productId) {
$product = Mage::getModel('catalog/product')->load($productId);
$this->processProduct($product);
}

Since Magento uses the entity attribute value (EAV) model, loading an entity requires several heavy queries to execute. As the number of executed queries is multiplied by the number of products, we get extremely inefficient and slow code. Instead of loading products in a loop, a Magento data collection can help to load a set of models in a very efficient manner. The following example filters the result set of a collection by an array of product IDs and adds all requested product fields to the result:

$collection = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('entity_id',array($this->getProductIds()))->addAttributeToSelect(array('name'));
foreach ($collection as $product) {
$this->processProduct($product);
}

Read More: 19 Methods for Securing Magento 2 eCommerce Store

2. Loading the Same Model Multiple Times

Developers sometimes do not consider that model load operation is not internally cached, and each time the load() method is called, one or more queries are run against the database. Loading the same model several times causes noticeable performance degradation.

For example:

$name = Mage::getModel('catalog/product')->load($productId)->getName();
$sku = Mage::getModel('catalog/product')->load($productId)->getSku();
$attr = Mage::getModel('catalog/product')->load($productId)->getAttr();

Each model should be loaded only once (if there is a reason to load it at all) to optimize performance:

$product = Mage::getModel('catalog/product')->load($productId);
$name = $product->getName();
$sku = $product->getSku();
$attr = $product->getAttr();

Another point to consider is that sometimes it is not even necessary to load the model because you don’t need to work with the model entity itself. The following code loads a product merely to get the product ID:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$res['id'] = $product->getId();

In this case, you can use the native product method getIdBySku() which will work much faster:

$res['id'] = Mage::getModel('catalog/product')->getIdBySku($sku);

3. Redundant Data Set Utilization

A common mistake made by inexperienced Magento developers is to retrieve the complete collection of data when only a limited number of items are required.

public function getRandomItem() {
$collection = Mage::getResourceModel('mymodule/my_collection')->setRandomOrder();
return $collection->getFirstItem();
}

Always remember to apply a limitation to every collection query when retrieving one or more items. Below is an example of a collection query:

public function getRandomItem() {
$collection = Mage::getResourceModel('mymodule/my_collection')>setRandomOrder()->setPageSize(1);
return $collection->getFirstItem();
}

Sometimes developers want to retrieve the number of items in a particular collection, without further processing of its items. It such cases most of them use $collection->count() or count($collection) constructs, which appear to be obvious and natural solutions. However, it is most definitely the wrong way, because all the items of the collection will be loaded from the database and iterated. It is much more efficient to call the $collection->getSize() method instead.

Read More: Magento 2.0 is the Way to Enhance Your eCommerce Business

4. Inefficient Memory Utilization

Using the database adapter method fetchAll() to fetch large result sets will cause a heavy demand on the system and possibly network resources. Magento developers often fetch and iterate result sets as
follows:

$rowSet = $this->_getReadAdapter()->fetchAll($select);
foreach ($rowSet as $row) {
//process row
}

On large amounts of fetched data, this code will execute for a very long time and PHP will probably run out of memory.

In the following example, each database row is fetched separately using the fetch() method to reduce resource consumption:

$query = $this->_getReadAdapter()->query($select);
while ($row = $query->fetch()) {
//process row
}

The database server will execute only one query and the database buffer will be used for retrieving records one by one.

5. Basic PHP Code Optimizations

Developers often miss the basic PHP code optimization techniques. For example inefficient use of count() function in a for a loop.

for ($i = 0; $i < count($rows); $i++) {
//some code
}

Although count() is fairly fast in regular use, this changes rapidly when it is used in a loop. If the array or collection $rows contains a lot of data, this code is slowed down significantly. Because the PHP interpreter doesn’t perform loop-invariant code motion automatically, a much better way is to move the count() call outside the loop:

$rowNum = count($rows);
for ($i = 0; $i < $rowNum; $i++) {
//some code
}

6. Modifying Magento Core Files

Generally, developers are given a short timeline to build or customize a Magento store. As a result, few developers attempt to modify Magento core files as a quick solution to customer customization requirements. This type of patchwork development will have long-term effects on the e-commerce website since it will interfere with future Magento upgrades. One should always keep Magento core files protected for future upgrades.

The best way to modify core files is to create your own module and override Model, Controller, Block, etc using Magento’s class rewrite techniques.

The easiest way to modify core files is to create a copy of the same folder structure in a local module folder as shown in the example below:

e.g.: To modify

app/code/core/Mage/Checkout/controllers/OnepageController.php

Copy the complete folder structure to

app/code/local/Mage/Checkout/controllers/OnepageController.php

1. Security Patches

Always install the latest security patches from Magento. Ignoring security patches can result in serious security threats. You can download the latest security patches from Magento’s official website.

2. Admin router

On numerous occasions, developers copy and paste standard XML from the internet and create a module that ends up receiving errors. For example, if your module doesn’t require an admin router, then you should not leave the admin router in the folder. Based on the SUPEE-6788 patch released by the Magento team, use a new method to specify admin routers in confi.xml.

Old Method – won’t work after installing security patch – SUPEE-6788

Admin

Pits_Mymodule
pits_mymodule

New Method

Pits_Mymodule_Adminhtml

7. Magento Designer Guide

Always use Magento Designer guides to style Magento Websites. Since Magento has a great theme fallback system there is really no need to touch the default theme.

Although the easiest way to make a new theme for the new Magento is to top copy the whole theme to a new folder, don’t do that.

Copy only the files you will need:from /design/frontend/default/default/ directory to /design/frontend/default/YOUR_NEW_THEME directory.

Do the same thing with /skin/frontend/default/default/. You can apply a new theme under System/Configuration/Design. If you copy the entire theme folder to create a new theme, future upgrades will be a difficult task for you as you have to upgrade entire theme files.

Read More: Build Easy Online Stores with the Help of Magento eCommerce Store Development

Wrapping Up

One thing Magento PHP developers should keep in mind: Always be attentive to performance-critical code, avoid useless computations inside loops, try to learn and understand the features of the framework, and use them correctly. If you’re dealing with small data volumes you may not care too much about code optimization. Use common sense – sometimes it is acceptable to fetch all data or process it in a loop, but always think ahead and consider how your code will perform and scale in the future.

Magento developers often copy and paste Magento codes snippets from the internet, which may be inefficient for that particular scenario resulting in performance bottlenecks and even some errors. So it is advisable to use only standard documentations and also hire Magento developers who build a customized eCommerce store.

Project Developed By Techtic

Aghora - On-Demand Delivery Application

Agohra

The concept of Agohra was born to tackle a plaguing concern – our lack of time. In this swiftly functioning world, time is something we find hard to make. In our distribution of time, we tend to lose out on some things that are crucial for our everyday lives,

View Case Study

Kishan Gediya - Head of Open Source Department Kishan Gediya

Join over 10,000 people who
love best articles, and tips.

Relevant Blog

Why Choose Shopware for your eCommerce Store?
Kishan Gediya - Head of Open Source Department

Benefits of Shopware Development for your eCommerce Store

Published on Jul 29, 2021 by Kishan Gediya

Contents What is Shopware Development? Features of Shopware Development for your eCommerce Store The Platform is Open-Source Seamless Combination of Content (CMS) & Commerce Numerous […]

Start A Project

Speed up your App Development with this MVP Template

Netflix, Uber, Airbnb, and more launched MVPs faster to test their product in the market. Build yours in 3 simple steps and become a market leader now.

Download Your MVP Template

No thanks, I don't want to speed up my app development

Download Now