Let’s initiate a discussion!!
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
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?php use \Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); // add bootstrap $bootstraps = Bootstrap::create(BP, $_SERVER); $object_Manager = $bootstraps->getObjectManager(); $app_state = $object_Manager->get('\Magento\Framework\App\State'); $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('\Magento\Catalog\Model\Product'); $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.
[crayon-641ef78009c82554727255/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.