Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function _createProduct($type, $doSave = true, $originalProduct, $newImagePath = "")
- {
- // code for Guest
- Mage::register('isSecureArea', true);
- $session = Mage::getSingleton('customer/session');
- // if ($session->isLoggedIn()) {
- // return;
- // }
- $result = array(
- 'success' => false
- );
- $login = "";
- $productId = "";
- if ($this->getRequest()->isPost()) {
- $login = $this->getRequest()->getPost('login');
- $productId = $this->getRequest()->getPost('product_id');
- }
- // echo $login['product_id'];die;
- // if (!empty($login['username']) && !empty($login['password'])) {
- try {
- if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {
- $designId = Mage::getSingleton('core/session')->getData('mydesign_id');
- $session->login($login['username'], $login['password']);
- $result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array(
- '_secure' => true
- ));
- $result['success'] = true;
- Mage::app()->getLayout()->getBlock('head')->addJs('js/hide/here.js');
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $model = Mage::getModel('example/mydesign')->load($designId);
- if ($model->getId()) {
- $model->setData('customer_id',$customerId);
- $model->save();
- }
- Mage::getSingleton('core/session')->unsetData('mydesign_id');
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
- return $this;
- } else {
- $customerId = "";
- }
- // code for guest end
- // required for some versions
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $product = Mage::getModel('catalog/product');
- // set madatory system attributes
- $rand = rand(1, 999999999);
- $product->setTypeId($type)// e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
- ->setAttributeSetId(17)// default attribute set
- ->setSku('CDSB1_sku' . $rand)// generate some random SKU
- ->setWebsiteIDs(array(
- 1
- ));
- // make the product visible
- $product
- ->setCategoryIds(array(2, 3))
- ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
- ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
- ;
- // configure stock
- $product->setStockData(array(
- 'use_config_manage_stock' => 1, // use global config ?
- 'manage_stock' => 0, // shoudl we manage stock or not?
- 'is_in_stock' => 1,
- 'qty' => 50
- ));
- // optimize performance, tell Magento to not update indexes
- $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);
- // finally set custom data
- $product->setName($originalProduct->getName());
- //$product->setDescription($originalProduct->getName());
- $product->setDescription('what ever you need here')->setShortDescription('short desciption')
- // set up prices
- ->setPrice(599)
- //->setSpecialPrice(19.99)
- ->setTaxClassId(0)// Taxable Goods by default
- ->setWeight(1);
- $product->setDeliveryInfo('content goes here');
- $product->setInteriorExterior('content goes here');
- // kiatng code
- $storeId = $customer->getStoreId();
- $product = Mage::getModel('catalog/product')
- ->setStoreId($storeId);
- // if you know the product ID
- $product->load($productId);
- // if you know the sku
- $product->loadByAttribute($sku, 'sku');
- // you can load by another attribute, if the name of the product is unique
- $product->loadByAttribute($name, 'name');
- // kiatng code end
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $product->setCreatedByCustomerId($customerId);
- $product->setCustomUid($customerId);
- $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
- // $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');
- // $newImagePath = Mage::getSingleton('checkout/session')->getNewImagePath();
- if ($doSave) {
- // add product images
- $images = array(
- 'thumbnail' => 'image.png', // displaying under cart page
- //'small_image' => 'image.png',
- 'image' => 'image.png' // displaying under my design
- );
- 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;
- }
- //echo $path."<br>";
- //Mage::log("File Path: " . $path, Zend_Log::DEBUG, 'debug.log');
- //Mage::log("File Path: " . file_exists($path), Zend_Log::DEBUG, 'debug1.log');
- if (file_exists($path)) {
- try {
- $product->addImageToMediaGallery($path, $imageType, false);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- } else {
- echo "Can not find image by path: `{$path}`<br/>";
- }
- }
- }
- $product->save();
- // To save inside my_designs
- $fav1_id = '';
- $data = array(
- 'title' => $fav1_id,
- 'product_id' => $product->getId(),
- 'customer_id' => $customerId,
- 'store_id' => 1,
- 'created_at' => $emaildate,
- 'value' => $newImagePath
- // 'value' => $emailimage
- );
- //echo '<pre/>';print_r($data);die('die');
- $model = Mage::getModel('example/mydesign')->setData($data);
- try {
- $fav1_id = $model->save()->getId();
- if (!$customerId) {
- Mage::getSingleton('core/session')->setData('mydesign_id', $fav1_id);
- }
- //echo "Custom Design saved successfully";
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- // To save inside my_designs end
- return $product;
- // code for guest
- } 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::helper("core")->log("There has been an error during the login.");
- // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
- }
- // } else {
- // $result['error'] = Mage::helper('customer')->__('Login and password are required.');
- // }
- // }
- Mage::unregister('isSecureArea');
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
- // code for guest end
- }
Add Comment
Please, Sign In to add comment