Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. <?php
  2. use MagentoFrameworkAppBootstrap;
  3. include('app/bootstrap.php');
  4.  
  5. // add bootstrap
  6. $bootstraps = Bootstrap::create(BP, $_SERVER);
  7. $object_Manager = $bootstraps->getObjectManager();
  8.  
  9. $app_state = $object_Manager->get('MagentoFrameworkAppState');
  10. $app_state->setAreaCode('frontend');
  11.  
  12. // get date
  13. $today_date = date("m/d/Y");
  14. $added_date = date('m/d/Y',strtotime("+17 day"));
  15.  
  16. $set_product = $object_Manager->create('MagentoCatalogModelProduct');
  17.  
  18. try{
  19. $set_product->setWebsiteIds(array(1));
  20. $set_product->setAttributeSetId(4);
  21. $set_product->setTypeId('simple');
  22. $set_product->setCreatedAt(strtotime('now'));
  23. // time of product creation
  24. $set_product->setName('Test Sample Products');
  25. // add Name of Product
  26. $set_product->setSku('add-sku-1');
  27. // add sku hear
  28. $set_product->setWeight(1.0000);
  29. // add weight of product
  30. $set_product->setStatus(1);
  31. $category_id= array('3,4');
  32. // add your catagory id
  33. $set_product->setCategoryIds($category_id);
  34. // Product Category
  35. $set_product->setTaxClassId(0);
  36. // type of tax class
  37. // (0 - none, 1 - default, 2 - taxable, 4 - shipping)
  38. $set_product->setVisibility(4);
  39. // catalog and search visibility
  40. $set_product->setManufacturer(28);
  41. // manufacturer id
  42. $set_product->setColor(24);
  43. //print_r($_product);die;
  44. $set_product->setNewsFromDate($today_date);
  45. // product set as new from
  46. $set_product->setNewsToDate($added_date);
  47. /* // add image path hear
  48. $set_product->setImage('/testimg/test.jpg');
  49. // add small image path hear
  50. $set_product->setSmallImage('/testimg/test1.jpg');
  51. // add Thumbnail image path hear
  52. $set_product->setThumbnail('/testimg/test2.jpg');
  53. // product set as new to
  54. */$set_product->setCountryOfManufacture('AF');
  55. // country of manufacture (2-letter country code)
  56. $set_product->setPrice(100.99) ;
  57. // price in form 100.99
  58. $set_product->setCost(88.33);
  59. // price in form 88.33
  60. $set_product->setSpecialPrice(99.85);
  61. // special price in form 99.85
  62. $set_product->setSpecialFromDate('06/1/2016');
  63. // special price from (MM-DD-YYYY)
  64. $set_product->setSpecialToDate('06/30/2018');
  65. // special price to (MM-DD-YYYY)
  66. $set_product->setMsrpEnabled(1);
  67. // enable MAP
  68. $set_product->setMsrpDisplayActualPriceType(1);
  69. // display actual price
  70. // (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
  71. $set_product->setMsrp(99.99);
  72. // Manufacturer's Suggested Retail Price
  73. $set_product->setMetaTitle('test meta title 2');
  74. $set_product->setMetaKeyword('test meta keyword 2');
  75. $set_product->setMetaDescription('test meta description 2');
  76. $set_product->setDescription('This is a long description');
  77. $set_product->setShortDescription('This is a short description');
  78. $set_product->setStockData(
  79. array(
  80. 'use_config_manage_stock' => 0,
  81. // checkbox for 'Use config settings'
  82. 'manage_stock' => 1, // manage stock
  83. 'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed
  84. 'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
  85. 'is_in_stock' => 1, // Stock Availability of product
  86. 'qty' => 100 // qty of product
  87. )
  88. );
  89.  
  90. $obj = MagentoFrameworkAppObjectManager::getInstance();
  91.  
  92. /** @var MagentoFrameworkFilesystem $filesystem */
  93. $filesystem = $obj->create('MagentoFrameworkFilesystem');
  94. /** @var MagentoFrameworkFilesystemDirectoryWriteInterface $mediaDirectory */
  95. $mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
  96. $mediaPath = $mediaDirectory->getAbsolutePath();
  97.  
  98. //Sample product id
  99. /* $productId = 1;
  100. $model = $obj->create('MagentoCatalogModelProduct');
  101. $product = $model->load($productId); */
  102. /* $mainImage = 'test.jpg';
  103. if ($mainImage) {
  104. $image_directory = $mediaPath.'testimg/'. $mainImage;
  105. if (file_exists($image_directory)) {
  106. $set_product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
  107. ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);//assigning image, thumb and small image to media gallery
  108. }
  109. else {
  110. $image_directory = $mediaPath.'testimg/'. 'test2.jpg';
  111. $set_product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
  112. ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);
  113. }
  114. } */
  115. $mainImage = $mediaPath.'testimg/test.jpg';
  116. $mainImage1 = $mediaPath.'testimg/test1.jpg';
  117. $mainImage2 = $mediaPath.'testimg/test2.jpg';
  118. $set_product->setImage(
  119. $mainImage
  120. )
  121. ->setSmallImage(
  122. $mainImage1
  123. )
  124. ->setThumbnail(
  125. $mainImage2
  126. )
  127. ->addImageToMediaGallery(
  128. $mainImage,
  129. null,
  130. false,
  131. false
  132. )->addImageToMediaGallery(
  133. $mainImage1,
  134. null,
  135. false,
  136. false
  137. )->addImageToMediaGallery(
  138. $mainImage2,
  139. null,
  140. false,
  141. false
  142. );
  143. $set_product->save();
  144. // get id of product
  145. $get_product_id = $set_product->getId();
  146. echo "Upload simple product id :: ".$get_product_id."n";
  147. }
  148. catch (Exception $e) {
  149. echo $e->getMessage();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement