Magento 2 Snippets
Published on 02 Jun 2016 , edited on 18 Jun 2019In it’s complexity I find one can lose a lot of time in just looking for the right mark-up for the right situation. That’s why I decided to make this little ‘scrap-book’ of snippets.
Overwriting files
Overwriting files in Magento 2 isn’t as straightforward as was with Magento 1. There you just had to have the same path and you’re done. In Magento 2, this is a bit different.
Take a look at these examples and try to see the pattern:
Source/vendor/magento/module-theme/view/frontend/templates/html/header.phtml
Goal/app/design/frontend/${VENDORNAME}/${THEMENAME}/Magento_Theme/templates/html/header.phtml
Sourcevendor/magento/module-theme/view/frontend/templates/html/topmenu.phtml
Goalapp/design/frontend/${VENDORNAME}/${THEMENAME}/Magento_Theme/templates/html/topmenu.phtml
Source/vendor/magento/module-catalog/view/frontend/templates/category/image.phtml
Goalapp/design/frontend/${VENDORNAME}/${THEMENAME}/Magento_Catalog/templates/html/topmenu.phtml
Source/vendor/magento/module-catalog/view/frontend/templates/category/image.phtml
Goalapp/design/frontend/${VENDORNAME}/${THEMENAME}/Magento_Catalog/templates/html/topmenu.phtml
User management
For some reason I forget my password every single f%&*@Q!ng time I try to login to my testenviorment, despite having a password manager and already 2 admin accounts because of this. This oneline adds a new admin user to login with and update the password for the ‘regular’ one. Ofcourse, delete this user afterwards.
magento admin:user:create --admin-user bert --admin-password bert01234 --admin-email bert@bert.nl --admin-firstname bert --admin-lastname bert
Module management
Install module via composer
composer require <component-name>:<version>
magento module:status
magento module:enable <modulename> --clear-static-content
magento setup:upgrade
magento setup:di:compile
magento cache:clean
Error handling
Find class errors, from root;
grep -rnw -e 'Vendor\\Log\\Logger'
rm -rf var/generation/* var/cache/* var/report/* pub/static/frontend/* pub/static/adminhtml/* var/page_cache/* var/di/* generated/*
magento c:c
magento c:f
magento setup:upgrade
magento s:d:c