{"id":379306,"date":"2023-05-02T11:29:23","date_gmt":"2023-05-02T11:29:23","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=379306"},"modified":"2024-07-19T13:19:07","modified_gmt":"2024-07-19T13:19:07","slug":"virtual-types-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/","title":{"rendered":"Virtual Types in Magento 2"},"content":{"rendered":"\n<p>Hello Friends, <br>In this blog, we will learn about the Virtual Types concept in Magento 2.<\/p>\n\n\n\n<p>Along side, you can also have a look at our\u00a0<a href=\"https:\/\/store.webkul.com\/Magento-2.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 extensions<\/a>\u00a0already crafted by certified Adobe Commerce Developers.<\/p>\n\n\n\n<p><strong>What are Virtual Types in Magento 2?<\/strong><\/p>\n\n\n\n<p>In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file(it isn&#8217;t necessary.<\/p>\n\n\n\n<p>But if we want then we can create it as well), we can use inject these Virtual Types where we need them.<\/p>\n\n\n\n<p>In Magento 2, Virtual Types are like a sub-class for an existing class.<\/p>\n\n\n\n<p>Moreover, there is no need to seek elsewhere; avail yourself of the chance to commence your projects with an endorsed <a href=\"https:\/\/webkul.com\/magento-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 development company<\/a>.<\/p>\n\n\n\n<p><strong>How to use Virtual Types?<\/strong><\/p>\n\n\n\n<p>In Magento 2, we can mention Virtual Types in the di.xml file inside the<em> &lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/etc\/<\/em> directory OR <em>\/vendor\/VendorName\/src\/module-name\/etc\/ <\/em>directory <\/p>\n\n\n\n<p>OR<\/p>\n\n\n\n<p><em>&lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/etc\/&lt;frontend Or adminhtml&gt;<\/em> directory OR <em>\/vendor\/VendorName\/src\/module-name\/etc\/&lt;frontend Or adminhtml&gt; <\/em>directory.<\/p>\n\n\n\n<p>In the di.xml file, we use the tag to add our custom Virtual Type class.<br>&lt;virtualType\/&gt; accepts two attributes:<\/p>\n\n\n\n<p>[A] <strong>name: <\/strong>In this attribute, we set virtualType&#8217;s name as value.<br>[B] <strong>type:<\/strong> This attribute contains the existing class name which we use to create the virtualType.<\/p>\n\n\n\n<p>For better understanding, here I have created a di.xml file and added a Virtual Type class using the &lt;virtualType\/&gt; tag.<br>The di.xml file is placed inside the &lt;magento-root-directory&gt;\/app\/code\/Webkul\/Module\/etc\/ directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\n--&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;!--Employee Class--&gt;\n    &lt;type name=&quot;Webkul\\Module\\Model\\Employee&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;empattribute&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;name&quot; xsi:type=&quot;string&quot;&gt;Name&lt;\/item&gt;\n                &lt;item name=&quot;id&quot; xsi:type=&quot;string&quot;&gt;Id&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/type&gt;\n\n    &lt;!--CustomVirtualEmployee virtualType class for Employee class--&gt;\n    &lt;virtualType name=&quot;CustomVirtualEmployee&quot; type=&quot;Webkul\\Module\\Model\\Employee&quot;&gt;\n    &lt;\/virtualType&gt;\n\n    &lt;!--FirstEmployee virtualType class for EmpRecord class--&gt;\n    &lt;virtualType name=&quot;FirstEmployee&quot; type=&quot;Webkul\\Module\\Model\\EmpRecord&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;record&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;name_1&quot; xsi:type=&quot;string&quot;&gt;Jack&lt;\/item&gt;\n                &lt;item name=&quot;id_1&quot; xsi:type=&quot;string&quot;&gt;101&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/virtualType&gt;\n\n    &lt;!--SecondEmployee virtualType class for EmpRecord class--&gt;\n    &lt;virtualType name=&quot;SecondEmployee&quot; type=&quot;Webkul\\Module\\Model\\EmpRecord&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;record&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;name_2&quot; xsi:type=&quot;string&quot;&gt;Lusi&lt;\/item&gt;\n                &lt;item name=&quot;id_1&quot; xsi:type=&quot;string&quot;&gt;140&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/virtualType&gt;\n\n    &lt;!--using virtualType classes FirstEmployee and SecondEmployee within di.xml file--&gt;\n    &lt;type name=&quot;Webkul\\Module\\Model\\AllRecords&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;allRecords&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;firstEmployee&quot; xsi:type=&quot;object&quot;&gt;FirstEmployee&lt;\/item&gt;\n                &lt;item name=&quot;secondEmployee&quot; xsi:type=&quot;object&quot;&gt;SecondEmployee&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>In the above di.xml, I have used Virtual Types in two ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Mentioned the <strong>FirstEmployee<\/strong> and <strong>SecondEmployee<\/strong> classes, these classes don&#8217;t exist in the Module but they will work virtually. <br><br>If we use this class in code then it will not work. We can use these virtualType classes within the di.xml.<\/li>\n\n\n\n<li>Mentioned the <strong>CustomVirtualEmployee<\/strong> class and also created this PHP class file in the module.<br><br>If we will create the class, then we can do some other stuff like modifying methods or simply using the class inside PHP code.<\/li>\n<\/ol>\n\n\n\n<p>Here, I have created the <strong>Employee.php <\/strong>class file<strong> <\/strong>placed inside the <em>&lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/Model\/<\/em> directory. I have created <strong>CustomVirtualEmployee<\/strong> virtualType for the <strong>Employee<\/strong> class.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Module\\Model;\n\nclass Employee\n{\n    \/**\n     * @var array\n     *\/\n    private $empattribute;\n    \n    \/**\n     * Construct method\n     * \n     * @param array $empattribute\n     * @return void\n     *\/\n    public function __construct(array $empattribute = &#091;])\n    {\n        $this-&gt;empattribute = $empattribute;\n    }\n\n    \/**\n     * Get Employee Data\n     * \n     * @return array\n     *\/\n    public function getEmployeeData()\n    {\n        return $this-&gt;empattribute;\n    }\n}<\/pre>\n\n\n\n<p>Now, I have created the <strong>CustomVirtualEmployee.php <\/strong>class file<strong> <\/strong>placed inside the <em>&lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/Model\/<\/em> directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Module\\Model;\n\nclass CustomVirtualEmployee extends \\Webkul\\Module\\Model\\Employee\n{\n    \/**\n     * @var array\n     *\/\n    private $empattribute;\n    \n    \/**\n     * Construct method\n     * \n     * @param array $empattribute\n     * @return void\n     *\/\n    public function __construct(array $empattribute = &#091;])\n    {\n        parent::__construct($empattribute);\n        $this-&gt;empattribute = $this-&gt;getEmployeeData();\n        $this-&gt;empattribute&#091;&quot;addedNewOne&quot;] = $this-&gt;getNewAttribute();\n    }\n\n    \/**\n     * Get New Attribute Label\n     * \n     * @return string\n     *\/\n    public function getNewAttribute()\n    {\n        return &quot;Salary&quot;;\n    }\n\n    \/**\n     * Get All Data\n     * \n     * @return array\n     *\/\n    public function getAllData()\n    {\n        return $this-&gt;empattribute;\n    }\n}<\/pre>\n\n\n\n<p>Now, I have created the <strong>EmpRecord.php <\/strong>class file<strong> <\/strong>placed inside the <em>&lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/Model\/<\/em> directory. For this class, I have created two virtualTypes <strong>FirstEmployee<\/strong> and <strong>SecondEmployee<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Module\\Model;\n\nclass EmpRecord\n{\n    \/**\n     * @var array\n     *\/\n    private $record;\n    \n    \/**\n     * Construct method\n     * \n     * @param array $record\n     * @return void\n     *\/\n    public function __construct(array $record = &#091;])\n    {\n        $this-&gt;record = $record;    \n    }\n\n    \/**\n     * Get Record Data\n     * \n     * @return array\n     *\/\n    public function getRecord()\n    {\n        return $this-&gt;record;\n    }\n}<\/pre>\n\n\n\n<p>Now, I have created the<strong> AllRecords.php <\/strong>class file<strong> <\/strong>placed inside the <em>&lt;magento-root-dir.&gt;\/app\/code\/Vendor\/Module\/Model\/<\/em> directory. In this class, I have used virtualTypes <strong>FirstEmployee<\/strong> and <strong>SecondEmployee<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Module\\Model;\n\nclass AllRecords\n{\n    \/**\n     * @var array\n     *\/\n    private $allRecords;\n\n    \/**\n     * Construct method\n     * \n     * @param array $allRecords\n     * @return void\n     *\/\n    public function __construct(array $allRecords = &#091;])\n    {\n        $this-&gt;allRecords = $allRecords;\n    }\n\n    \/**\n     * Get All Records\n     * \n     * @return array\n     *\/\n    public function getRecords()\n    {\n        return $this-&gt;allRecords;\n    }\n}<\/pre>\n\n\n\n<p>After adding the virtualType in your code, please execute the following command to see the result.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:di:compile<\/pre>\n\n\n\n<p><strong>See the Result:<\/strong><br>To display the result, I created the Controller file Index.php inside the &lt;magento-root-dir.&gt;app\/code\/Webkul\/Module\/Controller\/Index\/ directory and added the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_Module\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\n\nnamespace Webkul\\Module\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\n\nclass Index extends Action\n{\n    \/**\n     * @var \\Magento\\Framework\\Controller\\Result\\Raw\n     *\/\n    protected $rawResultFactory;\n\n    \/**\n     * @var \\Webkul\\Module\\Model\\AllRecords\n     *\/\n    protected $empAllRecords;\n\n    \/**\n     * @var \\Webkul\\Module\\Model\\CustomVirtualEmployee\n     *\/\n    protected $virEmp;\n\n    \/**\n     * Initialize dependencies\n     * \n     * @param Context $context\n     * @param \\Webkul\\Module\\Model\\AllRecords $empAllRecords\n     * @param \\Webkul\\Module\\Model\\CustomVirtualEmployee $virEmp\n     * @param \\Magento\\Framework\\Controller\\Result\\Raw $rawResultFactory\n     * @return void\n     *\/\n    public function __construct(\n        Context $context,\n        \\Webkul\\Module\\Model\\AllRecords $empAllRecords,\n        \\Webkul\\Module\\Model\\CustomVirtualEmployee $virEmp,\n        \\Magento\\Framework\\Controller\\Result\\Raw $rawResultFactory\n    ) {\n        $this-&gt;virEmp   = $virEmp;\n        $this-&gt;rawResultFactory = $rawResultFactory;\n        $this-&gt;empAllRecords = $empAllRecords;\n        parent::__construct($context);\n    }\n\n    \/**\n     * Execute method to print data\n     *\n     * @return \\Magento\\Framework\\Controller\\Result\\Raw\n     *\/\n    public function execute()\n    {\n        $result = $this-&gt;rawResultFactory;\n        $result-&gt;setHeader(&#039;Content-Type&#039;, &#039;text\/html&#039;);\n\n        $empRecords = $this-&gt;empAllRecords-&gt;getRecords();\n\n        $dataStr = &quot;&quot;;\n\n        $dataStr .= &quot;&lt;h3&gt;Result for Case 1: Used &amp;lt;virtualType&amp;gt; within di.xml file&lt;\/h3&gt;&quot;;\n\n        foreach ($empRecords as $index=&gt;$each) {\n            $dataStr .= $index.&quot;::&quot;.json_encode($each-&gt;getRecord()).&quot;&lt;br&gt;&quot;;\n        }\n\n        $employeeAttrs = $this-&gt;virEmp-&gt;getAllData();\n    \n        $dataStr .= &quot;&lt;h3&gt;Result for Case 2: Created &amp;lt;virtualType&amp;gt; for class Webkul\\Module\\Model\\Employee and created CustomVirtualEmployee class&lt;\/h3&gt;&quot;;\n\n        foreach ($employeeAttrs as $key=&gt;$value) {\n            $dataStr .= $key.&quot;::&quot;.$value.&quot;&lt;br&gt;&quot;;\n        }\n\n        $result-&gt;setContents( $dataStr);\n\n        return $result;\n    }\n}<\/pre>\n\n\n\n<p>When you will run the controller on browser, you will see the result as below image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"329\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png\" alt=\"VirtualTypeResult\" class=\"wp-image-379373\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-300x82.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-250x68.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-768x210.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult.png 1234w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>Virtual types can be used to modify or replace the constructor argument of an existing class.<\/p>\n\n\n\n<p><br>In &lt;virtualType\/&gt;, in the <strong>name<\/strong> attribute, we can pass the ClassName or complete path of the virtual class as the following example in <em>&lt;magento-root-dir.&gt;\/vendor\/magento\/module-catalog\/adminhtml\/etc\/di.xml<\/em> file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;virtualType name=&quot;Magento\\Catalog\\Ui\\DataProvider\\Product\\ProductCollectionFactory&quot; type=&quot;Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory&quot;&gt;\n    &lt;arguments&gt;\n        &lt;argument name=&quot;instanceName&quot; xsi:type=&quot;string&quot;&gt;\\Magento\\Catalog\\Ui\\DataProvider\\Product\\ProductCollection&lt;\/argument&gt;\n    &lt;\/arguments&gt;\n&lt;\/virtualType&gt;<\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/khushboowebkul\/virtualTypesDemoMagento2\" target=\"_blank\" rel=\"noreferrer noopener\">Download the Full Code.<\/a><\/p>\n\n\n\n<p>For a personalized touch, engage and <a href=\"https:\/\/webkul.com\/hire-magento-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">hire Magento developers<\/a> who are focused on delivering exceptional results for your custom e-commerce projects.<br><br>Hope this will be helpful. Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Friends, In this blog, we will learn about the Virtual Types concept in Magento 2. Along side, you can also have a look at our\u00a0Magento 2 extensions\u00a0already crafted by certified Adobe Commerce Developers. What are Virtual Types in Magento 2? In Magento 2, Virtual Types allows us to modify existing classes without affecting the <a href=\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,302],"tags":[14118,14119],"class_list":["post-379306","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-magento2","tag-virtual-types-in-magento-2","tag-virtualtype"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Virtual Types in Magento 2 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Virtual Types in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-02T11:29:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-19T13:19:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Virtual Types in Magento 2\",\"datePublished\":\"2023-05-02T11:29:23+00:00\",\"dateModified\":\"2024-07-19T13:19:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\"},\"wordCount\":630,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png\",\"keywords\":[\"virtual types in magento 2\",\"virtualType\"],\"articleSection\":[\"Magento 2\",\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\",\"name\":\"Virtual Types in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png\",\"datePublished\":\"2023-05-02T11:29:23+00:00\",\"dateModified\":\"2024-07-19T13:19:07+00:00\",\"description\":\"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult.png\",\"width\":1234,\"height\":338,\"caption\":\"VirtualTypeResult\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Virtual Types in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Virtual Types in Magento 2 - Webkul Blog","description":"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Virtual Types in Magento 2 - Webkul Blog","og_description":"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file","og_url":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-05-02T11:29:23+00:00","article_modified_time":"2024-07-19T13:19:07+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png","type":"","width":"","height":""}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Virtual Types in Magento 2","datePublished":"2023-05-02T11:29:23+00:00","dateModified":"2024-07-19T13:19:07+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/"},"wordCount":630,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png","keywords":["virtual types in magento 2","virtualType"],"articleSection":["Magento 2","Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/","name":"Virtual Types in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult-1200x329.png","datePublished":"2023-05-02T11:29:23+00:00","dateModified":"2024-07-19T13:19:07+00:00","description":"Virtual Types in Magento 2: In Magento 2, Virtual Types allows us to modify existing classes without affecting the other classes and without having to create a new class file","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/05\/VirtualTypeResult.png","width":1234,"height":338,"caption":"VirtualTypeResult"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/virtual-types-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Virtual Types in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/379306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=379306"}],"version-history":[{"count":15,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/379306\/revisions"}],"predecessor-version":[{"id":453748,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/379306\/revisions\/453748"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=379306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=379306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=379306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}