Questions tagged [best-practice]

Denotes questions related to best-practices in Magento.

Filter by
Sorted by
Tagged with
0
votes
3answers
45 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
67 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
31 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
89 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
211 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?
0
votes
0answers
21 views

Magento 2 Theme development when HTMLs are provided

I need to integrate,develop theme from the HTMLs provided.The designer provided me with Homepage,Cart,Account page HTMLs etc.Need to either integrate with existing luma theme or create from scratch....
0
votes
0answers
35 views

How to save a custom entity with child entities related?

I have a question about saving a custom entity with some child entities related. In Magento 1, I used to set a child items collection inside the main entity model and after saving it, in '_afterSave()...
1
vote
0answers
40 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
33 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
35 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
250 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
70 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 ...
0
votes
0answers
38 views

M2: Should I make a generic “My Company” module?

My question is a process question. I want to use Magento 2 best practices and I haven't seen any guidance in this regard. A lot of what I read is "how" to do something and not whether it's a good idea....
2
votes
0answers
56 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
47 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 ...
20
votes
2answers
673 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
75 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
69 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
31 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
59 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
189 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
211 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 ...
5
votes
6answers
3k 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
37 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
239 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
29 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
124 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
67 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
77 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
62 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
44 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
42 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
506 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
198 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
1answer
394 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
385 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
515 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
52 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
985 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
85 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
177 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
268 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
907 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()) ...
0
votes
2answers
110 views

Differences in observer class naming?

When creating an observer you add your model to config.xml like ... <models> <some_name> <class>My_Module_Model</class> </some_name> </models> ...
5
votes
1answer
241 views

Magento 2 Testing Attributes

I've just started working on Magento 2. I created my first custom attribute for the customer entity and now i would like to create a test for it. As far as i understand it, this should be an ...
4
votes
2answers
2k 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 ...
3
votes
1answer
415 views

How to convert raw PHP Join Query into Magento Join Query

How to convert raw php LEFT JOIN query in to magento query? What is Magentos syntax for? Here is my query: SELECT fieldsmanager_orders.value,sales_flat_order_address.entity_id FROM ...
2
votes
1answer
41 views

Magento 2 custom templating/module in combination with composer

In Magento 2 you can install themes and extensions through composer and they end up in the vendor folder. What if I want to make customisation to that template/extension? Do I copy the code over to ...
2
votes
4answers
467 views

limit on getData( ) - Magento 1.9

Most of the time during the customization, we need at one time to use a getData() on an object exemple $_item = $this->getItem(); to retrieve the datas and see if we have what we are looking for, ...