Questions tagged [best-practice]
Denotes questions related to best-practices in Magento.
208
questions
2
votes
0
answers
84
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
374
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
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 ...
0
votes
1
answer
625
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
974
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
62
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
1
answer
46
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
2
answers
2k
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
1
answer
109
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
1
answer
270
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
1
answer
648
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
1
answer
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()) ...
0
votes
2
answers
122
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
1
answer
563
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 ...
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 ...
3
votes
1
answer
559
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
1
answer
47
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
4
answers
966
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, ...
3
votes
1
answer
1k
views
Magento 2 - Best practice to modify third party extension
I need to modify a template from a third party extension to match my current theme. In the extension layout xml file
app\code\IWD\Opc\view\frontend\layout\checkout_index_index.xml
<action ...
1
vote
0
answers
28
views
Magento1 order extra steps for custom items
We have a Magento1 store that sells shirts. The customers can upload a logo, or send some text that we will print on the shirt for them.
Currently, for each order that needs a print job (as opposed ...
2
votes
1
answer
269
views
Why does my module getting warrning "Mysql4 classes are obsolete "
Hello i have create custom module and getting warning Mysql4 classes are obsolete
class Mymodule_Custom_Model_Mysql4_Stores extends Mage_Core_Model_Mysql4_Abstract
{
public function ...
1
vote
0
answers
112
views
Group by products by attribute in minicart - Magento1.9
I want to know how to group by the products of a minicart by vendor_id attribute, evidently each product has this attribute vendor_id. Then I display at the header of each group the name of the vendor,...
10
votes
3
answers
2k
views
ECG compliance: How to do?
There is something like a coding standard for Magento 1, called ECG where numerous things should be avoided or methods that should not be used.
Let's try to create a list that covers all cases that ...
0
votes
3
answers
408
views
setChild doesn't work - Magento 1.9
I want to force some block to be a child, so setchild() doesn't work !
<catalog_product_view>
<reference name="root">
<block type="type/block" name="grid" as="grid" ...
1
vote
1
answer
173
views
What stopObserving() function is used for - Magento 1.9
I want to know what the js function stopObserving() is used for.
1
vote
1
answer
725
views
How to use template methods fetchView() and renderView()?
I want to know how these two methods from the "core/template" block work and in wich case we need to use them, which we practically don't use often.
This is the code from app/code/core/Mage/Core/...
1
vote
1
answer
1k
views
Remove and add block in layout explanation - Magento 1.9
I had fun to create some small scenario in layout and I don't understand some thing.
In the layout, would it be possible to remove a block and then create it again ?
I explain: assuming that I want ...
1
vote
1
answer
202
views
Question about the Merge layout.xml - Magento 1.9
Question: If a layout handle exists in the "base/default" package, your custom design package, and the custom package's
"local.xml", wich handles can be merged ?
1) All of the handles can be merged....
0
votes
2
answers
150
views
What are the reflects of core files changed? [closed]
I'm using different versions of the Magento 1.* and Magento 2.*, and I'd like to know how to explain to my clients what are the impacts of the core files changed.
For example these bad points below, ...
0
votes
2
answers
45
views
What's the right way to build a theme in Magento 2?
I hope this isn't a question too subjective a question for Stack Exchange. If so, feel free to correct me.
I've recently moved on from Magento 1.9 to Magento 2.1. Right now I'm trying to build my own ...
0
votes
1
answer
217
views
Best way to start making Magento 2 themes
I have done the research about it and found many solutions I tried some of them and came up with results successfully.
In my study, I have found that Magento 2 comes with its built-in CSS library and ...
3
votes
1
answer
470
views
Module development workflow with GIT and modman
This question is a follow up of this one: Magento 1: improving my module development workflow (Modman, composer, git)
Reminder:
I am using a bunch of extensions that me and the team I'm working ...
4
votes
2
answers
603
views
Approach to third party module customisation
I am trying to customize a third party module and I was wondering what is the general approach to doing so? (I'm assuming that I am not the only one who needs to modify a third party extension)
...
1
vote
0
answers
46
views
Is extending stock knockout objects a good practice in Magento 2?
I'm writing an extra step in the M2 checkout. There are already written well structured models and actions in Magento_Checkout/js. Should I extend them (for instance js/model/quote or js/model/...
3
votes
3
answers
1k
views
Adminhtml - white grid - Magento 1.9
I'm creating some menu admin and i have a container grid with buttons, but not a grid, logically this means that I don't load my block but I dont find the issue, i tried to get the class of a block ...
4
votes
2
answers
1k
views
Dependency Injection and constructor parameters
Lets suppose I have extended a class from magento core and used some extra classes to have a specific functionality. Here is the code (just example).
// core code
public function __construct(
\...
1
vote
2
answers
543
views
Use or not local.xml instead module layout.xml
Is it recommended and necessary to use local.xml for layout updates instead the module layout.xml, does not create redundancy and overload in code ?
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.
...
3
votes
1
answer
1k
views
Put CDATA It is a good practice or not necessarily - Magento 1.9
<script type="text/javascript">
//<![CDATA[
Some code...
//]]>
</script>
We agree that magento use the CDATA in javascript pieces codes, but I would like to know which is ...
4
votes
2
answers
682
views
Magento 2 - what functions can I use?
Recently I've went through the "Fundamentals of Magento 2 development" course and there was such statement that we SHOULD NOT use functions from other modules that are not defined in the API ...
55
votes
3
answers
18k
views
Magento 2 as a headless solution
I want to know if there are some best practices to use Magento 2 as a headless E-commerce solution.
A typical E-commerce in 2017 is to have an omni-channel solution which includes
E-commerce
CMS
...
1
vote
2
answers
3k
views
Magento 2 : What's the best way to add custom html to a product block in the product list?
I thought this would be better in Magento 2, but currently (Magento 2.1.4) Magento still has a quote complex way of rendering a product list. I'm talking about vendor/magento/module-catalog/view/...
1
vote
2
answers
431
views
Events, is there a predefined list of events or not
I want to know if there is a predefined list of events like this one https://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ or not because most of the events are dynamically named ?
15
votes
4
answers
3k
views
Magento 2 best practice for class locations and names
In Magento 1 we were used to place our classes in these directories
Block
Helper
Model
Resource
and use a simple class name without any capital letters in the middle of the name.
...
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 ...
2
votes
0
answers
345
views
Magento 2.1.3 : What's the best way to manipulate a products' price at run-time?
I have the following task:
"Whenever a client is in a certain customer group, he needs to see a different price for the product in question."
Now, we all know we need to use tier prices for this (...
1
vote
2
answers
151
views
Where to put generic functions?
I have a collection of generic functions in my Magento project. Right now they're in single PHP file, and it's being called with a require_once in each phtml that needs it.
I don't like it and I feel ...
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\...
2
votes
1
answer
341
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 ...