Google logo
4.9 on Google
Get a free Magento audit

Recommendations on page speed, accessibility issues, and real user experience data

Skip to main article

Creating Composer Patches in Magento 2

Patches are ideal for quick, isolated fixes to third-party or core code, minimising complexity and ensuring future compatibility without needing full overrides or plugins.

Creating Composer Patches in Magento 2
10 mins

Develo is a leading Magento agency and eCommerce web development company based in Birmingham, in the UK, serving clients globally since 2010.

1. Install the Composer Patches Module

Ensure the cweagans/composer-patches package is installed:

composer require cweagans/composer-patches

Note for Magento Cloud Projects: If you’re using Magento Cloud, patches in the m2-hotfixes folder are processed automatically during deployment, so the cweagans module is not required, and patches don’t need to be referenced in composer.json.

2. Create a Patch

Follow these steps to create a patch for files not tracked by Git (example: patching vendor/magento/module-catalog/Controller/Product/Compare.php):

Step-by-Step Process:

1. Navigate to the Package to be Patched:

cd vendor/magento/module-catalog

2. Initialise a Temporary Git Repo:

git init . git add -A . git commit -m "Initial commit for creating a diff"

3. Make the Necessary Changes in your editor (e.g., Compare.php).

4. Commit the Changes:

git add -A . git commit -m "Commit explaining the changes"

5. Create the Patch File:

git format-patch -1 HEAD

6. Rename and Move the Patch:

Move the patch to a dedicated patches directory in your Magento root. Use descriptive names like github-issue-6474.patch or MAGETWO-56699.patch for easier reference.

7. Clean Up the Temporary Git Repository:

rm -rf .git


3. Apply the Patch in composer.json

Update composer.json to apply the patch:

"extra": { 
    "patches": { 
        "magento/module-catalog": { 
            "Fix description or issue reference": "patches/github-issue-6474.patch" 
        } 
    }
}


4. Apply the Patch via Composer

Run the following to apply the patch:

composer install


Last updated: October 18, 2024

Looking to become a Magento Expert?

Learn with us

Latest learning resources

Practical ideas, helpful platform tips, and smart strategies to help your eCommerce store thrive.