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!

Previous Article

How to Create Grouped Product Programmatically in Magento 2

Next Article

How to Create Bundled Product Programmatically in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