Skip to content

Instantly share code, notes, and snippets.

@erikhansen
Last active February 28, 2023 13:59
Show Gist options
  • Save erikhansen/cac936c313ac67ae1cb0d36d3e3b0b86 to your computer and use it in GitHub Desktop.
Save erikhansen/cac936c313ac67ae1cb0d36d3e3b0b86 to your computer and use it in GitHub Desktop.
Patch Magento 2 extension that must be installed in app/code

See also this alternative approach: https://gist.github.com/cmtickle/8900629447429126ffd7ff84e56ec780#to-patch-code-in-appcode

If you need to patch an M2 extension that isn't available to be installed via Composer, and you're concerned about losing edits directly to files in app/code/<Vendor> directory, you can install and then patch the extension locally using these steps:

  1. Copy the extension into an arbitrary folder location like app/code/packages/VendorName/ModuleName (this assumes this module has a composer.json file with the package name of vendorname/module-modulename)

  2. Run this command to add your custom package location to the composer repositories list:

    composer config repositories.vendorname/module-modulename path app/packages/VendorName/ModuleName
    
  3. Require the extension:

    composer require vendorname/module-modulename
    
  4. Now that you've installed the extension via composer, you can patch any files using composer-patches without worrying about losing edits when you upgrade the extension with updates from the vendor. See this article for more about composer-patches.

Important: One thing to be aware of when using this approach is that if you are patching an extension in app/packages, when you run composer install locally, the patch changes will be applied to the files in app/packages, and the changes will show as "modified" in Git. You'll be tempted to commit those changes, but you should not, because that will break the ability for composer-patches to apply the patches when you do a clean clone of the repo and run composer install later. Instead, you should reset the modifications each time you run composer install locally.

Inspiration from this article and specifically this comment from @navarr.

@brunoviana
Copy link

Thank you

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