118 votes
Accepted

To use or not to use the ObjectManager directly?

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 ...
user avatar
  • 20.3k
99 votes
Accepted

When Should We Use a Repository and Factory in Magento 2?

If there is a repository and it does what you need well, always prefer the repository. Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they ...
user avatar
60 votes

To use or not to use the ObjectManager directly?

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 ...
user avatar
  • 196k
60 votes

To use or not to use the ObjectManager directly?

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 ...
user avatar
  • 3,458
47 votes
Accepted

Magento 2 as a headless solution

Answers to the questions Who is responsible for formatting data, for example prices. Magento API and frontend framework? Magento API provides access to the data and business logic. Formatting data/...
user avatar
30 votes

Magento : Fastest way to update a product attribute

Actually there are 3 ways to update an attribute on a product without save the full product. Depending on the code/requirements one can be faster that the other. Method 1: $product = Mage::...
user avatar
  • 6,855
28 votes
Accepted

What is helper in Magento?

Theoretically you should never use helpers. Helpers are just collections of unrelated methods and are always instantiated as a singletons. This is basically procedural programming with functions ...
user avatar
  • 196k
27 votes

When Should We Use a Repository and Factory in Magento 2?

Good question. Even if both Repositories and Factories let us access an Entity I think we should focus on their responsibility. From Magento documentation: "Factories are service classes that ...
user avatar
25 votes

Magento 2 - Database transaction for multiple model / resource model objects?

You are probably saving an aggregate of multiple connected objects. Determine which of these objects is the root, for example: [order] <------ this is the root ...
user avatar
24 votes

How to write a custom extension?

Andreas von Studnitz and Dr. Nikolai Krambrock gave a good presentation on code quality on the Meet Magento DE 2014. They distinguish between general code quality and Magento-specific code quality. In ...
user avatar
24 votes

To use or not to use the ObjectManager directly?

Why is Magento doing what they recommend us not to do ? Does that mean there are some cases where we should use the ObjectManager directly ? If so, what those cases ? Without knowing the full story ...
user avatar
23 votes

How to render HTML with AJAX in Magento 2

I would also go the way 2 and, indeed, you actually can render "pure" HTML via AJAX without the head, body, css and so on. The trick is to: tell your controller to instanciate a Response that is of ...
user avatar
21 votes
Accepted

Magento 2: proper use of helpers

Don't. This is like using ObjectManager::getInstance()->create() in a template! Use a custom Block that receives the helper as a constructor dependency instead, and add a proxy method that calls ...
user avatar
  • 14.4k
20 votes
Accepted

Magento2: Best practice to modify css files

You should not edit/modify files within pub/* or vendor/* directory. Pub is for deployment and vendor is for default structure, which you override via your template or custom modules. Instead: create ...
user avatar
  • 701
20 votes
Accepted

Magento 2 - good practice to use/avoid magic getters?

The above question is about using magic methods vs. getDataByKey or getDataByPath. I think there is a third option, too, and that is implementing real getter and setter methods. The getData* methods ...
user avatar
  • 14.4k
19 votes

How to write a custom extension?

If you sell your extension or share it with others, think about writing code that is human readable. don't make method too complex add DOC blocks to your methods * use proper variable names, like $...
user avatar
  • 11.5k
19 votes
Accepted

Best Way to Load a Custom Model in Magento 2

Best practice: via the service contract The best practice is always to use the service contract whenever it's possible. You can find the list of reasons here: Magento 2: what are the benefits of ...
user avatar
16 votes
Accepted

Best Practise to Add Custom Tab in Product Details (backend)

Personally I would go for a layout/action approach using the addTab() provided by Mage_Adminhtml_Block_Widget_Tabs So 2 main action are involved here: add XML layout changes create the tab class ( ...
user avatar
  • 6,855
16 votes

To use or not to use the ObjectManager directly?

Should not use Object manager directly! For instance: \Magento\Framework\App\ObjectManager::getInstance(); also if you are working with event observers or plugins, you should never use it directly....
user avatar
16 votes
Accepted

Magento 2 - How to read the values in etc/config.xml?

You cannot read the values directly from etc/config.xml. I mean, you can, but there is no point in reading directly from there since the values may be overwritten from the stores->configuration ...
user avatar
  • 196k
15 votes
Accepted

Why use a theme when you have modules?

You can do anything you fancy, but if it is the right thing to do that is the question here. Modules Modules should add or change functionality of the webshop and should/could be transferred to ...
user avatar
  • 7,362
14 votes

Magento 2: How to get data from controller to block/view

One approach is to use the registry so in your controller class you put it in the registry, and then in your block you can retrieve it. <?php namespace <Vendor>\<ModuleName>\...
user avatar
  • 3,210
14 votes

To use or not to use the ObjectManager directly?

The main reason that developers are strongly discouraged from using the Object Manager directly is that direct use of the Object Manager causes the extension not to be installable in compiled ...
user avatar
14 votes
Accepted

How to manage config.php in Magento 2

First of all, very good question. But...I don't agree with this: The problem is when someone install the project he doesn't have this file but he already have the database so he can't run bin/...
user avatar
  • 196k
13 votes

What is helper in Magento?

The question has two aspects: Should I write my own helpers and what for? Should I use the core helpers and what for? 1. Writing helpers In general, having classes named Helper, Util or similar ...
user avatar
13 votes

Magento 2 as a headless solution

I can share some insights of developing headless magento projects as my company has created 2 of them. We have decided to use reactjs as a frontend application and connect it with node powered ...
user avatar
  • 5,798
12 votes
Accepted

Strategy for rewriting classes

After having a look at your problem with this very basic class inheritance diagram (sorry for the bad drawing, by the way), I say it's better to continue with the approach you have taken and create ...
user avatar
  • 17.2k
11 votes
Accepted

Magento 2: what's the status of M2 regarding PSR compliance?

PSR-3 is already used (with Monolog as default implementation) PSR-4 is already used (via composer) PSR-6 is not supported and I know of no plans to do so, but there is a bridge implementation: ...
user avatar
11 votes

What are the best practices for installing third party extensions in Magento 2?

Install module via composer is the best way to Magento 2. Becuase there are many advantages if we install module via composer. If you want to upgrade module you just need to change the version in ...
user avatar
  • 21.7k
11 votes
Accepted

Magento 1: layout action tag best practice

So this is basically only allow two different ways to do the same thing. The code that actually picks which one to use has the "fallback". It can be found in Mage_Core_Model_Layout::_generateAction ...
user avatar

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