Skip to content

Instantly share code, notes, and snippets.

@cmtickle
Last active May 10, 2024 12:04
Show Gist options
  • Save cmtickle/8900629447429126ffd7ff84e56ec780 to your computer and use it in GitHub Desktop.
Save cmtickle/8900629447429126ffd7ff84e56ec780 to your computer and use it in GitHub Desktop.
Patching Magento 2 Code

To patch code in Composer modules

  1. composer require cweagans/composer-patches
  2. Create your patch file as normal (referencing the paths to file in /vendor) and put it in a '.patches' folder at the top level of your code base.
  3. Edit composer.json to apply the patch(es) as below (this goes at the first level of composer.json) :
    "extra": {
        "magento-force": "override",
        "enable-patching": true,
        "patches-file": "composer.patches.json"
    },
  1. Create composer.patches.json with content as per below example:
{
  "patches": {
    "magento/framework": {
      "MAG231-SALESRULE-FIX-UPG": ".patches/MAG231-SALESRULE-FIX-UPG.patch"
    },
    "magento/module-checkout-agreements": {
      "MAGFIX_231_AGREEMENT_CONFIG": ".patches/MAGFIX_231_AGREEMENT_CONFIG.patch"
    }
}

To patch code in /app/code

  1. Create your patch file as normal (referencing the paths to file in app/code/) and put it in a '.patches' folder at the top level of your code base.
  2. Edit composer.json to apply the patch(es) as below (this goes at the first level of composer.json) :
{
<other json here>
    "scripts": {
        "post-update-cmd": [
            "patch -p1 < .patches/app_code_some.patch",
            "patch -p1 < .patches/app_code_another.patch"
        ],
        "post-install-cmd": [
            "patch -p1 < .patches/app_code_some.patch",
            "patch -p1 < .patches/app_code_another.patch"
        ]
    }
}
@erikhansen
Copy link

@cmtickle Got it. Considering that I run composer install and composer update **** dozens/hundreds of times across the course of an M2 project, I think that would get old after a while. :) But to each their own!

@cmtickle
Copy link
Author

@erikhansen I guess once you're confident the patches apply you could just use the --no-scripts: argument for composer to avoid applying the patches again?

@xtremevision
Copy link

Remembering to reset the original unpatched version or using --no-scripts argument is not sustainable, not long term, not across multiple projects. The patch module should keep track of the patches applied and skip them. My 2c worth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment