10 Essential Magento Interview Questions *

question badge

In Magento 1, how can you change the behavior and extend Magento core functionality? If there are multiple ways, explain their differences and advantages/disadvantages.

answer badge

There are three ways to override core functionalities:

  1. Monkey patches: Because Magento loads modules in a specific order, you can override the modules located in the core and community code pools by copying them into the local code pool. In that case, you will rewrite the whole class. This is the least preferred method.
  2. 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.
  3. 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.
question badge

What is the best way to count the items in a collection? Explain the differences with other method(s).

answer badge

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.

question badge

What does EAV mean? What are the advantages and disadvantages of it, and how does Magento addresses the issues associated with it?

answer badge

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.

Find top Magento developers today. Toptal can match you with the best engineers to finish your project.

Hire Toptal’s Magento developers
question badge

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>
answer badge

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.

question badge

When catalog_product_flat_data is running, what are the consequences for the store?

answer badge

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.

question badge

In Magento 1, what should you do in order to change the CSS of your current theme?

answer badge

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.

question badge

In Magento 2, what are the different deploy modes and what are their differences?

answer badge

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.

question badge

In Magento 2, what is dependency injection and what are its advantages?

answer badge

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:

  1. Easier to test
  2. Easier to re-use
  3. Easier to maintain
question badge

In Magento 2, what is a factory class and how does it work?

answer badge

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.

question badge

What is the difference between a store and a website?

answer badge

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.

* There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.
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.
All fields are required
Thanks for submitting your question.
Our editorial staff will review it shortly. Please note that 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 experts? Check out Toptal’s Magento developers.
Ihar Finchuk
Belarus
Ihar is certified Magento developer with more than 10 years of experience in design and development of distributed systems, workflow applications using PHP, JavaScript, MySQL, Oracle, and Perl. Over the last 5 years, he's been working mainly with projects based on Magento and the Zend framework.