10 Essential Magento Interview Questions *
Toptal sourced essential questions that the best Magento developers and engineers can answer. Driven from our community, we encourage experts to submit questions and offer feedback.
Hire a Top Magento Developer NowIn Magento 1, how can you change the behavior and extend Magento core functionality? If there are multiple ways, explain their differences and advantages/disadvantages.
There are three ways to override core functionalities:
-
Monkey patches: Because Magento loads modules in a specific order, you can override the modules located in the
core
andcommunity
code pools by copying them into thelocal
code pool. In that case, you will rewrite the whole class. This is the least preferred method. - Rewrites: You can rewrite a function by specifying a class in your config file to rewrite an existing class. In this case, you can extend the parent class and rewrite only one function.
- Observers: Magento throws events when specific actions are done. If there is an event that is thrown before or after the action you want to interact with, you can intercept it and modify it. This is the preferred method.
What are the problems with the following code in a .phtml
file:
<?php
$products = Mage::getModel('catalog_products')
->getCollection()
->addFieldToFilter('price' ['>' => 100]);
?>
<h1>Our products less than $100:</h1>
<ul>
<?php
foreach ($products as $product) {
echo '<li>' . $product->getName() . '</li>';
}
?>
</ul>
Loading a model in a template file is bad practice. The template should be for representational logic only. Respect the MVC architecture.
The title should be translated:
<h1><?php echo $this->__('Our products less than $100') ?> :</h1>
The attribute “name” is not selected:
->addAttributeToSelect('name')
The correct model name is catalog/product
and not catalog_products
.
The correct expression for addFieldToFilter
is :
->addFieldToFilter('price', ['lt' => 100]);
Here is a corrected version:
Block class:
Class Toptal_Test_Block_Demo extends Mage_Catalog_Block_Product_Abstract {
public function getProductsLessThan($price){
return Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('price', ['lt' => $price]);
}
}
Template file:
<?php $price = 100 ?>
<h1><?php echo $this->__('Our products less than %s', Mage::helper('core')->currency($price , true, false)) ?> :</h1>
<ul>
<?php
/** @var Mage_Catalog_Model_Product $product */
foreach ($this->getProductsLessThan($price) as $product) {
echo '<li>' . $product->getName() . '</li>';
}
?>
</ul>
There are other issues related to visibility, output formatting, base currency, etc. that you should also be careful about.
When flat catalog indexing is running, the data is retrieved through EAV. Therefore performance is slowed down by both the indexing process and overhead due to EAV retrieval. The information from the products is still correct.
The most important rule in Magento is “Do not edit the core.” Therefore, you should not edit the template core files either.
In order to change the current theme CSS, the fastest way is to add your custom CSS to the <head>
of the generated HTML by using the layout update. Edit the local.xml
file located in the layout folder of your theme.
If you want to change the template files and be able to easily reuse the theme, you can create your own theme.
Developer
In this mode, all the files in pub/static/
are symlinks to the original file. Exceptions are thrown and errors are displayed in the front end. This mode makes pages load very slowly, but makes it easier to debug, as it compiles and loads static files every time. Cache can still be enabled.
Default
This default is enabled out-of-the-box. It is a state in between production and developer, as the files are generated when they are needed. I.e. CSS files are generated using several LESS files in several locations. These files will be generated only when they are needed by the front end, and will not be generated again the next time they are needed.
Production
This mode should be enabled for all Magento 2 websites in production, as all the required files are generated and placed in the pub/static
folder.
Dependency injection is a design pattern strategy that relegates the responsibility of injecting the right dependency to the calling module or framework. This is the Hollywood Principle: “Don’t call us, we’ll call you.”
The responsibility of calling the right dependency is no longer handled by the function and respects the SOLID principle.
Its main advantages are that it makes code:
- Easier to test
- Easier to re-use
- Easier to maintain
What is the best way to count the items in a collection? Explain the differences with other method(s).
The best way is to use the method getSize()
. This function will not load the collection each time to count the items but store it. So every time you need this value you will not have to recalculate it. Moreover, it uses the SQL COUNT()
function in order to speed up the counting process. However, if the collection has been modified, this value can become inconsistent.
In contrast, the count()
method will load the collection and count its items every time it is called. This can become very resource demanding.
What does EAV mean? What are the advantages and disadvantages of it, and how does Magento addresses the issues associated with it?
EAV stands for entity-attribute-value. It is the way customers, products, and address data are stored in Magento’s database. In order to retrieve information about a customer (entity), you will need to query three tables. For example, if you need to get the date of birth (attribute) of a customer (entity), you will need to retrieve the customer ID using its email address by querying the customer_entity
table, the dob
attribute ID in the eav_attribute
table, and finally use both the entity ID and attribute ID in order to retrieve the date (value) in the customer_entity_datetime
table.
While this makes it complicated to retrieve a value and requires multiple calls, it makes the system very flexible and allows the user to change the attributes, adding and removing them easily without having to modify the database schema.
In order to make data retrieval faster, Magento uses flat tables that are regenerated using indexes; it allows you to retrieve some values querying only this table.
The efficiency and usability of this model is debatable and is still a subject of great discussion between pro- and anti-EAV camps.
Factory classes are generated when code generation happens. They are created automatically for models that represent database entities.
Factory classes are used to create, get, or change entity records without using the ObjectManager
directly, as its direct usage is discouraged by Magento. (This is because it goes against the principles of dependency injection.)
These classes do not need to be manually defined, but they can be, in case you need to define a specific behavior.
Some parameters are defined by a store and some are defined by a website:
Parameter | Scope |
---|---|
Product settings | Default, Store View |
Product prices | Default, Website |
Product tax class | Default, Website |
Base currency | Default, Website |
Display currency | Default, Store view |
System configuration settings | Default, Website, and Store view |
Root category configuration | Store group |
Orders | Store view |
Customers | Default, Website |
Category settings | Default, Store view |
For example, if you need to define different base currencies, you will need two different websites.
These sample questions are intended as a starting point for your interview process. If you need additional help, explore our hiring resources—or let Toptal find the best developers, designers, marketing experts, product managers, project managers, and finance experts for you.
Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.
Looking for Magento Developers?
Looking for Magento Developers? Check out Toptal’s Magento developers.
Dejan Beljic
Dejan is a software engineer with 16+ years of experience. Since 2011, he has worked as a consultant, senior back-end developer, and support engineer on various Magento 1 and Magento 2 projects. He has also led or worked in many international development teams. Dejan is eager to acquire new knowledge and he strongly believes that broadening knowledge is the key to success.
Show MoreAleksandar Markovic
Aleksandar is a team lead and senior front-end developer with 12+ years of experience using PHP, JavaScript, and Magento 2. He is a driven professional with a broad technical skillset and extreme attention to detail, able to multitask and juggle multiple pressing projects simultaneously, as his freelance experience confirms. Aleksandar is always on top of the latest trends and technologies, knowing how to improvise, troubleshoot, take ownership, and learn new skills on the job.
Show MoreDhara Bhatti
Dhara is a back-end developer specializing in Magento 2. With nine years of extensive experience in web development, she developed a wide range of Magento 2 websites and served over 20 clients. Her comprehensive professional background also includes projects in PHP, Yii 1, Yii 2, CakePHP, WordPress, Node.js, and AngularJS. Dhara is always ready to help developers and clients solve their technical issues and aims to work on projects where she can leverage and enhance her knowledge and skills.
Show MoreToptal Connects the Top 3% of Freelance Talent All Over The World.
Join the Toptal community.