Magento 2 Interview Questions

Add Your Magento2 Question
  • 1 What is a Plugin

    A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface.
    Extensions that wish to intercept and change the behavior of a public method can create a Plugin class.

  • 2 In which directory we write our own custom module in Magento2

     If we need to create our own module e.g Hello World then we define it in

    app/code folder. It will be app/code/VendorName/moduleName.

    e.g : app/code/Letsknowit/Hello_world.

  • 3 From where in admin panel we can enable/disable cache

    Admin->System->Tools->Cache Management

  • 4 What is the command to enable/disable a module in Magento2

    php bin/magento module:enable module_name

    php bin/magento module:disable module_name

  • 5 Which method a controller loads by default in Magento2

     execute method

    public function execute()

        {

        }

  • 6 How to enable/disable module in magento2

    To enable we need to run
         php bin/magento module:enable Namespace_modulename
      
       e.g php bin/magento module:enable Letsknowit_Custommodule
     
        to disable we need to run 
        
        php bin/magento module:disable Namespace_modulename
      
       e.g php bin/magento module:disable Letsknowit_Custommodule
  • 7 What is the command to know the status of all modules in magento2

     php bin/magento module:status

  • 8 What is the command to clear and flush magento cache

     To clean the cache 

      php bin/magento cache:clean
      To flush cache
      php bin/magento cache:flush
  • 9 How to override controller in Magento2

    To override controller we need to create a basic module structure. In our below code we have Hello module that will override Product view controller.

     

    1. Create di.xml file in Folder Letsknowit/Hello/etc
       our complete path is app/code/Letsknowit/Hello/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="MagentoCatalogControllerProductView" type="LetsknowitHelloControllerRewriteProductView" />
    </config>

    2. Now we need to create view.php controller file in LetsknowitHelloControllerRewriteProduct
        /**
         * Hello Rewrite Product View Controller
         *
         */
        namespace LetsknowitHelloControllerRewriteProduct;
     
        class View extends MagentoCatalogControllerProductView
        {
            /**
             * @return MagentoFrameworkControllerResultRedirect|MagentoFrameworkViewResultPage
             */
            public function execute()
            {
                // Do your stuff here
                return parent::execute();
            }
        }

    In the same way we can override other controllers

  • 10 What is a UI component in Magento2

    In Magento UI component is a combination of:

    1. XML declaration that specifies the component’s configuration settings and inner structure.

    2. JavaScript class inherited from one of the Magento JavaScript framework UI components base classes (such as UIElement, UIClass or UICollection).

    3. Related template(s)
     

  • 11 What is Object Manager in Magento2

    Object manager is a class that intiates the object before begining of Bootstrapping process.

  • 12 What is the command to enable developer/production mode.

    To see on which mode it is currently on :

           bin/magento deploy:mode:show

    To enable developer mode

            bin/magento deploy:mode:set developer

    To enable production mode

          bin/magento deploy:mode:set production

  • 13 What are the files needed to bootstrap a module

    Mainly there are three files for a module to bootstrap.

    registration.php

    etc/module.xml

    and composer. json

  • 14 What are Magento area types.

    An area in Magento is a logical component which can help you organizes code in order to optimize the processing of the request.
    There are 6 magento area types
    1. Magento Admin: adminhtml
    2. Storefront: frontend
    3. Basic: base
    4. Web API REST: webapi_ rest
    5. Web API SOAP: webapi_soap
    6. Cron: crontab

  • 15 What are the limitations of using plugins for customization?

    • Plugins only work on public functions (not protected or private).
    • Plugins do not work on final classes or final methods.
    • Plugins do not work on static methods.

  • 16 In which cases should plugins be avoided?

    Plugins are useful to modify the input, output, or execution of an existing method. 
    Plugins are also best to be avoided in situations where an event observer will work. Events work well when the flow of data does not have to be modified.

  • 17 How do we enable/disable maintenance mode in Magento?

    To enable/disable maintenance mode in Magento we need to run below commands respectively
    bin/magento maintenance:enable
    bin/magento maintenance:disable

  • 18 What is the difference between sales rules and catalog rules?

    Sales rules apply to products that are already added in the cart. Catalog rules apply to products before added to the cart.

  • 19 What are Extension Attributes.

    Extension attributes are new in Magento. It used to adding additional data to an existing entity, especially if that data came from another table or was calculated was difficult and error-prone.
    At this point, extension attributes exert little control over how you implement, which gives you great power. 

  • 20 How to turns on logging of database queries

    To turn on the logging of Db queries we need to run the below command

    bin/magento dev:query-log:enable

  • 21 What is difference between cache clean and cache flush

    php bin/magento cache:clean 

    Cache cleans deletes all items from enabled Magento cache types only.
     In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses.

    php bin/magento cache:flush

    Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage.

  • 22 What is difference between ObjectManager::create($className) and ObjectManager::get($className)

    ObjectManager::create($className) :  create always returns a new instance of the requested class.

    ObjectManager::get($className) : get  returns an instance which 
      may be one that has already been instantiated

  • 23 What is Proxy Design pattern in Magento 2

    In magento2 a new concept is introduced of dependency injection. There are many types of dependency injection but  magento2 uses mainly constructor injection and method injection.
    Constructor injection says if you want to use any other class object in your class, don’t instantiate it inside the class, inject the object into the constructor 
    and then use the object in the class, so when your class will be instantiated all the dependencies injected in your class constructor will also get instantiated, and it will trigger a chain reaction of object creation, this can really slow down the process, so to stop the chain reaction of object creation magento 2 uses proxy design pattern.

    It's a new feature in Magento. It is a set of classes that are automatically generated by Magento during compilation.
    Proxies are used to lazy load a class they extend. So Injecting a Proxy object is light and takes up little resources. The object is instantiated only when it’s time for the code to use one of the methods.

  • 24 How do you make your observer only be active on the frontend or backend?

    Place it in the /etc/[area]/events.xml folder.

  • 25 how to configure a scheduled job in Magento2

    To execute a specific action on a schedule, we need to set up crontab.xml under etc folder and it will not be area specific. Syntax will look like this

     

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"-xsi:noNamespaceSchemaLocation="urn:magento:modu
    le:Magento_Cron:etc/crontab.xsd">
        <group id="default">
            <job name="job_name" 
            instance="Letsknowit\ProductModifier" 
            method="execute">
              <schedule>*/10 * * * *</schedule>
            </job>
        </group>
    </config>

     

  • 26 How to declraded a plugin in Magento 2

    <config>
        <type name="{ObservedType}">
            <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false"/>
        </type>
    </config>

    Explainations

    Required options
    type name: Enter name of a class or interface that needs to be followed.
    plugin name: Plugin name that identifies a plugin. Also used to merge the configurations for the plugin.
    plugin type: Fill the name of a plugin’s class or its virtual type. 

    You can refer the following naming convention for this field: \Vendor\Module\Plugin\<ModelName>Plugin.

    Optional options
    plugin sortOrder: Set order when the plugin calls the other same methods in the process.
    plugin disabled: That allows you enable or disable a plugin quickly. As the default configuration, the chosen value is false. Use this property to disable core or third-party plugins in your di.xml file.

    e.g:
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <type name="Letsknowit\HelloModule\Controller\Index\Example">
            <plugin name="Letsknowit_HelloModule_Plugin" type="Letsknowit\HelloModule\Plugin\ExamplePlugin" sortOrder="5" disabled="false"  />
        </type>
    </config>

  • 27 How would you create an module that deletes one item if another item was deleted?

    There are three options that we can implement in the module to delete the item.
    1. Create an event observer for sales_quote_remove_item

    2. We can create  a plugin for the \Magento\Quote\Model\Quote::removeItem

    3. OR we can use the deleteById() method

    e.g:  \Magento\Quote\Api\ CartItemRepositoryInterface::deleteById() method.

  • 28 How to add a field to the shipping address

    i) To add a new field in the shipping address we need to add a new column to the sales_order_address table.

    ii) Create a  view/frontend/layout/checkout_index_index.xml file by replicating the same path that is found in

    vendor/magento/modulecheckout/view/frontend/layout/checkout_index_index.xml 

    iii) Add the children node and specify the child node.

List Your Business

Improve Your Business Visibility

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site