Guest User

feffe

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