Skip to main content

Questions tagged [best-practice]

Denotes questions related to best-practices in Magento.

Filter by
Sorted by
Tagged with
12 votes
5 answers
23k 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
2 votes
1 answer
362 views

Magento 2 module file permissions and php file creation functions?

What permissions(rights) should Magento 2 module files/dir have in production environment? Can I use php functions that creates a file inside a module directory (fwrite)? I am considering writing db ...
user3748173's user avatar
6 votes
1 answer
878 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
2 votes
2 answers
116 views

Do you have any Tips for Magento 2 SEO? Help me [closed]

I am a newbie in Magento 2 and I am learning about SEO. Is there anyone who knows about Magento 2 seo best-practices?
Mark Tan's user avatar
4 votes
3 answers
2k views

What is faster - getting raw attribute value or use collection?

I need to load one attribute value(weight) for many product IDs. Say list of the product ids is in array $productIDs. In general case, which approach is faster, Loading raw attribute value for ...
vladPavlov's user avatar
2 votes
1 answer
2k views

Dynamically instanciate class with arguments

I am actually trying to instanciate a class dynamically with these arguments. Here is the code I use in class A: /** @var string $source */ $source = '\Foo\Bar\My\Class'; // Dynamic value that I ...
Matthéo Geoffray's user avatar
0 votes
1 answer
1k views

Best practice: Static block on Category page

I want to place a full width static block at the bottom of the category page (of every category). I am quite new to Magento 2, and I was wondering what the best practice is for this?
Maarten Wolfsen's user avatar
1 vote
0 answers
37 views

Magento 2: Should we use API in setup as best practice?

Some time ago I opened an issue on magento2 about a possible problem I've spotted in Magento EavSetup class. In the discussion that follows some Magento devs are saying that for module setup (...
Mir's user avatar
  • 466
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
0 votes
0 answers
35 views

Magento 2 theme to build in

I want to practice with adding themes to a Magento 2 environment, but I don't want to waste time on skinning the shop first. I've been searching on Google for Magento 2 themes, but all I'm able to ...
user3478148's user avatar
1 vote
1 answer
2k views

Magento 2 Save custom entity primary key

Before all I am using Magento 2.1.2, with a custom entity. I am trying to save multiple row in a flat table that I have created with $connection->insertMultiple($this->getMainTable(), $answers)...
Matthéo Geoffray's user avatar
1 vote
1 answer
326 views

Factory methods, should I manually create them?

So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually ...
Brideo's user avatar
  • 404
3 votes
2 answers
3k views

Magento 2 Layout Events: controller_action_layout_render_before vs. layout_render_before

Magento 2 has two sets of events that are triggered before the layout is rendered: in \Magento_Framework_App_View::renderLayout() controller_action_layout_render_before ...
Fabian Schmengler's user avatar
4 votes
2 answers
229 views

Best practice for location of a class

