Created
June 21, 2015 20:50
-
-
Save eriksimonic/8b39e56b96f51a6c7622 to your computer and use it in GitHub Desktop.
Add random stock image to product, testing catalog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
public function _init() | |
{ | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
self::$imagesList = array_diff( | |
scandir(dirname(__FILE__) . "/" . self::$tempImagesSource), | |
array('.', '..') | |
); | |
self::$ImagesCount = count(self::$imagesList) - 1; | |
} | |
public function Create() | |
{ | |
$items = Mage::getModel('catalog/product')->getCollection() | |
->addAttributeToSelect('entity_id') | |
->addAttributeToFilter('entity_id', array('lt' => '9557')); | |
foreach ($items as $i) { | |
$product = Mage::getModel('catalog/product')->load((int)$i->getId()); | |
if ($product) { | |
/** @var $product Mage_Catalog_Model_Product */ | |
self::CreateDummyProduct($product); | |
} | |
} | |
} | |
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($product) | |
{ | |
#mkdir('tmpimage', 0777); | |
for ($j = 0; $j < self::$IMAGES_PER_PRODUCT; $j++) { | |
// download and save file | |
$image_name = self::getImage(); | |
try { | |
/** @var $product Mage_Catalog_Model_Product */ | |
$product->getMediaGalleryImages(); | |
$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