1

How I can customize Magento 2 core module?

Can I create new module or override core modules?

1

If want to change in any template,js,xml and html file then you will be need to override in your theme using path :

app/design/frontend/{{vendor}}/{{theme}}/Vendor_Module/

If you want to change block,model,cotroller and helper then you will be need to create your module and rewrite your logic on this module using di.xml

6
  • Thanks What is module and what is plug in Jan 31 '19 at 10:50
  • @prabhakaran7 You want to applying code and some logic before, after, or around a public method, a plugin extends or modifies that method’s behavior Jan 31 '19 at 10:56
  • thank you if i want to edit the search functionality and checkout module can i copy the vendor/magento_module/*(which module) to app/code/Changing_core/ like this and edit the code Jan 31 '19 at 10:59
  • if want to change in any template,js,xml and html and create your theme and override template file in your them. for example: app/design/frontend/{{vendor}}/{{theme}}/Magento_Checkout/templates/cart.phtml. Block and other php file and you want to create new module and rewrite Block file using di.xml Jan 31 '19 at 11:08
  • ok thank you ... can i get your email contact for future use Jan 31 '19 at 11:10
1

Yes, basically you can override Magento core modules by creating your own custom module.
Here's an example on how to create your own module:

Step 1: Create a module folder for Magento 2 Modules

Module name is HelloMagento and Vendor name is CustomVendor
create a new folder: app/code/CustomVendor/HelloMagento

Step 2: Declare the module by module.xml Create a configuration in module etc directory.
Magento 2 will use it to recognize the module’s name and module’s version

app/code/CustomVendor/HelloMagento/etc/module.xml
Add this content to declare module name HelloMagento and version 1.0.0

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomVendor_HelloMagento" setup_version="1.0.0" />
</config>

Step 3: Register the module by registration.php
This file will be created in magento root folder: app/code/CustomVendor/HelloMagento/registration.php
Add this content to register the module:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'CustomVendor_HelloMagento',
    __DIR__
);

Step 4: Install Setup, Enable the module
After create all files of above steps.
You can install the module through command line.
Open your terminal and use these commands:

cd [magento_directory]
php bin/magento setup:upgrade

Step 5: Create a route for the module
url Format: http://.com/// Example: http://<magento_url>/customer/account/create

Create a routers.xml file: app/code/CustomVendor/HelloMagento/etc/frontend/routes.xml Add this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="yourstore" frontName="hellomagento">
            <module name="CustomVendor_HelloMagento" />
        </route>
    </router>
</config>

Step 6: Create controller and action
In the last step, we will create url for displaying in your browser:
“Hello Magento 2!. We will change the world!”.

Create an action file: app/code/CustomVendor/HelloMagento/Controller/Index/Index.php
Add the content:

<?php
namespace CustomVendor\HelloMagento\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
  public function __construct(
\Magento\Framework\App\Action\Context $context)
  {
    return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello Magento 2! We will change the world!';
    exit;
  }
}

Result: Open your browser, enter this link: http://<magento_url>/hellomagento/index/index and look at the result:
Hello Magento 2! We will change the world!

With your Magento 2 custom Module you will be able to override Magento core modules using Plugins and Observers

Reference for creating and using Plugins to override Magento Core function:
Magento2 – Create and Use Plugins

Reference for creating Module: Magento 2 Module

0

Both way you can do it.

It depends on your requirement. What actually you want.

If there are some modification then you can done in core magento.

If there are some major changes like customisation in predefined module then you can override custom module.

If modification are there you can use Plugins and if you want to handle the event then you can go with Observer.

Hope this will help you.

6
0

Choose between Create a new module or Override core modules depends on your business requirement and your time and your cost.

As Magento provide a strong platform for an e-commerce platform,so if your requirement is easy to manage from Magento exits modules, then off course go ahead of Core override using PLugin or PREFERENCE,

For override a class and a method at Magento 2 what you want to use between plugin or preference then it your class but if you want to override an method/class then off course go to With Plugin.

Magento2: what is the basic difference between plugin and preference?

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/build/di-xml-file.html

7
  • Thank you but i know how to create module basic module how i can change the core functionality can i edit code in vendor folder Jan 31 '19 at 10:46
  • NO,Both case, you have to create a custom module where you have to define plugin or preference for change the core class or core method behavior.
    – Amit Bera
    Jan 31 '19 at 10:50
  • what is preference Jan 31 '19 at 10:53
  • have to check the link provide me on off course go ahead of Core override using PLugin or PREFERENCE, text?
    – Amit Bera
    Jan 31 '19 at 10:54
  • ok thank you but have some confusion on changing core functionality Jan 31 '19 at 10:56
0

This is a very wide topic. There are quite a few interaction methods between custom (your) code and other code, e.g. Magento core. You should really read the Magento Developer Documentation to familiarise yourself with basic concepts.

There are guides for both front-end and backend developers.

First, you must understand the concept of a module. Module is a structural element of Magento 2. The whole system is built upon modules and your customisation starts with building a module. Instructions on how to create a new module.

There are many ways how your module can interact or override the Magento core (or any other module, for that matter).

  • You can substitute a core class with your own class. This is called dependency injection.
  • You can excecute your own code before and/or after a function call. This is called plugins.

  • At various points Magento fires events, and you hook your own code to be executed at an event. This is called event-observer pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.