Simplest Way To Programmatically Create Virtual Product In Magento 2
Magento 2 Virtual Products are the non-tangible products that can’t be touched and have no weight.
There are six product types in Magento 2:
- Simple Product
- Configurable Product
- Grouped Product
- Virtual Product
- Bundle Product
- Downloadable Product
The virtual products are products such as memberships, services, warranties, subscriptions, or any type of digital file. They are sold either individually or with grouped or bundle products. The perfect example of a virtual product is an ebook or a gym membership.
Here’s the easy method to programmatically create virtual product in Magento 2.
Method to programmatically create virtual product in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $objectManager = Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager try { $product = $objectManager->create('Magento\Catalog\Model\Product'); $product->setSku('Main Virtual'); // Set your sku here $product->setName('Virtual Color Product'); // Name of Product $product->setAttributeSetId(4); // Attribute set id $product->setStatus(1); // Status on product enabled/ disabled 1/0 $product->setWeight(10); // weight of product $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually) $product->setTaxClassId(0); // Tax class id $product->setTypeId('virtual'); // type of product (simple/virtual/downloadable/configurable) $product->setPrice(500); // price of product $product->setStockData( array( 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 200 ) ); // Adding Image to product $mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media'); $imagePath = "logo.jpg"; // path of the image $imageFilename = basename($imagePath); $image_type = substr(strrchr($imageFilename,"."),1); $filename = md5($imagePath . strtotime('now')).'.'.$image_type; if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true); else chmod($mediaDir, 0777); $filepath = $mediaDir . '/catalog/product'. $filename; //path for temp storage folder: pub/media file_put_contents($filepath, file_get_contents(trim($imagePath))); $imgArray = $filepath; $product->addImageToMediaGallery($imgArray, array('image', 'small_image', 'thumbnail'), false, false); $product->save(); // Adding Custom option to product $options = array( array( "sort_order" => 1, "title" => "Custom Option 1", "price_type" => "fixed", "price" => "10", "type" => "field", "is_require" => 0 ), array( "sort_order" => 2, "title" => "Custom Option 2", "price_type" => "fixed", "price" => "20", "type" => "field", "is_require" => 0 ) ); foreach ($options as $arrayOption) { $product->setHasOptions(1); $product->getResource()->save($product); $option = $objectManager->create('\Magento\Catalog\Model\Product\Option') ->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($arrayOption); $option->save(); $product->addOption($option); } } catch (\Exception $e){ echo $e->getMessage(); } |
If you are a beginner in Magento 2 platform and have any doubts regarding the method to create Magento 2 virtual product, or any other product types, feel free to post them in the Comments section below. I’d be glad to be of any help to my readers.
Please share this post with the Magento community via social media profiles.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Practical Way To Programmatically Create Downloadable Product In Magento 2
Learn to Programmatically Create Bundled Product In Magento 2
Next