I've created a Magento 2 module and I set up a class that extends Magento\Framework\DataObject that I'm using to pass data between my helper (e.g. Package\Module\Helper\Data) and model (e.g. Package\...
jared_flack'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,339
0 votes
2 answers
661 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?
TFS's user avatar
  • 4,339
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,339
4 votes
1 answer
1k views

Magento 2 - CurrentCustomer or Session?

Which class I should inject to get the current customer? What are their differences? \Magento\Customer\Helper\Session\CurrentCustomer \Magento\Customer\Model\Session
TFS's user avatar
  • 4,339
1 vote
1 answer
40 views

How to share code intelligence between several blocks of the same module?

How to share code intelligence between several blocks of the same module ? Is there a "Magento way" to do this ? or should I simply use Traits to share some functions between a given module class ?
Gael COAT's user avatar
  • 387
3 votes
1 answer
52 views

All code intelligence in Block and nothing in Controller?

Is it a good practice in Magento CE 2.1 to put all code intelligence of a module in Blocks and leave the Controller almost empty ?
Gael COAT's user avatar
  • 387
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
13 votes
1 answer
339 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
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
5 votes
1 answer
4k views

Magento 2 - SQL Injection

How to avoid sql injection when running a native sql query with parameter input? e.g. $sql = "Select * from eav_attribute where attribute_id = {$attribute_id} "; $result = $this->_connection->...
TFS's user avatar
  • 4,339
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
2 votes
0 answers
427 views

How to proper extend variables and override module style?

I am new to Magento and currently developing a child theme inherited from the Magento blank theme. I read the developer documentation and doing okay. I created my own style.less in my web directory ...
Dominik Spiertz's user avatar
6 votes
1 answer
2k views

What is the preferred base class for Payment method

I will write a payment module and read some tutorials. All tutorials are using \Magento\Payment\Model\Method\Cc or \Magento\Payment\Model\Method\AbstractMethod to extend from, but both are deprecated. ...
bitkorn's user avatar
  • 175
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
3 votes
1 answer
531 views

Mage_Catalog_Model_Product::getImageUrl() deprecated since 1.1.5

I found some interesting methods in the product model /** * Return re-sized image URL * * @deprecated since 1.1.5 * @return string */ public function getImageUrl() { return (string)$this->...
Fabian Schmengler's user avatar
2 votes
0 answers
647 views

Product brand (attribute, category or both)?

I am fairly new to Magento module development, and I am dealing with catalog/product pages at the moment. I have seen our clients using different approaches when dealing with storing product brands. ...
Emke's user avatar
  • 133
0 votes
1 answer
483 views

learning approach for a Magento beginner [duplicate]

I am new to magento. Before the question, my background: I have some previous experience in php. I have developed a small e-commerce site for food ordering as part of my course project. (but I have ...
user avatar
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
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
5 votes
3 answers
730 views

Most efficient way to get data in templates

I've created a small template in which I need access to information such as the currency code, cart total etc. I'm able to get them with the methods below, but which is the most efficient? Is there ...
Holly's user avatar
  • 4,873
0 votes
1 answer
2k views

Should I use dynamic variables in cache tags?

TL;DR Can/should cache tags in Magento be dynamic? Background I'm creating a CMS integration from an external CMS into Magento. The data is stored in Elasticsearch in AWS. I have a custom ...
scrowler's user avatar
  • 2,014
3 votes
2 answers
325 views

Magento 1: use of observers and missing native core setter?

Yet another best practice question. So here's the thing, I have several modules that deal with setting different data on the core Magento block. I don't like to rewrite entire blocks and I tend to ...
Raphael at Digital Pianism's user avatar
3 votes
1 answer
81 views

Magento 2 Session Service Layer

I'm trying to figure out the best way to interact with the Customer session using the service layer. I couldn't identify the proper repository to use. Any ideas?
ryanF's user avatar
  • 2,279
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
1 vote
0 answers
175 views

Magento 2 Code Repository Approach

I would like to know how members of the community are approaching the storage of the M2 codebase (ex: git) and sql dumps for scalability and quick deployments (also allowing for quick rollbacks). I ...
ryanF's user avatar
  • 2,279
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,279
7 votes
2 answers
303 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
9 votes
1 answer
923 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
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
6 votes
5 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
162 votes
10 answers
188k 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
4 votes
2 answers
1k views

Magento 2: good practice regarding custom module README.md

As you may have noticed, every Magento 2 core module contains a README.md file. Example for the Magento_Catalog module: Magento_Catalog module functionality is represented by the following sub-...
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
5 votes
2 answers
3k views

Use preconfigured builders (e.g. for SearchCriteria) in Magento 2

Warning, this is a long winded question about module architecture. I'm not mad if it gets closed as opinion based, but I hope someone has a better idea than me. In Magento 2 we have builders like the ...
Fabian Schmengler's user avatar
5 votes
1 answer
442 views

<module>...</module> vs <namespace_module>...</namespace_module> in config.xml Magento

I am new into Magento development, I have created some basic modules also with the help of available tutorials. However, I am not able to understand the difference between <module>...</module&...
Vaibhav Dass's user avatar
4 votes
2 answers
3k views

What is good practise for getting resource/collection models?

Which one is more correct: Mage::getResourceModel() or Mage::getModel('catalog/product')->getCollection() I always use the first method unless I already have a model that I can call the ...
Domeglic's user avatar
  • 542