Hot answers tagged

191

Here is what I usually do: Always develop with error_reporting on. Always develop with isDeveloperMode set to true. Just add SetEnv MAGE_IS_DEVELOPER_MODE 1 to your httpd.conf file (or corresponding file for Nginx or something else) If the extension is linked to a core functionality add the dependency in the declaration file <depends><Mage_Catalog /...


111

You should not use the ObjectManager directly! Exception from the rule are: in static magic methods like __wakeup, serialize, etc in case you should make backward compatibility of constructor in global scope, like in fixtures of integration test. in class that needs only for creation of object like factory, proxy , etc


68

/app/code/local/Your/Customattribute/sql/your_customattribute_setup/install-0.1.0.php <?php $installer = $this; $installer->startSetup(); $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $entityTypeId = $setup->getEntityTypeId('customer'); $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); $attributeGroupId = $...


66

In Magento 2, you can actually now remove Compare products functionality via xml file. Compare products block is defined in vendor/magento/module-catalog/view/frontend/layout/default.xml and you can remove it by adding a default.xml file to your theme in: <theme_dir>/Magento_Catalog/layout/default.xml inside which you remove your block as following: ...


57

You should never use \Magento\Framework\App\ObjectManager::getInstance(). It defeats the purpose of dependency injection. We're back at Mage::getModel(). Object manager should be used only in factories and then as injected in a constructor. The advantage of using this is less code to write. But this does not make it OK. The fact that this is still used in ...


56

