Guest User

HGH

a guest
May 3rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. protected function _createProduct($type, $doSave = true, $originalProduct, $newImagePath = "")
  2. {
  3.  
  4. // code for Guest
  5.  
  6. Mage::register('isSecureArea', true);
  7. $session = Mage::getSingleton('customer/session');
  8.  
  9. // if ($session->isLoggedIn()) {
  10. // return;
  11. // }
  12.  
  13. $result = array(
  14. 'success' => false
  15. );
  16.  
  17. $login = "";
  18. $productId = "";
  19. if ($this->getRequest()->isPost()) {
  20. $login = $this->getRequest()->getPost('login');
  21. $productId = $this->getRequest()->getPost('product_id');
  22. }
  23. // echo $login['product_id'];die;
  24. // if (!empty($login['username']) && !empty($login['password'])) {
  25. try {
  26. if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {
  27. $designId = Mage::getSingleton('core/session')->getData('mydesign_id');
  28. $session->login($login['username'], $login['password']);
  29. $result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array(
  30. '_secure' => true
  31. ));
  32. $result['success'] = true;
  33. Mage::app()->getLayout()->getBlock('head')->addJs('js/hide/here.js');
  34. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  35. $model = Mage::getModel('example/mydesign')->load($designId);
  36. if ($model->getId()) {
  37. $model->setData('customer_id',$customerId);
  38. $model->save();
  39. }
  40.  
  41. Mage::getSingleton('core/session')->unsetData('mydesign_id');
  42. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  43. return $this;
  44. } else {
  45. $customerId = "";
  46. }
  47.  
  48. // code for guest end
  49.  
  50. // required for some versions
  51. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  52.  
  53. $product = Mage::getModel('catalog/product');
  54.  
  55. // set madatory system attributes
  56. $rand = rand(1, 999999999);
  57. $product->setTypeId($type)// e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
  58. ->setAttributeSetId(17)// default attribute set
  59. ->setSku('CDSB1_sku' . $rand)// generate some random SKU
  60. ->setWebsiteIDs(array(
  61. 1
  62. ));
  63.  
  64. // make the product visible
  65. $product
  66. ->setCategoryIds(array(2, 3))
  67. ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
  68. ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
  69. ;
  70. // configure stock
  71. $product->setStockData(array(
  72. 'use_config_manage_stock' => 1, // use global config ?
  73. 'manage_stock' => 0, // shoudl we manage stock or not?
  74. 'is_in_stock' => 1,
  75. 'qty' => 50
  76. ));
  77.  
  78. // optimize performance, tell Magento to not update indexes
  79. $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);
  80.  
  81. // finally set custom data
  82.  
  83. $product->setName($originalProduct->getName());
  84.  
  85. //$product->setDescription($originalProduct->getName());
  86. $product->setDescription('what ever you need here')->setShortDescription('short desciption')
  87. // set up prices
  88. ->setPrice(599)
  89. //->setSpecialPrice(19.99)
  90. ->setTaxClassId(0)// Taxable Goods by default
  91. ->setWeight(1);
  92. $product->setDeliveryInfo('content goes here');
  93. $product->setInteriorExterior('content goes here');
  94.  
  95. // kiatng code
  96.  
  97. $storeId = $customer->getStoreId();
  98. $product = Mage::getModel('catalog/product')
  99. ->setStoreId($storeId);
  100. // if you know the product ID
  101. $product->load($productId);
  102. // if you know the sku
  103. $product->loadByAttribute($sku, 'sku');
  104. // you can load by another attribute, if the name of the product is unique
  105. $product->loadByAttribute($name, 'name');
  106.  
  107. // kiatng code end
  108.  
  109.  
  110.  
  111.  
  112. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  113. $product->setCreatedByCustomerId($customerId);
  114. $product->setCustomUid($customerId);
  115.  
  116. $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
  117.  
  118. // $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');
  119. // $newImagePath = Mage::getSingleton('checkout/session')->getNewImagePath();
  120.  
  121. if ($doSave) {
  122.  
  123. // add product images
  124.  
  125. $images = array(
  126. 'thumbnail' => 'image.png', // displaying under cart page
  127. //'small_image' => 'image.png',
  128. 'image' => 'image.png' // displaying under my design
  129. );
  130. foreach ($images as $imageType => $imageFileName) {
  131. $path = '';
  132. if ($newImagePath != "") {
  133. $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
  134. $path = $dir . $newImagePath;
  135. } else {
  136. $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
  137. $path = $dir . $imageFileName;
  138. }
  139. //echo $path."<br>";
  140. //Mage::log("File Path: " . $path, Zend_Log::DEBUG, 'debug.log');
  141. //Mage::log("File Path: " . file_exists($path), Zend_Log::DEBUG, 'debug1.log');
  142.  
  143. if (file_exists($path)) {
  144. try {
  145. $product->addImageToMediaGallery($path, $imageType, false);
  146. } catch (Exception $e) {
  147. echo $e->getMessage();
  148. }
  149. } else {
  150. echo "Can not find image by path: `{$path}`<br/>";
  151. }
  152. }
  153. }
  154. $product->save();
  155.  
  156.  
  157. // To save inside my_designs
  158. $fav1_id = '';
  159. $data = array(
  160. 'title' => $fav1_id,
  161. 'product_id' => $product->getId(),
  162. 'customer_id' => $customerId,
  163. 'store_id' => 1,
  164. 'created_at' => $emaildate,
  165. 'value' => $newImagePath
  166. // 'value' => $emailimage
  167. );
  168. //echo '<pre/>';print_r($data);die('die');
  169. $model = Mage::getModel('example/mydesign')->setData($data);
  170. try {
  171. $fav1_id = $model->save()->getId();
  172. if (!$customerId) {
  173. Mage::getSingleton('core/session')->setData('mydesign_id', $fav1_id);
  174. }
  175. //echo "Custom Design saved successfully";
  176. } catch (Exception $e) {
  177. echo $e->getMessage();
  178. }
  179. // To save inside my_designs end
  180.  
  181. return $product;
  182.  
  183. // code for guest
  184.  
  185.  
  186. } catch (Mage_Core_Exception $e) {
  187. switch ($e->getCode()) {
  188. case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  189. $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']));
  190. break;
  191. case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  192. $message = $e->getMessage();
  193. break;
  194. default:
  195. $message = $e->getMessage();
  196. }
  197. $result['error'] = $message;
  198. $session->setUsername($login['username']);
  199.  
  200. } catch (Exception $e) {
  201. $result = "ERROR :" . $e->getMessage();
  202. // Mage::helper("core")->log("There has been an error during the login.");
  203. // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
  204. }
  205. // } else {
  206. // $result['error'] = Mage::helper('customer')->__('Login and password are required.');
  207. // }
  208. // }
  209.  
  210. Mage::unregister('isSecureArea');
  211. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  212.  
  213. // code for guest end
  214. }
Add Comment
Please, Sign In to add comment