Magento 2 Virtual Type

Using VirtualType in Magento 2 is really necessary when you are running an virtual store based on Magento 2 platform? On Magento 2 platform, the di.xml supports for two types of node which are a node type and a node virtualtype, meanwhile virtualtype is considered as a perfect method instead of the type. The virtual tape allows inserting different dependencies into the existing classes but not making any change to other classes. With this tutorial, Mageplaza team will lead you to learn how to create and use VirtualType in Magento 2.

Creating VirtualType in Magento 2

Creating virtualtype in Magneto 2 means creating a sub-class for an existing class. All are available to help you create the virtualtype in magento 2.

<?php
class OurVirtualTypeName extends \Mageplaza\HelloWorld\Model\Virtualtype
{
}

The following script code is inserted to the module’s di.xml to create a virtualtype in Magento 2.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml
<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">  
    </virtualType>        
</config>

Namely, the nodes are placed under the main node and include two attributes: name and type. While the name attribute is mostly the universally special name of that node, the type attribute is the real PHP for the virtual type.

As you see, it is simple to give some descriptions about the virtual type. If you erase the cache and repeat the request, the output is still the same.

$ php bin/magento hw:tutorial-virtual-type
First, we'll report on the Mageplaza\HelloWorld\Model\Example object
The Property $property_of_example_object
  is an object
  created with the class: 
  Mageplaza\HelloWorld\Model\Virtualtype

Next, we're going to report on the Example object's one property (an Virtualtype class)
The Property $property_of_argument1_object
  is an object
  created with the class: 
  Mageplaza\HelloWorld\Model\Argument2

Finally, we'll report on an Virtualtype object, instantiated separate from Example
The Property $property_of_argument1_object
  is an object
  created with the class: 
  Mageplaza\HelloWorld\Model\Argument2

Using Virtual Type in Magento 2

The functionality of a virtual type is replacing the position of PHP classes and you wouldn’t have to use a configuration as the below if you wanted to custom the argument injected into the Example class’s constructor.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml    
<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">  
    </virtualType>        

    <type name="Mageplaza\HelloWorld\Model\Example">
        <arguments>
            <argument name="the_object" xsi:type="object">Some\Other\Class</argument>
        </arguments>
    </type>        

</config>

However, applying that command will lead you to the following error even though you have already cleaned your cache.

$ php bin/magento hw:tutorial-virtual-type

  [ReflectionException]                  
  Class Some\Other\Class does not exist 

Let’s move to a smart way with the virtual type. This means Some\Other\Class is recovered by ourVirtualTypeName. You can ensure that no error is caused unless you call the command with the above in place.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml      
<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">  
    </virtualType>        

    <type name="Mageplaza\HelloWorld\Model\Example">
        <arguments>
            <argument name="the_object" xsi:type="object">ourVirtualTypeName</argument>
        </arguments>
    </type>        

</config>

Thanks for your reading and I hope that your business become more perfect with our guides of creating and using virtual types.



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
use
virtualtype

Comments for Magento 2 Virtual Type

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 use virtualtype
  • magento 2 how to use virtualtype
  • Magento 2 Virtual Type
  • magento 2 virtual product type
  • magento 2 virtual type
  • virtual product type in magento 2
  • magento 2 change product type to virtual