Questions tagged [best-practice]

Denotes questions related to best-practices in Magento.

Filter by
Sorted by
Tagged with
0
votes
0answers
13 views

Pass Variable to Child Block

Once again, a best practice question I have. I need to pass some variable to a child block in a template. In particular, the child block is a Default Rendered block I specified in a custom RenderList ...
0
votes
1answer
18 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 ...
0
votes
2answers
36 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 ...
1
vote
1answer
19 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
1answer
136 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
0answers
23 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 ...
1
vote
0answers
28 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 ...
1
vote
1answer
286 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" &...
0
votes
1answer
60 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 ...
0
votes
3answers
96 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: ...
0
votes
2answers
306 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 ...
2
votes
1answer
38 views

Best Practice Magento 2 / Composer / Custom Module

Can you tell me what are the best practice when you create a custom module for a project. I would say to put the custom module related to the project in app/code/MyNamespace/MyModule and all Generic ...
2
votes
1answer
264 views

How to load current product in Magento 2 without using deprecated functionality?

The core uses two deprecated classes to load the current product within the block class on the front end: \Magento\Catalog\Block\Product\AbstractProduct \Magento\Framework\Registry See: \Magento\...
2
votes
2answers
951 views

Magento 2 - Override a helper using plugin

What is the best practice to override a helper? is it through Preference or Plugin? And how can I override a helper using Plugin?
1
vote
0answers
64 views

Headless Magento

Please let me know the best practices to be followed while creating an e-commerce solution which uses the headless approach. Whether Magento or frontend-side need to implement custom business logic ...
0
votes
0answers
36 views

What is the best way to save a custom field in shipping address?

I have added a custom field on checkout in shipping address section using below code. etc/frontend/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
2
votes
1answer
42 views

Magento 2: best practice building classes

I need some advice how to structure my code in magento 2. I am working on an ERP interface to import and export products, customers and orders. So I need some helper classes to take care of some ...
2
votes
1answer
916 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
1answer
194 views

What is the best method to retrieve a SINGLE ITEM order details in Magento2?

I am currently using the \Magento\Sales\Model\OrderFactory $_orderFactory and loadByIncrementId($id) in my custom block and calling it in my .phtml file. I get basic details of the order, but I ...
2
votes
0answers
91 views

M2: getBaseUrl() - StoreManagerInterface vs StoreConfigInterface

