Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Will create a series of random products
  5. *
  6. * @category Data Mining
  7. * @package Data_Mining
  8. * @author Diana Botean <diana.botean@gmail.com>
  9. */
  10. require_once '../abstract.php';
  11.  
  12. class Data_Mining_Create_Products extends Mage_Shell_Abstract
  13. {
  14.  
  15. /**
  16. * Will retrieve the options of a dropdown/multiselect attribute
  17. *
  18. * @author Diana Botean <diana.botean@gmail.com>
  19. * @param string $attributeCode
  20. * @return array
  21. */
  22. public function getAttributeOptions($attributeCode)
  23. {
  24. $values = array();
  25. $attributeDetails = Mage::getSingleton("eav/config")
  26. ->getAttribute("catalog_product", $attributeCode);
  27. $options = $attributeDetails->getSource()->getAllOptions(false);
  28. foreach ($options as $option) {
  29. $values[] = $option["value"];
  30. }
  31. return $values;
  32. }
  33.  
  34. /**
  35. * Will create a batch of products containing a given number of products
  36. *
  37. * @author Diana Botean <diana.botean@gmail.com>
  38. * @param int $numberOfProducts
  39. */
  40. public function createBatch($numberOfProducts)
  41. {
  42. $colors = $this->getAttributeOptions("color");
  43. $manufacturers = $this->getAttributeOptions("manufacturer");
  44.  
  45.  
  46. $productModel = Mage::getModel('catalog/product');
  47. $attrColor = $productModel->getResource()->getAttribute("color");
  48. $attrManufacturer = $productModel->getResource()->getAttribute("manufacturer");
  49. $catIds = array(3, 4, 5);
  50. $catBatch = intval($numberOfProducts / count($catIds));
  51. $lastIndex = $catBatch;
  52. $startIndex = 1;
  53.  
  54. foreach ($catIds as $catId) {
  55.  
  56. for ($i = $startIndex; $i <= $lastIndex; $i++) {
  57. $sku = 'test' . $i;
  58. $attrSetId = 4;
  59. $color = $colors[array_rand($colors)];
  60. $manufacturer = $manufacturers[array_rand($manufacturers)];
  61.  
  62. $colorLabel = $attrColor->getSource()->getOptionText($color);
  63. $manufacturerLabel = $attrManufacturer->getSource()->getOptionText($manufacturer);
  64.  
  65. $name = "Product " . $i . " " . $manufacturerLabel . " " . $colorLabel;
  66. $price = rand(10, 2000);
  67.  
  68. $this->createProduct($sku, $attrSetId, $name, $catId, $price, $color, $manufacturer);
  69. }
  70. $startIndex = $lastIndex+1;
  71. $lastIndex += $catBatch;
  72. }
  73. }
  74.  
  75.  
  76. /**
  77. * Will create a product with a given set of properties
  78. *
  79. * @author Diana Botean <diana.botean@gmail.com>
  80. * @param string $sku
  81. * @param int $attrSetId
  82. * @param string $name
  83. * @param int $catId
  84. * @param int $price
  85. * @param int $color
  86. * @param int $manufacturer
  87. */
  88. public function createProduct($sku, $attrSetId, $name, $catId, $price,
  89. $color, $manufacturer)
  90. {
  91.  
  92. $product = new Mage_Catalog_Model_Product();
  93. // Build the product
  94. $product->setSku($sku);
  95. $product->setAttributeSetId($attrSetId);
  96. $product->setTypeId('simple');
  97. $product->setName($name);
  98. $product->setCategoryIds(array(2, $catId));
  99. $product->setWebsiteIDs(array(
  100. 1
  101. ));
  102. $product->setPrice($price);
  103. # Custom created and assigned attributes
  104. $product->setColor($color);
  105. $product->setManufacturer($manufacturer);
  106. //Default Magento attribute
  107. $product->setVisibility(
  108. Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH
  109. );
  110. $product->setStatus(1);
  111. $product->setTaxClassId(0);
  112. $product->setStockData(array('manage_stock'=> 0,'is_in_stock' => 1, 'qty' => 100));
  113.  
  114. $product->setCreatedAt(strtotime('now'));
  115.  
  116. $imagePath = $this->getRandomPic( Mage::getBaseDir('media') . DS. "sample");
  117. if(!empty($imagePath)){
  118. $product->addImageToMediaGallery($imagePath,array('image','small_image','thumbnail'),true,false);
  119. }
  120. try {
  121. $product->save();
  122. } catch (Exception $ex) {
  123. Mage::logException($ex);
  124. }
  125. }
  126.  
  127. /**
  128. * Will return the file path of a random picked file from a given folder
  129. *
  130. * @author Diana Botean <diana.botean@gmail.com>
  131. * @param string $dir
  132. * @return string
  133. */
  134. public function getRandomPic($dir)
  135. {
  136. $files = glob($dir . '/*.*');
  137. $file = array_rand($files);
  138. return $files[$file];
  139. }
  140.  
  141. public function run()
  142. {
  143. $this->createBatch(120);
  144.  
  145. }
  146.  
  147. }
  148. $script = new Data_Mining_Create_Products();
  149. $script->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement