Guest User

ferffr

a guest
Jun 29th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. public function saveProduct($type, $doSave = true, $originalProduct, $newImagePath) {
  2.  
  3. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  4.  
  5. $product = Mage::getModel('catalog/product');
  6. $setId = $product->getAttributeSetId();
  7.  
  8. $attributSets = array(
  9. 9 => 17,
  10. 18 => 21,
  11. );
  12.  
  13. if (in_array($setId, array_keys($attributSets)))
  14. {
  15. $rand = rand(11, 999999999);
  16.  
  17. $product->setTypeId($type)
  18. ->setAttributeSetId($attributSets[$setId])
  19. ->setSku('CDSB1_sku' . $rand)
  20. ->setWebsiteIDs(array(
  21. 1
  22. ));
  23. }
  24. $product
  25. ->setCategoryIds(array(2, 3))
  26. ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
  27. ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
  28. ;
  29.  
  30. $product->setStockData(array(
  31. 'use_config_manage_stock' => 1,
  32. 'manage_stock' => 0,
  33. 'is_in_stock' => 1,
  34. 'qty' => 50
  35. ));
  36.  
  37. $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);
  38.  
  39. $product->setName($originalProduct->getName());
  40.  
  41. $product->setDescription($originalProduct->getDescription())->setShortDescription($originalProduct->getShortDescription())
  42.  
  43. ->setPrice(599)
  44. ->setTaxClassId(0)
  45. ->setWeight(1);
  46. $product->setDeliveryInfo($originalProduct->getDeliveryInfo());
  47. $product->setInteriorExterior($originalProduct->getInteriorExterior());
  48. $product->setSizeSpace($originalProduct->getSizeSpace());
  49.  
  50.  
  51. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  52. $product->setCreatedByCustomerId($customerId);
  53. $product->setCustomUid($customerId);
  54.  
  55. $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
  56.  
  57.  
  58. if ($doSave) {
  59.  
  60. $images = array(
  61.  
  62. 'image' => 'image.png'
  63.  
  64. );
  65.  
  66.  
  67. $imag_data = Mage::getSingleton('core/session')->getProductImage();
  68. $newImagePath = $imag_data;
  69.  
  70. foreach ($images as $imageType => $imageFileName)
  71. {
  72. $path = '';
  73. if ($newImagePath != "")
  74. {
  75. $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
  76. $path = $dir . $newImagePath;
  77. }
  78. else
  79. {
  80. $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
  81. $path = $dir . $imageFileName;
  82. }
  83.  
  84. if (file_exists($path))
  85. {
  86. try
  87. {
  88. $product->addImageToMediaGallery($path, array('image','thumbnail'), false);
  89. }
  90. catch (Exception $e)
  91. {
  92. echo $e->getMessage();
  93. }
  94. }
  95. else
  96. {
  97. echo "Can not find image by path: `{$path}`<br/>";
  98. }
  99. }
  100. }
  101. $product->save();
  102.  
  103. return $product;
  104. }
Add Comment
Please, Sign In to add comment