We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    If you want to save simple product using programming use below code

    1. Create file in magento 2 root named saveproduct.php

    2. Put below code in saveproduct.php file

    <?php
    
    	use MagentoFrameworkAppBootstrap;
    	include('app/bootstrap.php');
    
    	// add bootstrap
    	$bootstraps = Bootstrap::create(BP, $_SERVER);
    	$object_Manager = $bootstraps->getObjectManager();
    
    	$app_state = $object_Manager->get('MagentoFrameworkAppState');
    	$app_state->setAreaCode('frontend');
    
    	// get date 
    	$today_date = date("m/d/Y");
    	$added_date = date('m/d/Y',strtotime("+17 day"));
    
    	$set_product = $object_Manager->create('MagentoCatalogModelProduct');
    
        $set_product->setWebsiteIds(array(1));
        $set_product->setAttributeSetId(9);
        $set_product->setTypeId('simple');
        $set_product->setCreatedAt(strtotime('now')); 
        // time of product creation
        $set_product->setName('Test Sample Product 2'); 
        // add Name of Product
        $set_product->setSku('add-sku-2');
        // add sku hear
        $set_product->setWeight(1.0000);
        // add weight of product
        $set_product->setStatus(1);
        $category_id = array(4,5);
        // add your catagory id
        $set_product->setCategoryIds($category_id); 
        // Product Category
        $set_product->setTaxClassId(0); 
        // type of tax class 
        // (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        $set_product->setVisibility(4); 
        // catalog and search visibility
        $set_product->setManufacturer(28); 
        // manufacturer id
        $set_product->setColor(24);
        //print_r($_product);die;
        $set_product->setNewsFromDate($today_date); 
        // product set as new from
        $set_product->setNewsToDate($added_date); 
        // add image path hear
        $imagePath = "img/sony.png"; // path of the image (e.g pub/media/<image folder>)
        $set_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
        // product set as new to    
        $set_product->setCountryOfManufacture('IN'); 
        // country of manufacture (2-letter country code)
        $set_product->setPrice(100.99) ;
        // price in form 100.99
        $set_product->setCost(88.33); 
        // price in form 88.33
        $set_product->setSpecialPrice(99.85); 
        // special price in form 99.85
        $set_product->setSpecialFromDate('06/1/2016'); 
        // special price from (MM-DD-YYYY)
        $set_product->setSpecialToDate('06/30/2018'); 
        // special price to (MM-DD-YYYY)
        $set_product->setMsrpEnabled(1); 
        // enable MAP
        $set_product->setMsrpDisplayActualPriceType(1); 
        // display actual price 
        // (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
        $set_product->setMsrp(99.99); 
        // Manufacturer's Suggested Retail Price
        $set_product->setMetaTitle('test meta title 2');
        $set_product->setMetaKeyword('test meta keyword 2');
        $set_product->setMetaDescription('test meta description 2');
        $set_product->setDescription('This is a long description');
        $set_product->setShortDescription('This is a short description');
        $set_product->setStockData(
            array(
            'use_config_manage_stock' => 0, 
            // checkbox for 'Use config settings' 
            'manage_stock' => 1, // manage stock
            'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed 
            'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
            'is_in_stock' => 1, // Stock Availability of product
            'qty' => 100 // qty of product
            )
        );
    
        $attr1 = $set_product->getResource()->getAttribute('manufacturer');
        $avid1 = $attr1->getSource()->getOptionId('Sony'); //name in Default Store View
        $set_product->setData('manufacturer', $avid1);
    
        $attr2 = $set_product->getResource()->getAttribute('color');
        $avid2 = $attr2->getSource()->getOptionId('White'); //name in Default Store View
        $set_product->setData('color', $avid2);
        
        $years = array('India','Dubai','London', 'America', 'Indonesia');
        $attr3 = $set_product->getResource()->getAttribute('available_in')->getSource();
        $valuesIds = array_map(array($attr3, 'getOptionId'), $years);
        $set_product->setData('available_in', $valuesIds);
    
    
        $set_product->save();
        // get id of product
        $get_product_id = $set_product->getId();
        echo "Upload simple product id :: ".$get_product_id."n";
    
    ?>

    Here I have used attributes and category as per my requirement, So you should use attributes and category as per your requirements.

    field_5bfb909c5ccae

      Get a Free Quote