Magento 2 Virtual Type

Using VirtualType in Magento 2 is really necessary when you are running a virtual store based on Magento 2 platform? On Magento 2 platform, the di.xml supports two types of nodes which are a node type and a node virtualtype, meanwhile virtualtype is considered as a perfect method instead of the type. Virtual types are similar to the types but provides additional flexibilty as they allow defining a virtual class.

There are two things you need to know to differentiate virtual types and types. For virtual types:

  • The type should becomes a class that virtual types can based on.
  • The argument name should coordinate with the type class constructor same as for regular type declaration.

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 Magento 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.

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

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
  • 2.3.x, 2.4.x
x