Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function saveProduct($type, $doSave = true, $originalProduct, $newImagePath) {
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $product = Mage::getModel('catalog/product');
- $setId = $product->getAttributeSetId();
- $attributSets = array(
- 9 => 17,
- 18 => 21,
- );
- if (in_array($setId, array_keys($attributSets)))
- {
- $rand = rand(11, 999999999);
- $product->setTypeId($type)
- ->setAttributeSetId($attributSets[$setId])
- ->setSku('CDSB1_sku' . $rand)
- ->setWebsiteIDs(array(
- 1
- ));
- }
- $product
- ->setCategoryIds(array(2, 3))
- ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
- ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
- ;
- $product->setStockData(array(
- 'use_config_manage_stock' => 1,
- 'manage_stock' => 0,
- 'is_in_stock' => 1,
- 'qty' => 50
- ));
- $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);
- $product->setName($originalProduct->getName());
- $product->setDescription($originalProduct->getDescription())->setShortDescription($originalProduct->getShortDescription())
- ->setPrice(599)
- ->setTaxClassId(0)
- ->setWeight(1);
- $product->setDeliveryInfo($originalProduct->getDeliveryInfo());
- $product->setInteriorExterior($originalProduct->getInteriorExterior());
- $product->setSizeSpace($originalProduct->getSizeSpace());
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $product->setCreatedByCustomerId($customerId);
- $product->setCustomUid($customerId);
- $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
- if ($doSave) {
- $images = array(
- 'image' => 'image.png'
- );
- $imag_data = Mage::getSingleton('core/session')->getProductImage();
- $newImagePath = $imag_data;
- foreach ($images as $imageType => $imageFileName)
- {
- $path = '';
- if ($newImagePath != "")
- {
- $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
- $path = $dir . $newImagePath;
- }
- else
- {
- $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
- $path = $dir . $imageFileName;
- }
- if (file_exists($path))
- {
- try
- {
- $product->addImageToMediaGallery($path, array('image','thumbnail'), false);
- }
- catch (Exception $e)
- {
- echo $e->getMessage();
- }
- }
- else
- {
- echo "Can not find image by path: `{$path}`<br/>";
- }
- }
- }
- $product->save();
- return $product;
- }
Add Comment
Please, Sign In to add comment