Guest User

grgtr

a guest
Apr 11th, 2017
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Amasty_Example_AmastyController extends Mage_Core_Controller_Front_Action
  4. {
  5. public function indexAction()
  6. {
  7. echo 'This is index controller. Please use specific actions below to create products.
  8. <br/>
  9. <a href="/example/amasty/createSimpleProduct">Create Simple Product</a>';
  10.  
  11. }
  12.  
  13.  
  14. public function createSimpleProductAction()
  15. {
  16. $product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
  17. echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">created simple product</a>';
  18. }
  19.  
  20. public function createSimpleProductAndRedirectAction()
  21. {
  22. $originalProductId = $this->getRequest()->getParam("id");
  23. // $productNameArray = explode(" - ",$this->getRequest()->getParam("id"));
  24. // $originalProductId = $productNameArray[0];
  25. $newImagePath = $this->getRequest()->getParam("image");
  26. $originalProduct = Mage::getModel('catalog/product')
  27. ->setStoreId(Mage::app()->getStore()->getId())
  28. ->load($originalProductId);
  29.  
  30. if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $originalProduct,$newImagePath))
  31. {
  32. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("Your Image Saved1"));
  33. }
  34.  
  35. }
  36.  
  37.  
  38. protected function _createProduct($type, $doSave=true, $originalProduct, $newImagePath="")
  39. {
  40.  
  41.  
  42.  
  43. // required for some versions
  44. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  45.  
  46. $product = Mage::getModel('catalog/product');
  47.  
  48. // set madatory system attributes
  49. $rand = rand(1, 9999);
  50. $product
  51. ->setTypeId($type) // e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
  52. ->setAttributeSetId(17) // default attribute set
  53. ->setSku('CDSB1_sku' . $rand) // generate some random SKU
  54. ->setWebsiteIDs(array(1))
  55. ;
  56.  
  57. // make the product visible
  58. $product
  59. ->setCategoryIds(array(2,3))
  60. ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
  61. ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
  62. ;
  63. // configure stock
  64. $product->setStockData(array(
  65. 'use_config_manage_stock' => 1, // use global config ?
  66. 'manage_stock' => 0, // shoudl we manage stock or not?
  67. 'is_in_stock' => 1,
  68. 'qty' => 50,
  69. ));
  70.  
  71. // optimize performance, tell Magento to not update indexes
  72. $product
  73. ->setIsMassupdate(true)
  74. ->setExcludeUrlRewrite(true)
  75. ;
  76.  
  77. // finally set custom data
  78. //if($_category instanceof Mage_Catalog_Model_Category && $_category->getId()):
  79. //$product->setName($originalProduct->getName());
  80. $product->setName($originalProduct->getName());
  81. // endif;
  82. //$product->setDescription($originalProduct->getName());
  83. $product->setDescription('what ever you need here')
  84.  
  85. ->setShortDescription('short desciption')
  86. // set up prices
  87. ->setPrice(599)
  88. //->setSpecialPrice(19.99)
  89. ->setTaxClassId(0) // Taxable Goods by default
  90. ->setWeight(1)
  91. ;
  92. $product->setDeliveryInfo('content goes here');
  93. $product->setInteriorExterior('content goes here');
  94.  
  95. // add product images
  96.  
  97. $images = array(
  98. 'thumbnail' => 'image.png', // displaying under cart page
  99. //'small_image' => 'image.png',
  100. 'image' => 'image.png', // displaying under my design
  101. );
  102.  
  103. foreach ($images as $imageType => $imageFileName) {
  104. if($newImagePath != ""){
  105. $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
  106. $path = $dir . $newImagePath;
  107. }else{
  108. $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
  109. $path = $dir . $imageFileName;
  110. }
  111. //echo $path."<br>";
  112. if (file_exists($path)) {
  113. try {
  114. $product->addImageToMediaGallery($path, $imageType, false);
  115. } catch (Exception $e) {
  116. echo $e->getMessage();
  117. }
  118. } else {
  119. echo "Can not find image by path: `{$path}`<br/>";
  120. }
  121. }
  122.  
  123. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  124. $product->setCreatedByCustomerId($customerId);
  125. //$emaildate = Mage::getModel('core/date')->date('Y-m-d H:i:s');
  126. // $emaildate = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
  127. $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
  128.  
  129. $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');
  130.  
  131. if ($doSave)
  132. $product->save();
  133. // To save inside my_designs
  134.  
  135. $data = array('title'=>fav1_id,'product_id'=>$product->getId(),
  136. 'customer_id'=> $customerId , 'store_id' => 1 , 'created_at' => $emaildate , 'value'=> $emailimage );
  137. $model = Mage::getModel('example/mydesign')->setData($data);
  138. try
  139. {
  140. $fav1_id = $model->save()->getId();
  141. //echo "Custom Design saved successfully";
  142. } catch (Exception $e)
  143. {
  144. echo $e->getMessage();
  145. }
  146. // To save inside my_designs end
  147.  
  148. return $product;
  149. }
  150.  
  151.  
  152.  
  153. protected function _getOptionIDByCode($attrCode, $optionLabel)
  154. {
  155. $attrModel = Mage::getModel('eav/entity_attribute');
  156.  
  157. $attrID = $attrModel->getIdByCode('catalog_product', $attrCode);
  158. $attribute = $attrModel->load($attrID);
  159.  
  160. $options = Mage::getModel('eav/entity_attribute_source_table')
  161. ->setAttribute($attribute)
  162. ->getAllOptions(false);
  163.  
  164. foreach ($options as $option) {
  165. if ($option['label'] == $optionLabel) {
  166. return $option['value'];
  167. }
  168. }
  169.  
  170. return false;
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. }
RAW Paste Data