Questions tagged [best-practice]

Denotes questions related to best-practices in Magento.

Filter by
Sorted by
Tagged with
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\...
0 votes
1 answer
184 views

Magento 2 - Best practices - Helper vs Model : Which one to get config values?

I cannot seem to find an official information about what is the recommended way to retrieve configuration values in Magento 2. I am searching for the recommended way to retrieve values from the ...
0 votes
1 answer
47 views

Using Env.php to Override Module Status in Config.php

What I am trying to achieve is overriding specific modules declared as enabled in the app/etc/config.php to be disabled by the app/etc/env.php file. A use case of that would be the 2FA module which I ...
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 ...
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 ...
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 ...
0 votes
1 answer
51 views

Getting a Product model from db

I've just started in the world of Magento and throughout the process of many questions I have came across one its proving dificult to find the best description for it. Considering the following ...
1 vote
2 answers
64 views

Current best practices for controllers

Now that we're not supposed to extend Action\Action anymore, is the current best practice to create a separate Controller for each CRUD action? One for create, one for delete etc?
0 votes
1 answer
542 views

Adding an Extension Attribute to Sales Order Items - Magento 2

I have created some custom attributes for products and using these attributes in order view in customer account orders section. I have deleted few products. When I view the customer orders, it gives ...
0 votes
2 answers
129 views

Indentation Style (Code style) Magento 2

Hey, I wanna check if there is some documentation about the code style for magento, specially regarding the best practices Indentation style for magento. I will give 2 examples, and I wanna also know ...
0 votes
1 answer
481 views

What is the best way to override constructor of a class - Magento 2.4.3

I want to make a single line change in the constructor of a core class. What would be the best recommended practice to achieve it. Here is what my existing constructor /** * Download constructor. ...
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?
3 votes
1 answer
3k views

Magento best practices: try / catch in templates

I have a question about best practices in templates. Let's assume I am editing the product details template file. I would like to call a method in the product model which can raise an exception. ...
2 votes
1 answer
3k views

Magento Recommended way to add comment to order

I am looking for the Magento recommended best practice for adding a comment to an order. I can easily accomplish this using things such as $order->addCommentToStatusHistory($message)->save() and ...
0 votes
1 answer
31 views

how do i make an admin grid based on sales_order table (some columns but not all) using best practice?

Let's say I need an admin grid where not all columns of the sales_order table will be, but only some of them. For example: customer_id customer_name nickname How best to solve such a problem?
2 votes
1 answer
34 views

Where do static library initializations go?

Suppose that I have a library located in: lib/Acme/ This library has to be statically initialized (once!) through the following call: AcmeBootstrap::init($data); $data in the above call is ...
3 votes
1 answer
113 views

Examples of well written modules

Can anyone point me in the direction (github or magento connect) of any well written modules in terms of code/architecture and not functionality? Preferably also using unit tests or magespec. It ...
5 votes
4 answers
384 views

What is the proper way to add theme-specific translations of new strings?

We are creating a new template and have some new text which are not yet defined anywhere. If they would be defined before we would add some "Mage_TheModule::Foo";"Fuh" to the translate.csv in the ...
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?
0 votes
2 answers
645 views

Magento 2 - Logic in helper or block

We can put our logic inside the block. Why we still need a helper? What is the best practice of using a helper class?
0 votes
1 answer
267 views

The best ways to practice Magento 2 when not in work

Any recommendations for how to practice Magento 2 when not working? i.e. it's easier when working as you're presented with real life task, issues, clients etc. but when practicing at home, I'm just ...
0 votes
1 answer
120 views

Join array values and keys between two php arrays

I need to merge (sort of join) two arrays with both same keys, and in result i want to have the values of the first as keys of the second one : Example : $keyArray = [ "key1" => "...
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 ...
3 votes
0 answers
408 views

Implementation of Composition Instead of Inheritance in Controllers Magento 2

I am trying to implement Magento 2 Coding Standard so I found an Article Here -> https://community.magento.com/t5/Magento-DevBlog/Decomposition-of-Magento-Controllers/ba-p/430883 I have changed my ...
1 vote
0 answers
69 views

Should I be integrating Babel with the frontend javascript in my Magento2 module?

I created a magento 2 module which is using mostly javascript. I want to know what is best practice for implementing backwards compatibility across older browsers in magento2.
4 votes
3 answers
235 views

Magento 2 Extension Best practices

If we list the things that make sure the extension has followed the best Magento practices, what it would be? few of them like Do not use object manager (use constructor dependency instead). Do not ...
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 ...
3 votes
1 answer
706 views

Magento 2 and git, best practice for custom module development

I have trouble wrapping my head around a very particular issue concerning a development setup for magento 2. I have read a lot about using composer and git. It makes a lot of sense to NOT include the ...
1 vote
1 answer
2k views

Changing the schedule of magento2 Crons

I have on setup on Magento and i have set cronjobs as per Magento guidelines as below. * * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" &...
3 votes
1 answer
146 views

Is it better to use solely around function instead of before+after combined?

The codes below can achieve the same output: Using solely around plugin public function aroundXXX( SomeClass $subject, \Closure $proceed ){ beforeFunctions(); /*Perform XXX*/ $...
0 votes
1 answer
343 views

