Skip to content

Instantly share code, notes, and snippets.

@eriksimonic
Created June 21, 2015 20:49
Show Gist options
  • Save eriksimonic/d1dd986e491f47d5d771 to your computer and use it in GitHub Desktop.
Save eriksimonic/d1dd986e491f47d5d771 to your computer and use it in GitHub Desktop.
Create product from existing product to populate the magento database for testing purposes.
<?php
/**
* Created by PhpStorm.
* User: Admin
* Date: 20. 06. 2015
* Time: 08:21
*/
// env config
ini_set('display_errors', 1);
umask(0);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once($mageFilename);
class ImageCreator
{
static $imagesList = array();
static $ImagesCount = 0;
static $textst = array();
static $CATEGORY_ID = 4;
static $PRODUCT_QTY = 15;
static $IMAGES_PER_PRODUCT = 1;
static $tempImagesSource = 'tmpimage';
static $base_items = array();
static $loremEndpoint = 'http://www.lipsum.com/feed/json?amount=50&what=paras&start=0';
public function _init()
{
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
self::$imagesList = array_diff(
scandir(dirname(__FILE__) ."/" .self::$tempImagesSource),
array('.', '..')
);
if(count(self::$imagesList) == 0) {
echo "Geting temp images";
self::_getImages();
self::$imagesList = array_diff(
scandir(dirname(__FILE__) ."/". self::$tempImagesSource),
array('.', '..')
);
}
self::$ImagesCount = count(self::$imagesList)-1;
$items = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('entity_id')
->load();
foreach ($items as $i) {
self::$base_items[] = $i->getId();
}
}
private function _getImages ()
{
$categories = [
'abstract','animals',
'business','cats',
'city','food',
'nightlife','fashion',
'people','nature',
'sports', 'transport', 'technics',
];
mkdir(self::$tempImagesSource, 0777);
foreach ($categories as $cat)
{
for($i = 1; $i <= 10; $i++)
{
$image_name = dirname(__FILE__)."/".self::$tempImagesSource."/{$cat}-{$i}.jpg";
file_put_contents($image_name, fopen("http://lorempixel.com/700/700/{$cat}/{$i}", 'r'));
}
}
}
public function Create()
{
for($i =0; $i < 750000 ; $i++)
{
echo "Creating {$i} product \n";
$mageID = self::$base_items[ rand(0, count(self::$base_items) -1 )];
$product= Mage::getModel('catalog/product')->load((int)$mageID);
if($product)
{
$dataSet = $product->getData();
unset($dataSet['entity_id']);
self::CreateDummyProduct($dataSet, $product->getCategoryIds());
}
}
}
function getImage()
{
$name = self::$imagesList[rand(2, self::$ImagesCount)];
echo $name . "\n";
$image = dirname(__FILE__) . "/".self::$tempImagesSource."/{$name}";
$shufled_name = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 10);
$dest = dirname(__FILE__) . "/".self::$tempImagesSource."/{$shufled_name}.jpg";
copy($image, $dest);
return $dest;
}
private function CreateDummyProduct($data,$CATEGORY)
{
$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));
$data['sku'] = $dummy_sku;
$data['price'] = number_format($rand_price / 10, 2, '.', '');
$data['cost'] = number_format($cost_price / 10, 2, '.', '');
$data['weight'] = number_format(rand(1, 10), 2, '.', '');
$data['category_ids'] = $CATEGORY;
$product = Mage::getModel('catalog/product');
$product->setData($data);
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 < self::$IMAGES_PER_PRODUCT; $j++) {
// download and save file
$image_name = self::getImage();
try
{
$product->addImageToMediaGallery($image_name, ($j == 0) ? array('thumbnail', 'small_image', 'image') : null, true, false);
}
catch (Exception $e)
{
echo "ERROR: \n{$image_name}\n";
}
}
try {
$product->save();
} catch (Exception $e) {
echo "ERROR: {$e}\n";
}
}
}
$imgc = new ImageCreator();
$imgc->_init();
$imgc->Create();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment