Questions tagged [best-practice]
Denotes questions related to best-practices in Magento.
202
questions
0
votes
1
answer
35
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
23
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" => "...
2
votes
0
answers
142
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
36
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.
1
vote
0
answers
18
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?
4
votes
3
answers
173
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 ...
0
votes
1
answer
252
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>/...
0
votes
1
answer
33
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
640
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
1
answer
286
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
955
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
30
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
0
answers
29
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
2
answers
1k
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
1
answer
256
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
3
answers
406
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
2
answers
973
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
1
answer
48
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
1
answer
578
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\...
3
votes
2
answers
2k
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
0
answers
79
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
0
answers
44
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
1
answer
86
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
1
answer
2k
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
625
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
0
answers
209
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
1
answer
57
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
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 (...
1
vote
0
answers
118
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
1
answer
252
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
1
answer
46
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
1
answer
85
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
2
answers
314
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
1
answer
370
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
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 ...
0
votes
3
answers
65
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/...
3
votes
1
answer
583
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
0
answers
31
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
3
answers
465
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
1
answer
72
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
1
answer
142
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
2
answers
104
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 ...
3
votes
1
answer
80
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
0
answers
60
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
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 ...
2
votes
2
answers
342
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
2
answers
1k
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
1
answer
518
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
1
answer
814
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
1
answer
60
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\...