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.