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 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 fix the "Unable to apply data patch Amasty\ShopbyBase\Setup\Patch\Data\AnchorRootCategories..." error?

        According to the Shop by Brand code base, the extension checks the root categories on the Magento instance and makes them anchored. Due to some instance-specific factors, the process may fail resulting in the following error while running the bin ...
      • 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 ...
      • How to revert a 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 is CSS customization done in the Jet Theme?

        Making style changes in any theme can be done in several different ways: by creating a child theme and using the backend configurations of it to experiment with different styles (this is the safest way); by extending the parent theme's styles through ...