Anshu & Jugal's answers are definitely the official way to do things, but there's also a popular method for people who use composer for integrating Magento. If the file you want to modify is within the composer vendor/
directory then you can use a package called cweagans/composer-patches
to create patches on specific files.
The benefit of this method is that it helps with upgradeability. It will patch the file after each composer install
or composer update
and if it can't apply the patch anymore (if the module was updated, for example) then the patch will fail and it'll tell you about it so you know to modify/remove the patch for the new version.
See here for the actual package and it's usage: https://github.com/cweagans/composer-patches
Basic steps are:
require
the package cweagans/composer-patches
in your project
- Create a patch list file. e.g.
composer.patches.json
- Point to the patch list file in
composer.json
- Generate your patch from a
diff
- You can generate one using
diff -u old/file.ext new/file.ext > thediff.patch
- Make sure in your generated patch that the root of the file is relative to the package's root
- i.e. if your file is at
vendor/magento/module-catalog-search/etc/search_request.xml
then the top lines would be etc/search_request.xml
. See the example below.
- In your patch list file, list your patch files against what module you're patching, with a description.
This example below was a fix for the magento/module-catalog-search
module where I modified an XML file. It can be any text file, no matter if it's PHP/PHTML, JS, XML, CSS, etc...
File: {magento_root}/composer.json
{
....
"require": {
....
"cweagans/composer-patches": "^1.6"
....
},
"extra": {
....
"patches-file": "composer.patches.json"
....
}
....
}
File: {magento_root}/composer.patches.json
{
"patches": {
"magento/module-catalog-search": {
"Change Magento search criteria rule from \"should\" to \"must\"": "patches/search-and_to_or.patch"
}
}
}
File: {magento_root}/patches/search-and_to_or.patch
--- etc/search_request.xml 2019-09-30 17:01:46.914871669 +0100
+++ etc/search_request.xml 2019-09-30 17:01:57.887042180 +0100
@@ -13,7 +13,7 @@
</dimensions>
<queries>
<query xsi:type="boolQuery" name="quick_search_container" boost="1">
- <queryReference clause="should" ref="search" />
+ <queryReference clause="must" ref="search" />
<queryReference clause="must" ref="category"/>
<queryReference clause="must" ref="price"/>
<queryReference clause="must" ref="visibility"/>