Skip to main content

Magento 2 add single product using custom script or programmatically

Magento 2 add single product programmatically

 <?php  
 //increase the max execution time  
 @ini_set('max_execution_time', -1);  
 //memory_limit  
 @ini_set('memory_limit', -1);  
 error_reporting(E_ALL);  
 ini_set('display_errors', '1');  
 use \Magento\Framework\App\Bootstrap;  
 require __DIR__ . '/app/bootstrap.php';  
 $bootstrap = Bootstrap::create(BP, $_SERVER);  
 $bootstrapManager = $bootstrap->getObjectManager();  
 $state = $bootstrapManager->get('\Magento\Framework\App\State');  
 $state->setAreaCode('frontend');  
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager  
 $product = $objectManager->create('\Magento\Catalog\Model\Product');  
 $sku = 'sku';  
 $name = 'Sample Product';  
 $product->setSku($sku); // Set your sku here  
 $product->setName($name); // 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('simple'); // type of product (simple/virtual/downloadable/configurable)  
 $product->setPrice(100); // price of product  
 $product->setStockData(  
             array(  
               'use_config_manage_stock' => 0,  
               'manage_stock' => 1,  
               'is_in_stock' => 1,  
               'qty' => 999999999  
             )  
           );  
 $product->save();  
 // Adding Image to product  
 $imagePath = "sample.jpg"; // path of the image  
 $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);  
 $product->save();  
 ?>  

Comments

Popular posts from this blog

Magento 2 How to get product collection using controller

Getting product collection in Magento 2 Step 1: Declare in Vendor_ModuleName Block Step 2: Display product collection in phtml file .. Step 1: Declare in Vendor_ModuleName Block You will use a block class of the module Vendor_ModuleName, then possibly inject the object of `\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory` in the constructor of the module’s block class.     app/code/Vendor/ModuleName/Block/ProductCollection.php add code this code  <?php namespace Vendor\ModuleName\Block; class ProductCollection extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, array $data = [] ) { $this->_productCollectionFact...

Magento 2 add/update configure products meta title, Description programetically using custom script

Magento 2 add/update meta values of configure products using custom script <?php //increase the max execution time @ini_set('max_execution_time', -1); //memory_limit @ini_set('memory_limit', -1); error_reporting(E_ALL); ini_set('display_errors', '1'); use \Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $instance = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $product_collections = $instance->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collections = $product_collections->create()->addAttributeToFilter('type_id', 'configurable'); foreach ($collections as $product) { echo ...