119 votes
Accepted

What is the difference between type and virtualType

Virtual types are a way to inject different dependencies into existing classes without affecting other classes. For example, the Magento\Framework\Session\Storage class takes a $namespace argument in ...
user avatar
34 votes

What is the difference between type and virtualType

Another way to understand virtual types - Let's say that you have a class \Class1, which has the following constructor - public function __construct(\Class2 $argOfClass1){...} And \Class2 has the ...
user avatar
  • 551
29 votes

Creating custom module in magento2 show fatal error

try to delete your module from the var/generation/vendor-name/your-module and try to refresh the page and check if it's working.
user avatar
  • 1,222
28 votes

Is it possible to disable plugins defined in di.xml programatically?

In your di.xml you can do the following: <type name="\Magento\Catalog\Model\Product"> <plugin name="se_catalog_product_plugin_ample" disabled="true" /> </type> Magento does it ...
user avatar
24 votes
Accepted

Magento 2: practical explanation of what is a proxy class?

This particular usage is not a good example of using Proxy pattern. I think it is even useless in that particular piece of code, as a collection is not doing any DB operations unless load method is ...
user avatar
22 votes
Accepted

DI & Extending a Block on Magento 2

The class you are trying to extend has this constructor: public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\MediaStorage\Helper\File\Storage\...
user avatar
  • 196k
21 votes
Accepted

What Triggers the Generation of a Factory in Magento 2

Some interesting code location for how this all works together: https://github.com/magento/magento2/blob/develop/dev/tests/integration/testsuite/Magento/Framework/Code/GeneratorTest.php#L40 With the ...
user avatar
20 votes
Accepted

Magento2 Circular Depedency when reading config

Your error message is: Circular dependency: Magento\Store\Model\ResourceModel\Config\Collection\Scoped depends on Magento\Store\Model\ResourceModel\Config\Collection\Scoped and vice versa. To ...
user avatar
  • 4,446
19 votes
Accepted

why i should not use object manager directly in magento2?

There are several reasons. The code will work, but it is best practice to not reference the ObjectManager class directly. Because we say so! ;-) (better expressed as consistent code is good code) ...
user avatar
  • 3,458
19 votes
Accepted

Why can't I inject the ProductRepositoryInterface in Magento 2?

This generally crops up when you add another parameter to the constructor because Magento caches it in var/generation. You need to clear var/generation which forces Magento to regenerate the ...
user avatar
  • 3,210
19 votes

Incorrect dependency ScopeConfigInterface already exists in context object in compilation of magento2

Your error comes from the fact that you're injecting \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig in the constructor whereas this class is already part of the parent Magento\...
user avatar
18 votes

Magento 2: practical explanation of what is a proxy class?

A proxy class lets you dependency-inject a class that you won't necessarily need, and that has a high cost associated with doing so. If you look at a proxy Magento has generated, like \Magento\...
user avatar
17 votes

Magento 2: what is the $data array constructor parameter?

The $data[] can be used to hold values passed from di.xml's arguments replacement mechanism, when \Magento\Catalog\Block\Product\ListProduct intantiated. In your case this could be populated by <...
user avatar
17 votes

How to override Virtual Type class in Magento 2

You have to use: <preference for="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Vendor\Module\Block\Product\ListProductSearch" /> as Raphael mentioned. To make this singular ...
user avatar
16 votes
Accepted

Magento 2: use statement versus direct class path?

There is no technical reason to prefer one over the other, except if there are name conflicts (like different "Context" classes). But those can be solved with aliases and that's what I usually do: ...
user avatar
16 votes
Accepted

How do I add tracking number to current order shipment in Magento 2?

AFAIK the track object is the same in M2. However, the rest of the code has changed. $data = array( 'carrier_code' => 'ups', 'title' => 'United Parcel Service', 'number' => '...
user avatar
15 votes
Accepted

Why Object manager can not instantiate Non-injectable?

Doc should say "Object that should not be instantiated by object manager". Injectables are usually stateless services without identity (EventManager, CustomerAccountManagementService). They can be ...
user avatar
  • 4,446
15 votes
Accepted

Why some classes define injections in both their constructor and di.xml?

As stated in the documentation, in Magento 2, the di.xml can be used to do the following: You can configure the class constructor arguments in your di.xml in the argument node. The object manager ...
user avatar
15 votes
Accepted

Magento 2: How to get customer collection

1) Use \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory class to get customer collection <?php protected $_customerFactory; public function __construct( ... \Magento\...
user avatar
  • 21.7k
14 votes

Is dependency injection possible for plugins?

Late answer but you need to change __constructor to __construct and it will work perfectly well. Don't use static ObjectManager if possible. You should always use DI instead.
user avatar
  • 704
14 votes
Accepted

get cart/quote in Observer

I can't explain how it works. But Magento2 will autoload all classes passed specified in the __constructor. It will use reflection in some way. I mostly copied \Magento\Wishlist\Observer\AddToCart, ...
user avatar
  • 980
14 votes
Accepted

Magento 2: what is the $data array constructor parameter?

The $data can be used to populate data on your object since the constructor of \Magento\Framework\DataObject is this public function __construct(array $data = []) { $this->_data = $data; } or ...
user avatar
12 votes
Accepted

How to get ScopeConfigInterface through the object manager of unit tests in magento 2?

I may be wrong here, but I think for unit tests you don't have to retrieve values from the data base. You can assume that the implementations of \Magento\Framework\App\Config\ScopeConfigInterface are ...
user avatar
  • 196k
12 votes
Accepted

Magento 2 DI preference priority

All config files merged in order of module load. You can change order by add sequence in you module.xml files. For example <sequence> <module name="Magento_Search"/> <module ...
user avatar
  • 20.3k
11 votes

What is the difference between type and virtualType

In the same di.xml file I found that lessFileSourceBase is passed as an argument for lessFileSourceBaseFiltered that is passed as an argument for lessFileSourceBaseSorted that is passed as an argument ...
user avatar
  • 196k
11 votes
Accepted

Factory Class vs. Proxy Class in magento2

Proxy classes are intended to increase object creation speed. For example you want to create an object of the class where a lot of another classes are created in the constructor via dependency ...
user avatar
11 votes

How do I add tracking number to current order shipment in Magento 2?

Adding to the accepted answer, it would be a good idea to use repositories instead of the deprecated save method. Also added the customer notification after the tracking creation. /** @var Magento\...
user avatar
11 votes
Accepted

Product grid says "The resource isn't set" when adding __construct to model class

I wasn't in accord with the parent class' constructor. The surprising thing is magento setup:di:compile yielded no error. Anyway, here's the solution. public function __construct( \Magento\...
user avatar
10 votes

Creating custom module in magento2 show fatal error

You can use below mentioned command and then refresh the page. php bin/magento setup:upgrade
user avatar
10 votes

What Triggers the Generation of a Factory in Magento 2

I am digging in this same pea soup right now. My understanding so far is that all of the stuff that gets auto generated at /var/generation is done from the preferences and interfaces declared in app/...
user avatar
  • 672

Only top scored, non community-wiki answers of a minimum length are eligible