Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save molotovbliss/9768933 to your computer and use it in GitHub Desktop.

Select an option

Save molotovbliss/9768933 to your computer and use it in GitHub Desktop.
Use filltext.com API and lorempixel.com to generate dummy product data
<?php
// Use filltext.com API and lorempixel.com to generate dummy product data
// Based off: https://github.com/sandermangel/magento-dummy-installers
// Usage: Upload this file to tmp/ under magento root or modify require_once, execute.
// env config
ini_set('display_errors', 1);
umask(0);
// mage setup
require_once '../app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// input values
define('CATEGORY_ID', 4); // Category ID to assign all products created to
define('PRODUCT_QTY', 15); // Number of products to generate
define('IMAGES_PER_PRODUCT', 4); // Number of images per product
define('LIPSUM_API', 'http://filltext.com/?rows=10&lipsum={lorem|220}&pretty=true'); // http://filltext.com/
define('IMAGE_API', 'http://lorempixel.com/700/700/cats/'); // http://lorempixel.com/ (everyone luvs cats!)
// make an array with all websites
$website_ids = array();
foreach (Mage::app()->getWebsites() as $website)
$website_ids[] = $website->getId();
echo "Creating ".PRODUCT_QTY." Dummy Products...";
for ($i = 0; $i < PRODUCT_QTY; $i++)
{
$dummy_text = json_decode(file_get_contents(sprintf(LIPSUM_API, 500+$i)), true);
if(!$dummy_text[0]) {
die("Problem with filltext API");
}
$dummy_title = substr($dummy_text[0]['lipsum'], 0, 30);
$dummy_shortdescr = substr($dummy_text[0]['lipsum'], 31, 220);
$dummy_descr = substr($dummy_text[0]['lipsum'], 221);
$dummy_sku = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 10);
$rand_price = rand(10, 999);
$cost_price = rand(10, ($rand_price/2));
$special_price = rand($cost_price, ($rand_price/2));
zend_debug::dump($dummy_title);
$product = Mage::getModel('catalog/product');
$product->setData(array(
'name' => $dummy_title,
'sku' => $dummy_sku,
'short_description' => $dummy_shortdescr,
'description' => $dummy_descr,
'price' => number_format($rand_price / 10, 2, '.', ''),
'cost' => number_format($cost_price / 10, 2, '.', ''),
'weight' => number_format(rand(1,10), 2, '.',''),
'status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED,
'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
'type_id' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
'tax_class_id' => 2,
'attribute_set_id' => 4,
'category_ids' => array(CATEGORY_ID),
'website_ids' => $website_ids,
));
if ($i%4 == 0) // give some products a special price
{
$product->setSpecialPrice(number_format($special_price / 10, 2, '.', ''));
}
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99,
'manage_stock' => 1,
));
try {
$product->save();
} catch (Exception $e) {
echo "ERROR: {$e}\n";
die();
}
$product_id = $product->getIdBySku($dummy_sku);
$product = Mage::getModel('catalog/product')->load($product_id);
mkdir('tmpimage', 0777);
for ($j = 0; $j < IMAGES_PER_PRODUCT; $j++)
{
// download and save file
$image_source = file_get_contents(IMAGE_API);
$image_name = dirname(__FILE__)."/tmpimage/{$dummy_sku}}_{$j}.jpg";
file_put_contents($image_name , $image_source);
chmod($image_source, 0777);
try {
$product->addImageToMediaGallery($image_name , ($j==0) ? array( 'thumbnail', 'small_image', 'image' ) : null, true, false );
} catch (Exception $e){
echo "ERROR: {$e}\n";
}
unlink($image_name);
unset($download);
}
try {
$product->save();
} catch (Exception $e) {
echo "ERROR: {$e}\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment