Questions tagged [best-practice]

Denotes questions related to best-practices in Magento.

Filter by
Sorted by
Tagged with
160 votes
10 answers
183k views

To use or not to use the ObjectManager directly?

Ok, so yesterday we had a big talk with other people from the Magento community regarding the direct use of the ObjectManager in classes/templates. I'm already aware of the reasons why we shouldn't ...
Raphael at Digital Pianism's user avatar
148 votes
5 answers
12k views

How to write a custom extension?

Because I had a lot of issues lately with free and commercial extension I've decided to ask this question and answer it with the steps I usually follow when writing an extension. Feel free to edit the ...
Marius's user avatar
  • 198k
108 votes
6 answers
45k views

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

I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities: Retrieve Data Using A ...
Rajeev K Tomy's user avatar
55 votes
3 answers
18k views

Magento 2 as a headless solution

I want to know if there are some best practices to use Magento 2 as a headless E-commerce solution. A typical E-commerce in 2017 is to have an omni-channel solution which includes E-commerce CMS ...
Franck Garnier's user avatar
53 votes
6 answers
17k views

Modern Magento 1.X Workflow and Development Tools

I am new to Magento Development ( CE 1.6 ) and am still trying to define my workflow. I currently develop on Mac OSX 10.8 with Netbeans 7.3, but I find that Netbeans is slow and freezes on me. I tend ...
ryanF's user avatar
  • 2,269
45 votes
2 answers
22k views

What's the preferred way of throwing exceptions in Magento?

All of the following methods are used in Magento core so which one is the preferred (or the latest "best practice") way? Mage::throwException('Some Message') - 732 Usages throw new Exception('Some ...
Samuel Katz's user avatar
27 votes
4 answers
34k views

Efficient get product url from id

What is the most efficient way of getting a product url given just it's ID? In a few places in our code we have things such as Mage::getModel('catalog/product')->load($id)->getProductUrl() in ...
Peter O'Callaghan's user avatar
27 votes
4 answers
4k views

Returning $this after observer

I see some conflicting information on the internet and in 3rd party modules alike - is it a requirement or best practice to return $this at the end of an observer method? E.g.: ...
philwinkle's user avatar
  • 35.8k
22 votes
6 answers
18k views

What is helper in Magento?

What is helper in Magento? In what cases should one use and not use helpers?
Supersonic's user avatar
21 votes
2 answers
3k views

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

Magic getters on Varien_Object (M1) and DataObject (M2) are common practice, but with Magento 2 it feels wrong to use it. Good: easy to read/write Bad It causes problems when using digits in keys (...
sv3n's user avatar
  • 11.6k
19 votes
2 answers
20k views

Best Way to Load a Custom Model in Magento 2

Because it was hard for me to find the right way, below you could find the best practice I made mine. Enjoy, correct my English if needed and say me I'm wrong if I am. :) Edit : ... and I found out I ...
Nicolas PERNOT's user avatar
19 votes
2 answers
1k views

Magento 2 DI best practices

Let's say I'm building a Magento 2 extension that does....well...not important. Let's say it does super awesome stuff. But I want to make sure that this is build using the proper standards so other ...
Marius's user avatar
  • 198k
17 votes
5 answers
5k views

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

While working on a client project for Magento 2 - I've discovered numerous methods of loading in and keeping track of third party extensions. Going into this assuming that we're using the integrator ...
Navarr's user avatar
  • 1,712
17 votes
1 answer
4k views

What is the best practice way in Magento 2 for creating Many To Many relationships?

I've looked around the core and seen a few examples of many to many relationships between models, but I can't see a definitive answer on this. As an example, say we create a new model and we want to ...
Chris's user avatar
  • 1,004
16 votes
5 answers
41k views

Magento2: Best practice to modify css files

I want to modify css file pub/static/frontend/Magento/luma/en_US/css/styles-l.css. Initially this file is not present in pub/static folder and it's present in vendor/magento/theme-frontend-blank/web/...
amitshree's user avatar
  • 6,896
15 votes
3 answers
23k views

Magento : Fastest way to update a product attribute

I'm looking for the fastest and reliable method for mass attribute update. Below are the methods am aware of but I don't know with which I should go. $store_id = 0; Mage::getSingleton('catalog/...
Deepak Mallah's user avatar
15 votes
4 answers
3k views

Magento 2 best practice for class locations and names

In Magento 1 we were used to place our classes in these directories Block Helper Model Resource and use a simple class name without any capital letters in the middle of the name. ...
Matthéo Geoffray's user avatar
15 votes
1 answer
11k views

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

If I have a logic that involve updating multiple model objects which will update a few tables in database, how to provide a database transaction to ensure the data integrity?
TFS's user avatar
  • 4,289
15 votes
1 answer
8k views

How to manage config.php in Magento 2

First of all it is maybe an already answered question but I did not find anything about that subject. It is not an issue but a question about the best way to manage the file config.php in Magento ...
Matthéo Geoffray's user avatar
14 votes
4 answers
2k views

Is it good to instantiate a getModel class on phtml templates?

This is a question regarding a good programming practices in Magento. I need to show (in the category product list) the product with its related products in thumbnails. So I edited mypackage/mytheme/...
user604's user avatar
  • 141
13 votes
1 answer
2k views

Avoiding save in a loop in mass action

I've created my own CRUD module that contains an inline edit action similar to the one for CMS pages Everything works OK, but when running phpsniffer with the EcgM2 standard I get this warning: ...
Marius's user avatar
  • 198k
13 votes
1 answer
309 views

Is there a good reason to put the same code in both try AND catch blocks?

In the file: app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Datetime.php, within the render() method, it seems like the exact same code is being run in both the try and catch blocks. ...
David Angel's user avatar
12 votes
5 answers
22k views

How to render HTML with AJAX in Magento 2

I try to find the best way to render HTML through AJAX in Magento 2. Way 1 : Using Controller without layout File Foo/Bar/Controller/Popin/Content.php <?php namespace Foo\Bar\Controller\...
Matthéo Geoffray's user avatar
11 votes
4 answers
25k views

Best practice to join product attributes

I have a custom table with a product reference product_id. Now I would like to show product information (sku, name) in my backend grid, but I am unsure what is the best practice to do this? My best ...
Celldweller's user avatar
11 votes
3 answers
7k views

Right way to implement getExtensionAttributes()

I was wondering, what's the right way to implement an extensible EAV model. I see that in Magento\Catalog\Model\Product, the method getExtensionAttributes() is implemented like this: public function ...
Fabian Schmengler's user avatar
10 votes
3 answers
5k views

Magento 2: proper use of helpers

I'm starting to see more and more people declaring helpers classes in order to be able to use the following in the template files: $this->helper('Path/To/Helper/Class')->customMethod(); This ...
Raphael at Digital Pianism's user avatar
10 votes
3 answers
2k views

ECG compliance: How to do?

There is something like a coding standard for Magento 1, called ECG where numerous things should be avoided or methods that should not be used. Let's try to create a list that covers all cases that ...
sv3n's user avatar
  • 11.6k
10 votes
5 answers
2k views

Where do I put .php, .js, .html, .css files from a 3rd party lib that interfaces with an extension I develop?

Let's say I want to develop a Magento extension that interfaces to, say, an Open Source charting package or an image gallery or whatever that is NOT part of the extension itself. When downloaded (...
fris's user avatar
  • 894
10 votes
2 answers
802 views

How To Properly Disable a (Model) Rewrite

There is a third-party extension which rewrites a specific Magento core class. It should not matter if it is a block, helper or model, but in this case it is a model. Now I encounter that this rewrite ...
Simon's user avatar
  • 5,695
10 votes
3 answers
2k views

Magento 1: Performance optimizations to delete entities

I'm currently trying to improve a couple of modules regarding performance. Some of you may know the usage of the walk() method on collection which is very useful to avoid looping through products ...
Raphael at Digital Pianism's user avatar
9 votes
3 answers
17k views

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

I would like to add an extra tab to the product details ( backend ) and I don't want to perform any override, if possible. What is the best way to accomplish this ?
Fra's user avatar
  • 6,955
9 votes
2 answers
1k views

Why use a theme when you have modules?

Inside of my custom module I have added the file Vendor/Theme/view/layout/default.xml which I am using to override a lot of the site's global templating with templates/blocks/layouts from the custom ...
Daniel Thompson's user avatar
9 votes
1 answer
12k views

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

How to define and read the values in etc/config.xml in a custom module? What is the best practice to use it?
TFS's user avatar
  • 4,289
9 votes
1 answer
907 views

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

So according to the official documentation, I understand that Magento 2 is PSR-1 and PSR-2 compliant. I'm wondering if Magento 2 is (or will be) compliant with the others ones ? PSR-3: Logger ...
Raphael at Digital Pianism's user avatar
9 votes
3 answers
9k views

Magento 2 - Best practice to get current date

I have to save an updated_at attribute of a custom entity and to know which is the best practice I took a look at how Magento 2 handles it. I looked at the \Magento\Eav\Model\Entity\Attribute\Backend\...
Alessandro Ronchi's user avatar
8 votes
2 answers
3k views

How to handle deprecated "Mysql4" classes when rewriting resource models

I have to rewrite Mage_Catalog_Model_Resource_Category_Flat. The old resource class Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat is still there in Magento 1.7 for backwards compatibility and ...
Fabian Schmengler's user avatar
7 votes
2 answers
635 views

Strategy for rewriting classes

[TL:DR] I have Core_Class_A and Core_Class_B extends Core_Class_A. I need to rewrite both of them and add a common functionality. So I made this: Custom_Class_A extends Core_Class_A. The question ...
Marius's user avatar
  • 198k
7 votes
3 answers
18k views

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

I am looking for the right flow of data of how Magento wants me to do it. I have some data which I want to pass to the view. How do I do this the right way? I saw the following answer for Magento 1.x ...
Akif's user avatar
  • 1,492
7 votes
6 answers
8k views

Best practice for local development

I've been trying to find the best way on how to develop a Magento 2 webshop locally. So far, I had no luck: lots of JS, CSS and 404 errors (on which I tried lots of fixes which only partly solved my ...
Condor's user avatar
  • 637
7 votes
1 answer
3k views

Magento 2 Workflow and Development Tools

I am curious what other members of the community are using and suggest for M2 development: "What are some suggested Magento 2.X workflows?" "What tools/configurations/plugins are being used for ...
ryanF's user avatar
  • 2,269
7 votes
2 answers
94 views

Is it best practice for a Block function to call a ResourceModel function?

Is it appropriate for a Block class to retrieve data through a function in a ResourceModel class, rather than a Model class? Or should the Block call the Model which then passes the call to the ...
fris's user avatar
  • 894
7 votes
1 answer
535 views

Best practices for running and updating multiple separate stores

3 years ago I was a part of a company that ran magento webshops for many independent customers. What we experienced at the end was that if we wanted to do a change that affects all the shops (bug fix) ...
David Gulbrandsen's user avatar
7 votes
3 answers
8k views

Best Practice for staging/development site

I am new to magento and have been working a my first site and it is now ready to go live. I have been advised that once live to also build a staging or development site so that i can in the future ...
odd_duck's user avatar
  • 1,396
7 votes
1 answer
219 views

Good practice regarding core tables relations with modules tables

This a question regarding Magento development, it can apply for both M1 and M2. For the last weeks, I've been working on several modules that are directly related to native Magento entities/database ...
Raphael at Digital Pianism's user avatar
7 votes
2 answers
298 views

How to avoid Law of Demeter violation ("Newable objects should not hold a field reference to an injectable object")

In the Rules for using dependency injection, the Magento 2 devdocs state: Newable objects should not hold a field reference to an injectable object nor should they request one in their constructor. ...
Fabian Schmengler's user avatar
7 votes
1 answer
1k views

Attrbributes Sets & Attributes: Non-Technical Overview/Logic

I work on the 'business' side of Magento (i.e. I'm not a developer). I'm looking to get a better understanding of the theory behind Attributes & Attribute Sets. My goal: Come up with a few rules ...
user700's user avatar
  • 71
6 votes
4 answers
10k views

Magento 2: how to use the collection walk iterator?

Back in Magento 1, it was possible to use the collection iterator to walk through the results and avoid looping through them. It was a huge improvement in terms of performance when dealing with ...
Raphael at Digital Pianism's user avatar
6 votes
1 answer
872 views

Magento 1: layout action tag best practice

While studying for the Frontend Certification, I noticed a new thing I never came accross. Basically when I want to use the <action> tag, let's say to change the template of a block, I'm used ...
Raphael at Digital Pianism's user avatar
6 votes
2 answers
483 views

Mage_Catalog_Model_Product_TYPE::TYPE_CONFIGURABLE vs 'configurable'

I'm working on an heavy customised magento version. Cancelling an order returned this fatal error: Mage_Catalog_Model_Product_TYPE not found on this line code: if($parent->getTypeId() == ...
apedic's user avatar
  • 923
6 votes
1 answer
153 views

Usage of superfluous (and potentially dangerous) standard code in installer scripts

I always thought it's some kind of epidemic plague that users behave in a high risk manner introducing $installer->startSetup() and $installer->stopSetup() calls in installer scripts. Many of ...
hakre's user avatar
  • 2,821

1
2 3 4 5