Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proxium/830fe06bf87083e5794a to your computer and use it in GitHub Desktop.
Save proxium/830fe06bf87083e5794a to your computer and use it in GitHub Desktop.
You need to put this script under shell/ directory in you Magento folder and adjust the constants to sweet your needs.
<?php
// env config
ini_set('display_errors', 1);
umask(0);
// mage setup
require_once dirname(__FILE__).'/../app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// input values
define('CATEGORY_ID', 4);
define('PRODUCT_QTY', 15);
define('IMAGES_PER_PRODUCT', 1);
define('IMAGE_API', 'http://lorempixel.com/700/700/technics/');
// make an array with all websites
$website_ids = array();
foreach (Mage::app()->getWebsites() as $website)
$website_ids[] = $website->getId();
for ($i = 0; $i < PRODUCT_QTY; $i++)
{
srand(500+$i);
$lorem_text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium.';
$dummy_text['lipsum'] = str_shuffle($lorem_text);
$dummy_title = substr($dummy_text['lipsum'], 0, 30);
$dummy_shortdescr = substr($dummy_text['lipsum'], 31, 220);
$dummy_descr = substr($dummy_text['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));
$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