protected function _createProduct($type, $doSave = true, $originalProduct, $newImagePath = "")
{
Mage::register('isSecureArea', true);
$session = Mage::getSingleton('customer/session');
$result = array(
'success' => false
);
$res = '';
$login = "";
$productId = "";
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
$productId = $this->getRequest()->getPost('product_id');
}
try {
if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {
$session->login($login['username'], $login['password']);
$result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array(
'_secure' => true
));
$result['success'] = true;
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
}
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
// Registered user click on save design
return $res = $this->saveProduct($type, $doSave, $originalProduct, $newImagePath);
} else {
$p_data = array(
'type' => $type,
'doSave' => $doSave,
'newImagePath' => $newImagePath,
'name' => $originalProduct->getName(),
);
Mage::getSingleton('core/session')->unsProductdata();
Mage::getSingleton('core/session')->setProductdata($p_data);
$result['success'] = false;
//guest enter wrong credentilas & click on login
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
return $this; exit;
}
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login['username']));
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$result['error'] = $message;
$session->setUsername($login['username']);
} catch (Exception $e) {
$result = "ERROR :" . $e->getMessage();
}
Mage::unregister('isSecureArea');
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
public function saveProduct($type, $doSave = true, $originalProduct, $newImagePath) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$rand = rand(11, 999999999);
$product->setTypeId($type)
->setAttributeSetId(17)
->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;
}
$newImagePath
variable. May be you are not sending this value. Check that.