How to apply a patch to avoid overwriting modified files when updating the module?

How to apply a patch to avoid overwriting modified files when updating the module?

Magento allows adding patches to Composer in order to prevent modifications from being overwritten while updating the extension. To apply the patch using Composer, follow the guidelines below.

Please note that this instruction works only for patches applied to the vendor folder. Patches that are applied to the app folder can't be added to Composer. 

Step 1. Navigate to the directory of your project via CLI.

Step 2. Run the below command to install package cweagans/composer-patches that allows you to patch modules:

composer require cweagans/composer-patches

Step 3. Edit the composer.json file and add the “extra” section with the subsequent information:
  1. Module name, which the patch is applied to:
    ⇾ "amasty/shopby"
  2. Title. We recommend specifying the title that contains the ticket ID where the patch was provided and a mini-description of the modification:
    ⇾ "390600: ILN can be enabled per store view"
  3. Path to patch. Usually, such patches are added to the "patches" folder in the root Magento:
    ⇾ "patches/390600.diff"
The expected outcome should resemble the following example:

  "extra": {
      "composer-exit-on-patch-failure": true,
      "patches": {
          "amasty/shopby": {
              "390600: ILN can be enabled per store view": "patches/390600.diff"
          }
      }
  }

If a patch affects multiple modules, you must create multiple patch files targeting multiple modules.

Step 4. Apply the patch with the command below (use the -v option only if you want to see debugging information):

composer -v install

Step 5. Update the composer.lock file. The lock file tracks which patches have been applied to each Composer package in an object.

composer update --lock


If you encounter any difficulties, please do not hesitate to contact us for assistance.
      • Related Articles

      • How to apply a diff patch?

        A “patch” is a compact representation of the differences between files, intended for use with line-oriented text files. It describes how to turn one file into another, and is asymmetric: the patch from file1 to file2 is not the same as the patch for ...
      • How to apply a custom patch on Magento Cloud?

        Please make sure to test all patches in a pre-production environment. For Adobe Commerce on cloud infrastructure, you can create new branches with the magento-cloud environment:branch <branch-name> CLI command. To apply a custom patch on a Cloud ...
      • Can I send you the files of my website to investigate the issue instead of giving SSH access?

        We can work with customer instances by deploying them in our local environment, so you can send us the archived files of your root Magento directory and database the following way: Step 1. Go to your root Magento directory via CLI and run this ...
      • How to translate a Magento 2 extension?

        Generally, an extension consists of specific elements (i.e. interface, settings display, added content) which can be translated to another language. When making a translation, as the first step, look through the module's elements in the Admin panel — ...
      • What information should I provide when submitting an issue-related ticket?

        In our support center, we are struggling to provide a solution for your request as fast as possible. Though, it might get delayed through verifying details, finding out your support license, Magento and module version and etc. There are several steps ...