Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Will create a series of random products
- *
- * @category Data Mining
- * @package Data_Mining
- * @author Diana Botean <diana.botean@gmail.com>
- */
- require_once '../abstract.php';
- class Data_Mining_Create_Products extends Mage_Shell_Abstract
- {
- /**
- * Will retrieve the options of a dropdown/multiselect attribute
- *
- * @author Diana Botean <diana.botean@gmail.com>
- * @param string $attributeCode
- * @return array
- */
- public function getAttributeOptions($attributeCode)
- {
- $values = array();
- $attributeDetails = Mage::getSingleton("eav/config")
- ->getAttribute("catalog_product", $attributeCode);
- $options = $attributeDetails->getSource()->getAllOptions(false);
- foreach ($options as $option) {
- $values[] = $option["value"];
- }
- return $values;
- }
- /**
- * Will create a batch of products containing a given number of products
- *
- * @author Diana Botean <diana.botean@gmail.com>
- * @param int $numberOfProducts
- */
- public function createBatch($numberOfProducts)
- {
- $colors = $this->getAttributeOptions("color");
- $manufacturers = $this->getAttributeOptions("manufacturer");
- $productModel = Mage::getModel('catalog/product');
- $attrColor = $productModel->getResource()->getAttribute("color");
- $attrManufacturer = $productModel->getResource()->getAttribute("manufacturer");
- $catIds = array(3, 4, 5);
- $catBatch = intval($numberOfProducts / count($catIds));
- $lastIndex = $catBatch;
- $startIndex = 1;
- foreach ($catIds as $catId) {
- for ($i = $startIndex; $i <= $lastIndex; $i++) {
- $sku = 'test' . $i;
- $attrSetId = 4;
- $color = $colors[array_rand($colors)];
- $manufacturer = $manufacturers[array_rand($manufacturers)];
- $colorLabel = $attrColor->getSource()->getOptionText($color);
- $manufacturerLabel = $attrManufacturer->getSource()->getOptionText($manufacturer);
- $name = "Product " . $i . " " . $manufacturerLabel . " " . $colorLabel;
- $price = rand(10, 2000);
- $this->createProduct($sku, $attrSetId, $name, $catId, $price, $color, $manufacturer);
- }
- $startIndex = $lastIndex+1;
- $lastIndex += $catBatch;
- }
- }
- /**
- * Will create a product with a given set of properties
- *
- * @author Diana Botean <diana.botean@gmail.com>
- * @param string $sku
- * @param int $attrSetId
- * @param string $name
- * @param int $catId
- * @param int $price
- * @param int $color
- * @param int $manufacturer
- */
- public function createProduct($sku, $attrSetId, $name, $catId, $price,
- $color, $manufacturer)
- {
- $product = new Mage_Catalog_Model_Product();
- // Build the product
- $product->setSku($sku);
- $product->setAttributeSetId($attrSetId);
- $product->setTypeId('simple');
- $product->setName($name);
- $product->setCategoryIds(array(2, $catId));
- $product->setWebsiteIDs(array(
- 1
- ));
- $product->setPrice($price);
- # Custom created and assigned attributes
- $product->setColor($color);
- $product->setManufacturer($manufacturer);
- //Default Magento attribute
- $product->setVisibility(
- Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH
- );
- $product->setStatus(1);
- $product->setTaxClassId(0);
- $product->setStockData(array('manage_stock'=> 0,'is_in_stock' => 1, 'qty' => 100));
- $product->setCreatedAt(strtotime('now'));
- $imagePath = $this->getRandomPic( Mage::getBaseDir('media') . DS. "sample");
- if(!empty($imagePath)){
- $product->addImageToMediaGallery($imagePath,array('image','small_image','thumbnail'),true,false);
- }
- try {
- $product->save();
- } catch (Exception $ex) {
- Mage::logException($ex);
- }
- }
- /**
- * Will return the file path of a random picked file from a given folder
- *
- * @author Diana Botean <diana.botean@gmail.com>
- * @param string $dir
- * @return string
- */
- public function getRandomPic($dir)
- {
- $files = glob($dir . '/*.*');
- $file = array_rand($files);
- return $files[$file];
- }
- public function run()
- {
- $this->createBatch(120);
- }
- }
- $script = new Data_Mining_Create_Products();
- $script->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement