Guest User

ferfer

a guest
Mar 13th, 2017
97
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. if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE))
  23. {
  24. $this->_redirect("catalog/product/view/id/".$product->getId());
  25. }
  26. }
  27.  
  28.  
  29. protected function _createProduct($type, $doSave=true)
  30. {
  31. // required for some versions
  32. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  33.  
  34. $product = Mage::getModel('catalog/product');
  35.  
  36. // set madatory system attributes
  37. $rand = rand(1, 9999);
  38. $product
  39. ->setTypeId($type) // e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
  40. ->setAttributeSetId(17) // default attribute set
  41. ->setSku('CDSB1_sku' . $rand) // generate some random SKU
  42. ->setWebsiteIDs(array(1))
  43. ;
  44.  
  45. // make the product visible
  46. $product
  47. ->setCategoryIds(array(2,3))
  48. ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
  49. ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
  50. ;
  51. // configure stock
  52. $product->setStockData(array(
  53. 'use_config_manage_stock' => 1, // use global config ?
  54. 'manage_stock' => 0, // shoudl we manage stock or not?
  55. 'is_in_stock' => 1,
  56. 'qty' => 50,
  57. ));
  58.  
  59. // optimize performance, tell Magento to not update indexes
  60. $product
  61. ->setIsMassupdate(true)
  62. ->setExcludeUrlRewrite(true)
  63. ;
  64.  
  65. // finally set custom data
  66. $product
  67. ->setName($product->getName()) // add string attribute
  68. ->setShortDescription('description') // add text attribute
  69.  
  70. // set up prices
  71. ->setPrice(599)
  72. //->setSpecialPrice(19.99)
  73. ->setTaxClassId(1) // Taxable Goods by default
  74. ->setWeight(1)
  75. ;
  76.  
  77. // add dropdown attributes like brand, color or size
  78. //$optionId = $this->_getOptionIDByCode('color', 'Black');
  79. //$product->setColor($optionId);
  80.  
  81. //$optionId = $this->_getOptionIDByCode('size', 'M');
  82. //$product->setSize($optionId);
  83.  
  84.  
  85. // add product images
  86.  
  87. $images = array(
  88. 'thumbnail' => 'image.png',
  89. 'small_image' => 'image.png',
  90. 'image' => 'image.png',
  91. );
  92.  
  93. $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
  94.  
  95. foreach ($images as $imageType => $imageFileName) {
  96. $path = $dir . $imageFileName;
  97. if (file_exists($path)) {
  98. try {
  99. $product->addImageToMediaGallery($path, $imageType, false);
  100. } catch (Exception $e) {
  101. echo $e->getMessage();
  102. }
  103. } else {
  104. echo "Can not find image by path: `{$path}`<br/>";
  105. }
  106. }
  107.  
  108. if ($doSave)
  109. $product->save();
  110.  
  111. return $product;
  112. }
  113.  
  114.  
  115.  
  116. protected function _getOptionIDByCode($attrCode, $optionLabel)
  117. {
  118. $attrModel = Mage::getModel('eav/entity_attribute');
  119.  
  120. $attrID = $attrModel->getIdByCode('catalog_product', $attrCode);
  121. $attribute = $attrModel->load($attrID);
  122.  
  123. $options = Mage::getModel('eav/entity_attribute_source_table')
  124. ->setAttribute($attribute)
  125. ->getAllOptions(false);
  126.  
  127. foreach ($options as $option) {
  128. if ($option['label'] == $optionLabel) {
  129. return $option['value'];
  130. }
  131. }
  132.  
  133. return false;
  134. }
  135.  
  136.  
  137.  
  138. }
RAW Paste Data