If someone asks for getting base URL, the answer is always to use StoreManagerInterface, like ... protected $storeManager; public function __construct( ... \Magento\Store\Model\...
2
votes
1answer
48 views

How to replace the production website with the development one?

I've been let down with a developer going AWOL half-way through a project and have had to learn as I go to get our new site sorted. The current site is : ourdomain.co.uk/ (not magento) We have the ...
21
votes
2answers
1k 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 (...
1
vote
0answers
94 views

Outdated codesniffer in MEQP2 composer.json for Magento 2.2?

How to correctly test against MEQP2 standard? Normally i use vendor/bin/phpcs --standard=PSR2 To supress errors a i use this: //phpcs:ignore or //phpcs:disable PSR2 When using MEQP2 standards ...
1
vote
1answer
111 views

custom extensions - are hyphens allowed in the Extension name?

This is a follow up on the Magento 2 Extension naming convention question here. I've learned thus far that you need a Vendor Name an Extension (or Module) name a combination of both like ...
0
votes
1answer
36 views

M1/M2 | Extensions - best practices for naming, versioning, author

was wondering what is the best practice for ... naming an extentsion? where to put the copyright holders /authors name? versioning (major.minor.patch maybe) of extensions I guess there must be ...
0
votes
1answer
69 views

Magento 1.9 Database transfer

We are trying to create our product with a clean version of Magento. But we need to transfer our database. Based on our research we have a couple of choices. Transfer important tables as Mysql dump. ...
3
votes
2answers
234 views

Best practice Magento dev environment, how do teams develop Magento? [closed]

I'm trying to set up a dev environment for my team to work together on 1 Magento project. But I don't know if I should use a shared server with a Magento installation or if we should all install ...
0
votes
1answer
243 views

DI Compile fails with “Area code not set” error

I'm trying to use the \Magento\Sales\Model\InvoiceOrder class to invoice some orders. If I include the class in my constructor, however, then I get the "Area code not set: Area code must be set before ...
7
votes
6answers
5k 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 ...
0
votes
2answers
40 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/...
2
votes
1answer
366 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 ...
0
votes
0answers
30 views

CSS Best Practices - single time use of CSS

I've made a module with a custom page. On this page I have a list of around 20 checkboxes, each followed by a single word for identification. I would like to display this with CSS in multiple columns. ...
0
votes
3answers
207 views

How to create a search bar that fixes or prevents spelling mistakes?

So my customers tend to be more so individuals who are not computer literate and often make errors when searching for our products. What is the best way to work out what they're trying to search for ...
0
votes
1answer
70 views

How to get names, IDs and details of my categories without falling in to bad practice?

It's been commonly accepted you should never use \Magento\Framework\App\ObjectManager::getInstance() I can link sources explaining why if needed. Given this how would I go about getting the names of ...
1
vote
1answer
103 views

Testing/Development Environment on local server

I need to create a local development environment and struggling a little with the setup. I want to replicate my live server as close as I possibly can. I hope I'm posting this question in a right ...
0
votes
2answers
86 views

Magento 2 : How to write a custom extension? [closed]

There is already a question on how to write a custom extension at How to write a custom extension? , but most of the answers I read , while being good and really detailed are about magento 1, so felt ...
2
votes
1answer
47 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*/ $...
2
votes
0answers
51 views

Magento 2: Best practice create model class

First thing, I have a question on this topic Optimize load model memory Then I have some questions for the best way to create a Model to load or save data to Database? Could you please explain which ...
9
votes
2answers
676 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 ...
2
votes
2answers
242 views

Is calling Mage::app() in a template file good or bad practice?

I want to get the store code in a phtml file. I know I can get it with the below. Mage::app()->getStore()->getCode() But is this good or bad practice? My guess is that it is not as Mage::...
2
votes
2answers
526 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 ...
0
votes
1answer
429 views

Add Category tree, if not exists programmatically

Ive been through several answers before succumbing to ask a question. I will list what i tried and why i think this is a bit of a special case, and not a duplicate. Am reading a .csv file from my ...
0
votes
1answer
629 views

Magento2 different between Object Manager get and create method [duplicate]

In magento2, we have use object manager: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); and use get() and create() method $cookieManager = $objectManager->get('{...
0
votes
1answer
59 views

How to modify delete method using objectManager

I have found the some extension from Amasty and here is the snipped code to delete the data using objectManager : public function deleteBy($methodId) { /** * @var \Amasty\ShippingTableRates\...
4
votes
1answer
42 views

Porting 2.1 extensions to 2.0

As I'm fairly new to the Magento ecosystem, I'm wondering what the best practice is for writing extensions. Is it better to write an extension for each major release (2.0, 2.1, 2.2), or can one ...
0
votes
2answers
1k views

How to sort product collection by sales in Magento 2

I need to sort products on catalog page by ordered quantity. What is the best way to do it? I know that table sales_order_item contains this data, but I think direct SQL queries is bad practice (...
3
votes
1answer
89 views

Right syntax for the foreign key in InstallScript

When I adding a foreign key, I write something like: ->addForeignKey( $installer->getFkName('my_table_name', 'website_id', 'store_website', 'website_id'), 'website_id', '...
1
vote
1answer
220 views

Difference between “Mage_Core_Exception $e” and “Exception $e”

I found different cases to use Magento throw exception, I want to know what is the difference between (Mage_Core_Exception $e) and (Exception $e) ? I found What's the preferred way of throwing ...
3
votes
1answer
378 views

What is the difference between use Magento\… under namespace and __construct(Magento\…)? [duplicate]

As I'm writing a new module, this question just raise out my curiousity. Let's take Magento\Customer\Model\Customer as an example, the below codes can perform the same function actually: Using use ...
3
votes
1answer
1k views

Magento2: What is the best practice to get collection

I know it is not a good practice to use objectManager to get collection. However, I don't know what is the advantage of using ModelFactory ($this->mymodelFactory->create()->getCollection()) ...