You can log the compiled layout XML directives which are used to generate blocks. Create an observer on controller_action_layout_generate_blocks_before, and in the observer method log the update XML from the transported layout object: public function logCompiledLayout($o) { $req = Mage::app()->getRequest(); $info = sprintf( "\nRequest: %...


56

So why does M2 sometimes access object manager directly when we recommend against it? Brutal answer: M2 is a port of M1 - not a complete rewrite. So don't assume that all the M2 code is perfectly ported yet (unfortunately). Just because you find something in the M2 code base, that does not mean "its the best way to do it". Sometimes it is just "we have not ...


55

Disabling the module on command line resolves in errors on different places. A better idea is to hide it with XML. Just create a child theme (if you don't have one already) like this: How to create a Child Theme in Magento 2 and then inside your child theme create: app/design/frontend/company_name/theme_name/Magento_Theme/layout/default.xml and add this: ...


50

yes, Magento 2 still allows you to define the default values inside a configuration file. namespace\modulename\etc\config.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <...


44

Use the below code in the controller class to get the controller, module, action and route name: <?php namespace Custom\Module\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $request; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\App\...


44

it says in the documentation you linked, this: This command works only with modules defined as Composer packages. If you did not install the module via composer, it won't work. You can only disable it via bin/magento module:disable SP_Gridthumbs. To remove the module, remove the folder SP/Gridthumbs, remove the record with module = 'SP_Gridthumbs' from ...


41

You need to upgrade the source to include the @import command in the style-l.less file with:- php bin/magento setup:upgrade Then you need to deploy static content to compile your less files with:- php bin/magento setup:static-content:deploy Also by convention the file should be named:- code/Vendor/module/view/frontend/web/css/source/_module.less


38

Try this: <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product echo $product->getId(); echo $product->getName(); ?> OR Add bellow code in your block file. for example app/code/AR/...


37

You can add following xml instruction to either your custom theme's default xml file which should be located at /app/design/frontend/Vendor/theme/Magento_Theme/layout/default.xml or to Magento_Catalog/layout/default.xml file in your custom theme: <referenceBlock name="catalog.compare.sidebar" remove="true"/> <referenceBlock name="view.addto....


35

app/code/core - Holds modules that are distributed with the base Magento and make up the core functionality. app/code/community - Holds modules that are developed by third-parties app/code/local - Holds custom modules you developed, including Mage code overrides. Why does Magento use two code pool for our customization? Magento actually uses three code ...


35

Magento 2 uses Service Contracts for retrieving and saving objects. In Magento this layer is formed by Repositories, which are managers with get() and save() methods. This keeps user code away from Model calls. Don't call model methods (like load() or save() or loadByIncrementId()) directly, they are being deprecated as custom code should use the Service ...


34

Your module naming convention is quite confusing - you're calling the module itself helper? For the purpose of explaining, I'm choosing to call your module myname_mymodule In your module ./app/code/community/MyName/MyModule/etc/config.xml, within the <global> tags <helpers> <mymodule> <class>MyName_MyModule_Helper</class&...


33

You can easily add default values for all configuration settings in your config.xml: <config> <default> <cms> <wysiwyg> <enabled>0</enabled> </wysiwyg> </cms> </default> </config> At the moment I'm unable to map your system.xml ...


32

I am a big fan of using modman so that I can develop and source control just my extension and leave the core files and folder structure unchanged. It is also makes testing across different installations run smoother. Oh and one massive tip always try to install your packaged extension locally on a clean install of magento before uploading it to Magento ...


32

I grepped the source for all compare blocks. For 2.3 this is the complete list. This will also remove the compare link from the header. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <!-- Remove compare --> &...


31

Part 1 For the purpose of this demo I'm going to create a module, let's name it Easylife_News that contains an entity named Article. The module comes with complete CRUD code for backend, including the section for managing attributes, and some simple frontend listing and view page for each article. It also ads a menu item in the top menu to the listing of ...


29

registration.php missing Apparently my Vendorname_Modulename module was missing the registration.php. I'm running the latest Magento2 version from GitHub. Every module has to register itself in the ComponentRegistrar. A typical registration.php for a module (in the root of your module) could contain: <?php use Magento\Framework\Component\...


29

create a app\code\Sugarcode\Test\Setup\UpgradeSchema.php and run upgrade command when ever version was changed just change in module.xml and in UpgradeSchema.php add one more if condition with version compare if (version_compare($context->getVersion(), '2.0.1', '<')) { // Changes here. } so when you run upgrade command it will ...


29

A DocBlock is required in app/code/Test/Calculator/Api/CalculatorInterface.php as described here: http://devdocs.magento.com/guides/v2.0/coding-standards/docblock-standard-general.html <?php namespace Test\Calculator\Api; interface CalculatorInterface { /** * Add two numbers. * * @param int $num1 * @param int $num2 * @...


28

Yes it does. First of all, less modules means less code to (potentially) load and process. Next to that, a lot of modules, like for example the Mage_Rss module run a lot of code in the background like forcing reindexes on certain events. On the method best to use: disabling a module using System > Configuration > Advanced only suppresses the output of ...


27

Sometimes we need to make modifications to existing templates. Instead of making changes directly to existing template files, we should override them in our own theme. Let us assume that we want to update the category listing page ( list.phtml ). To do so, create following directory structure: app/design/frontend/Mycompany/Basic/Magento_Catalog/templates/...


27

I use this method for creating patch, hope it helps . Let suppose you will modify this file vendor/magento/module-customer/Block/CustomerData.php Create a copy of that file with your changes on it: vendor/magento/module-customer/Block/CustomerDataModified.php What you need to do is run this command: diff -u CustomerData.php CustomerDataModified.php > ...


26

In Magento 2.0.2 you will get an error if you try what @MagePsycho suggested. You still can disable the module output by changing its status in the Advanced-Settings of Magento 2. Shops > Configuration > Advanced > Advanced and search for "Review". You might wanna flush the cache afterwards.


25

In my case, I ran php bin/magento setup:static-content:deploy and the error disappeared.


24

I have a hunch you have in your admin controller a method called _isAllowed. If that's true, I think it returns the wrong thing. It should look like this: protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('erp/stock_management/firtal_deadstock'); //or at least //return Mage::getSingleton('admin/...


Only top voted, non community-wiki answers of a minimum length are eligible