Why should I (not) put my extension into app/code?

a dumb question. From: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/module-file-structure.html Magento installs third-party components in the <Magento install directory>/...
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 ...
0 votes
3 answers
79 views

CSS good practice : inside theme or module folder?

I just want to know what is the best practice about css in Magento2. We have the possibility to have design in Vendor_Name/MyModule/view/frontend/web/css and we can also put the design in : app/...
1 vote
3 answers
3k views

Magento 2 - Accessing a helper without using constructor injection

Is there a clean way to initiate and access a custom helper without using the constructor? Currently I instantiate the custom helper using the constructor of the model as shown here as the accepted ...
0 votes
1 answer
107 views

Magento 2: How to add a class to an observer without construct

I am making this observer class to check if a visitor is logged in. use Magento\Framework\Event\ObserverInterface; use Magento\Customer\Model\Session; class CheckLoginObserver implements ...
1 vote
2 answers
2k views

Need fix for the codesniffer warning "The use of function sleep() is discouraged (Magento2.Functions.DiscouragedFunction.Discouraged)"

When I run PHP Code sniffer on my code, I see the below warning. WARNING | The use of function sleep() is discouraged (Magento2.Functions.DiscouragedFunction.Discouraged) Which class method ...
2 votes
2 answers
2k views

What should we use instead of the Magento\Checkout\Model\Cart model?

The Magento\Checkout\Model\Cart class was marked as deprecated a long time ago (since 2.1 ver.). What should I use instead of it to work with a customers cart? I know that I can work directly with a ...
2 votes
1 answer
528 views

Which files and directories we should commit to VCS

The fresh Magento installation brings a lot of files and folders to the table. When I measure, it takes like ~150MB. However, I don't think we need them all in VCS (i.e. git), because I can always ...
0 votes
1 answer
1k views

What is the fastest way to check if a configurable product is salable?

A product is salable or not is determined based on below logic: Product should be enabled. Product should be marked in-stock. qty -(minus) sum of reserved quantity in inventory_reservation table ...
1 vote
0 answers
31 views

Flat catalog tables considered a bad practice since M2.1.x and above? [duplicate]

Magento no longer recommends the use of a flat catalog as a best practice. Continued use of this feature is known to cause performance degradation and other indexing issues. A detailed description and ...
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\...
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/...
1 vote
0 answers
31 views

Code Pattern to Define and Use Javscript Callbacks / Hooks

We are using a third party module, that updates prices via AJAX on product / category pages. No we have to react on this update, to update some custom pricing logic. Of course we do not want to patch ...
0 votes
1 answer
353 views

CQRS module example magento2

Can anyone point to some decent github repository where module implements CQRS, SOLID, DRY and testing? I was reading about MSI, but it is kind of complex and huge and I am looking for something ...
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 ...
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 ...
6 votes
2 answers
5k views

How to use extension attributes for customer EAV attributes?

I've created custom customer attributes via InstallData and want to make them available as extension attributes. But I have trouble with the admin interface and am unsure if I'm doing it right. I've ...
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.: ...
0 votes
3 answers
494 views

How to deploy to production server customizations made in vendor folder?

So, We have 3 environments: development, staging and production. .gitignore file includes "vendor" folder and it has been working fine for us fine. Once We pull changes in Staging We would just run: ...
1 vote
2 answers
1k views

Magento 2 remove version number impact

I noticed if we type /magento_version along with site url then it displays our magento version. So anyone with bad intention can check my version and exploit it. to prevent this I can disable the ...

1
2 3 4 5