Magento 2: How to create your own Patch File and apply it through composer

Magento releases new patches for it's framework from time to time. Here is a tutorial of how to create your own patch file in Magento.


Although I personally am not a huge fan of patches, due to various drawbacks, there are desperate times when you need to apply a patch. I would suggest to use this approach as less as possible and instead opt going the standard magento way of versioned modules that store your changes.


But, In case there are times when you are forced to create a patch, or just for self-knowledge on how it's done, you can follow below tutorial.

STEP 1:
In your magento root directory create a folder called "patches".


STEP 2:
Create a sub-directory with the name Namespace_Module for the module which you want to create a patch for. So the path will look like this: <>\patches\Namespace_Module

STEP 3:
Add your patch file in the directory you created in Step 2 of this tutorial.

HOW TO CREATE A PATCH FILE:

  • Create a backup of the original file which you want to patch.
  • Now make your desired changes in the file.
  • Open console and run the following command:



STEP 4:
Open your Project's composer.json and add the following:



The next time do you run composer install or composer update; your patches will get applied. Alternatively you can also run composer patch:apply -vvv

NOTES:
Patch files are extremely sensitive to paths and line numbers. So, if your patch is not applying correctly, then cross-check these 2 things in your patch file.

Comments

Popular posts from this blog

XDebug - PHPStorm - Mac: How to configure Xdebug with PHPStorm on Mac OS and PHP 7.3

Elastic Search - How to install and manage multiple versions of elastic search