Skip to content

Instantly share code, notes, and snippets.

@yvoronoy
Created June 10, 2016 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yvoronoy/ac0b5c39ef1269b4dd960cc8591e246f to your computer and use it in GitHub Desktop.
Save yvoronoy/ac0b5c39ef1269b4dd960cc8591e246f to your computer and use it in GitHub Desktop.
Generate Magento1 Test Data
#!/usr/bin/php
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$parentId = '2';
$randomString = readable_random_string();
try{
$category = Mage::getModel('catalog/category');
$category->setName($randomString);
$category->setUrlKey($randomString);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
} catch(Exception $e) {
print_r($e);
}
$api = new Mage_Catalog_Model_Product_Api();
$attr_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
$attribute_sets = $attr_api->items();
$prefix = 'demo.';
$tax_class = 2;
$x = 1;
$sku = readable_random_string();
$price = 100;
$weight = rand(1,2);
$productData = array();
$productData['website_ids'] = array(1);
$productData['categories'] = array($category->getId());
$productData['status'] = 1;
$productData['name'] = $sku;
$productData['description'] = utf8_encode('This is a Demo Description for the Product: Demo ' . $sku);
$productData['short_description'] = utf8_encode('This is a short Demo Description for the Product: Demo ' . $sku);
$productData['price'] = $price;
$productData['weight'] = $weight;
$productData['tax_class_id'] = $tax_class;
$new_product_id = $api->create('simple', $attribute_sets[0]['set_id'], $prefix . $sku, $productData);
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct( $new_product_id );
$stockItem->setData('use_config_manage_stock', 1);
$stockItem->setData('qty', 100);
$stockItem->setData('min_qty', 0);
$stockItem->setData('use_config_min_qty', 1);
$stockItem->setData('min_sale_qty', 0);
$stockItem->setData('use_config_max_sale_qty', 1);
$stockItem->setData('max_sale_qty', 0);
$stockItem->setData('use_config_max_sale_qty', 1);
$stockItem->setData('is_qty_decimal', 0);
$stockItem->setData('backorders', 0);
$stockItem->setData('notify_stock_qty', 0);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('tax_class_id', 0);
$stockItem->save();
$product = Mage::getModel('catalog/product')->load($new_product_id);
$product->save();
function readable_random_string($length = 6)
{
$string = '';
$vowels = array("a","e","i","o","u");
$consonants = array(
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
);
// Seed it
srand((double) microtime() * 1000000);
$max = $length/2;
for ($i = 1; $i <= $max; $i++)
{
$string .= $consonants[rand(0,19)];
$string .= $vowels[rand(0,4)];
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment