0

What are all the best way to edit core files in Magento 2.3. eg: magento 1.9 edit app/code/local or community instead of app/code/core.

Note: What are all files I need to focus for Future Version updates.

  • 1
    Still Looking Best Answer... – zus Dec 27 '18 at 5:49
  • Please check my answer – Sourav Jan 5 at 7:00
0
+50

For new storefront theme


  1. Create a directory for the theme under app/design/frontend/<your_vendor_name>/<your_theme_name>.

  2. Add a declaration file theme.xml and optionally create etc directory and create a file named view.xml to the theme directory.

  3. Add a composer.json file.
  4. Add registration.php.
  5. Create directories for CSS, JavaScript, images, and fonts.
  6. Configure your theme in the Admin panel.

Create a theme directory To create the directory for your theme:

  1. Go to /app/design/frontend.

  2. Create a new directory named according to your vendor name: /app/design/frontend/.

  3. Under the directory, create a directory named according to your theme.


app/design/frontend/
├── <Vendor>/
│   │   ├──...<theme>/
│   │   │   ├── ...
│   │   │   ├── ...

Create directories for static files Your theme will likely contain several types of static files:

  • Styles
  • Fonts
  • JavaScript
  • Images

Each type should be stored in a separate sub-directory of web in your theme folder:

app/design/<area>/<Vendor>/<theme>/
├── web/
│ ├── css/
│ │ ├── source/ 
│ ├── fonts/
│ ├── images/
│ ├── js/

Your theme directory structure now At this point your theme file structure looks as follows:

app/design/frontend/<Vendor>/
├── <theme>/
│   ├── etc/
│   │   ├── view.xml
│   ├── web/
│   │   ├── images
│   │   │   ├── logo.svg
│   ├── registration.php
│   ├── theme.xml
│   ├── composer.json

Recommended reading

For Block

Build directories just like above -

magento2 --- app --- code

                      |--- Sample --- HelloWorld

                                             | --- Block

                                             | --- etc

                                                   | --- module.xml

                                                  | --- di.xml
  • Create module.xml to define a Magento 2 extension:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">   

<module name="Sample_HelloWorld" setup_version="0.0.1"/>

</config>
  • Set preference in di.xml

Create di.xml to refer the overriding block class:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">  

<preference for="Magento\Theme\Block\Html\Title" type="Sample\HelloWorld\Block\HelloTitle" />

</config>
  • Define an overriding class

Define HelloTitle.php under magento2/app/code/Sample/HelloWorld/Block:

<?php

namespace Sample\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

class HelloTitle extends \Magento\Theme\Block\Html\Title

{
   public function getPageTitle()
   {
      return 'haha9999';
   }  

   protected function _toHtml()
   {
      $this->setModuleName($this->extractModuleName('Magento\Theme\Block\Html\Title'));      

      return parent::_toHtml();
   }
}

For Models

Create Directories

magento2 --- app --- code

                      |--- Sample --- HelloWorld

                                             | --- etc

                                             | --- Model

                                                  | --- Catalog

Configuration of Module

Create module.xml in app/code/Sample/HelloWorld/etc and add the following code in it:

<?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="Sample_HelloWorld" setup_version="1.0.1">
</module>
</config>

Override 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="Magento\Catalog\Model\Product" type="Sample\HelloWorld\Model\Catalog\Product" />
</config>

Override Product.php

Now create Product.php in app/code/Sample/HelloWorld/Model/Catalog and add the following code in it.

<?php

namespace Sample\HelloWorld\Model\Catalog;

class Product extends \Magento\Catalog\Model\Product

{
   public function getName()
   {
       return $this->_getData(self::NAME) . ' + Demo Text';
   }

public function getSku()
   {
       return "123-Demo";
   }
}

Launch SSH and Run Commands

php bin/magento module:enable Magenticians_Moduleproduct
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Note : We're using preference to override Above model and blocks, Where (in some cases, it always depends on customization) we can also use Plugins and Virtual classes using Dependency Injections which is Highly recommended.

1

If you want to override block, controller, model ... files, then you have to create module. If you want to change only template file then create theme and put your file in theme.

How to create custom module https://www.magestore.com/magento-2-tutorial/magento-2-modules/

How to override block, controller, model https://webkul.com/blog/overriding-rewriting-classes-magento2/

How to create custom theme https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/theme-create.html

I hope this will help you.

1

One of the most common tasks for a Magento 2 developer is overriding a template.

In Magento 2, there are two main methods for overriding a template:

Theme file path

Layout Block Argument

The template path method is used when building a theme and the layout method is used when building a module. They are the simplest, most straightforward, and easiest to understand methods. You will almost always be using one of these two methods. For the very few times, these two methods are not available, you have two other methods to choose from:

Class preference

Plugin

1) Theme File Path

In Magento 2, themes can override any module’s or parent theme’s layout, template, or web (css, js, etc.) file simply by placing it in <theme_dir>/<Vendor>_<Module>/path/to/file For example, If you want to override the template located at <theme_dir>/<Vendor>_<Module>/view/html/header.phtml for the Magento_Theme module, you would place your template in <theme_dir>/Magento_Theme/templates/html/header.phtml.

There are several block definitions in Magento 2 that do not have the Vendor_Module prefix to specify which module the template belongs to. In these cases, the block’s class attribute will define what module the template belongs to. For example if you were to find a block definition in the Magento_Checkout module like this you would place your template inside the Magento_Checkout module directory inside your theme.

This Magento 2 devdoc on theme-inheritance goes into more detail on how to override a template in a theme.

2) Layout Block Argument

Module Creator

The layout method should be used when building a module. To override a template using layout XML, you only need to override the block’s template argument. Using the template Magento_Wishlist/view/frontend/templates/view.phtml as an example, to override view.phtml with your own template, you first have to create a new layout file. <Vendor>_<Module>/view/frontend/layout/wishlist_index_index.xml

wishlist_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer.wishlist">
            <arguments>
                <argument name="template" xsi:type="string">Vendor_Module::view.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Now you have to place your new custom template in the location you specified in your layout file. For this example, that is <Vendor>_<Module>/view/frontend/templates/view.phtml

-1

Basically, you can make changes in Magento 2 core by using any of the following methods

  • Preferences
  • Events and Observers
  • Plugins

Check out this video tutorial for more details https://www.youtube.com/watch?v=ko75VJVor9I&t=2s

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.