Skip to content

Adding Patches

In certain cases in can be beneficial for us to apply patches to the core code (code in vendor).

Creating a Patch

We can use meld to help create the patch file.

Create a blank diff copying the original code in the left hand pane and the changed in the right.

Click file > format as patch to see the patch content.

Creating a patch in meld

In the magento project create a blank patch file with a descriptive name i.e. 'Checkout-Address-Not-Saving-Issue.patch'

Tip

Keep all patch files in a patches directory in the project root and version them.

In this file create a header similar to below.

Index: ./vendor/magento/module-example/Path/To/BrokenFile.php
=========================================================================

Where the index is the path to the file to be patched.

Paste the patch content from meld below this and replace the <unamed> with the file path of the patch file, it should look something like the below.

Index: ./vendor/magento/module-example/Path/To/BrokenFile.php
=========================================================================
---  a/vendor/magento/module-example/Path/To/BrokenFile.php
+++  b/vendor/magento/module-example/Path/To/BrokenFile.php
@@ -2,7 +2,7 @@

 ## Creating a Patch

-Buggy Code
+Working Code

 We can use meld to help create the patch file.

Applying the Patch

We will use composer to apply the patch.

First we need to add the package:

composer require cweagans/composer-patches

Add the patch to the 'extra' section in the composer.json, similar to below.

{
"extra": {
      "composer-exit-on-patch-failure": true,
      "patches": {
          "magento/module-example": {
              "Title of issue we are fixing": "patches/Checkout-Address-Not-Saving-Issue.patch"
          }
      }
  }
}

Now run composer -v install to apply the patch with verbose output.

Read the Magento docs for more info.