Magento Tutorials

How to Create Virtual Product Programmatically in Magento 2

Hello Magento Friends,

Welcome to Magento 2 Tutorial series for creating the product programmatically. This is the comprehensive guide on How to Create Virtual Product Programmatically in Magento 2.

To handle a wide range of product catalogs, Magento 2 supports different product types.

Mainly we can create 6 types of products Programmatically in Magento 2:

Virtual products are abstract products that do not have a physical presence. Subscriptions, membership, services, warranties are the types of virtual products. Virtual products do not involve shipment and delivery. 

Here, is the full-fledged solution to Create Virtual Product Programmatically in Magento 2. 

Steps to Create Virtual Product Programmatically in Magento 2:

Step 1: Create a virtual_product.php file in the Magento root directory and add the below code

<?php

use Magento\Framework\AppInterface;

try {

    require_once __DIR__ . '/app/bootstrap.php';

} catch (\Exception $e) {

    echo 'Autoload error: ' . $e->getMessage();

    exit(1);

}

try {

    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();

    $appState = $objectManager->get('Magento\Framework\App\State');

    $appState->setAreaCode('frontend');




    $virtual_product = $objectManager->create('Magento\Catalog\Model\Product');

    $virtual_product->setStatus(1); // status enabled/disabled 1/0

    $virtual_product->setName('Virtual Product Name'); // Set Your Virtual Product Name

    $virtual_product->setSku('virtual_product_sku'); // Set Virtual Product SKU

    $virtual_product->setTypeId('virtual');  // Set Product Type Id (simple/virtual/downloadable/configurable/grouped/bundle)

    $virtual_product->setAttributeSetId(4); // Set Attribute Set ID

    $virtual_product->setWebsiteIds(array(1)); // Set Website Ids

    $virtual_product->setTaxClassId(0); // Set Tax class ID

    $virtual_product->setVisibility(4); // visibility of product (Not Visible Individually (1) / Catalog (2)/ Search (3)/ Catalog, Search(4))

    $category_id = array(2, 3);

    $virtual_product->setCategoryIds($category_id); // Assign your to category using category Id

    $virtual_product->setPrice(199); // Set Virtual Product Price

    $virtual_product->setStockData(

        [

            'use_config_manage_stock' => 0,

            'manage_stock' => 1,

            'min_sale_qty' => 1,

            'max_sale_qty' => 2,

            'is_in_stock' => 1,

            'qty' => 1000,

        ]

    );

    $virtual_product->save();

    if ($virtual_product->getId()) {

        echo "Virtual Product Created Successfully.";

    }

}

catch(\Exception $e) {

    print_r($e->getMessage());

}

After adding the above code, run the below URL

https://yourdomain.com/virtual_product.php

That’s it!

Conclusion:

This way, you can Create Virtual Product Programmatically in Magento 2. Besides this, make your store feature-rich with custom extension development according to the requirements of your store. Share the article further to help out your friends and stay in touch with us!

Happy Reading!

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago