import product and images
This commit is contained in:
@@ -50,14 +50,139 @@ try {
|
||||
|
||||
|
||||
$categories = createCategories();
|
||||
createColors();
|
||||
createBrand();
|
||||
$color = createColors();
|
||||
$brand = createBrand();
|
||||
|
||||
echo "creating templates\n";
|
||||
$template = new \Thelia\Model\Template();
|
||||
$template
|
||||
->setLocale('fr_FR')
|
||||
->setName('template de démo')
|
||||
->setLocale('en_US')
|
||||
->setName('demo template')
|
||||
->save();
|
||||
|
||||
$at = new Thelia\Model\AttributeTemplate();
|
||||
|
||||
$at
|
||||
->setTemplate($template)
|
||||
->setAttribute($color)
|
||||
->save();
|
||||
|
||||
$ft = new Thelia\Model\FeatureTemplate();
|
||||
|
||||
$ft
|
||||
->setTemplate($template)
|
||||
->setFeature($brand)
|
||||
->save();
|
||||
echo "end creating templates\n";
|
||||
|
||||
createProduct($categories, $template);
|
||||
|
||||
|
||||
|
||||
$con->commit();
|
||||
} catch (Exception $e) {
|
||||
echo "error : ".$e->getMessage()."\n";
|
||||
$con->rollBack();
|
||||
}
|
||||
|
||||
function createProduct($categories, $template)
|
||||
{
|
||||
echo "start creating products\n";
|
||||
$fileSystem = new \Symfony\Component\Filesystem\Filesystem();
|
||||
if (($handle = fopen(THELIA_ROOT . '/install/import/products.csv', "r")) !== FALSE) {
|
||||
$row=0;
|
||||
while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {
|
||||
$row++;
|
||||
if($row == 1) continue;
|
||||
$product = new \Thelia\Model\Product();
|
||||
$productCategories = explode(';', $data[13]);
|
||||
$product
|
||||
->setRef($data[0])
|
||||
->setVisible(1)
|
||||
->setTaxRuleId(1)
|
||||
->setTemplate($template)
|
||||
;
|
||||
foreach($productCategories as $productCategory) {
|
||||
|
||||
$productCategory = trim($productCategory);
|
||||
if(array_key_exists($productCategory, $categories)) {
|
||||
$product->addCategory($categories[$productCategory]);
|
||||
}
|
||||
}
|
||||
|
||||
$productCategories = $product->getProductCategories();
|
||||
$collection = new \Propel\Runtime\Collection\Collection();
|
||||
foreach($productCategories as $i => $productCategory) {
|
||||
if($i == 0) {
|
||||
$collection->append($productCategory->setDefaultCategory(1));
|
||||
} else {
|
||||
$collection->append($productCategory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$product->setProductCategories($collection);
|
||||
|
||||
$product
|
||||
->setLocale('en_US')
|
||||
->setTitle($data[1])
|
||||
->setChapo($data[2])
|
||||
->setDescription($data[4])
|
||||
->setPostscriptum($data[6])
|
||||
->setLocale('fr_Fr')
|
||||
->setTitle($data[1])
|
||||
->setChapo($data[3])
|
||||
->setDescription($data[5])
|
||||
->setPostscriptum($data[7])
|
||||
->save();
|
||||
|
||||
$images = explode(';', $data[10]);
|
||||
|
||||
foreach ($images as $image) {
|
||||
$image = trim($image);
|
||||
if(empty($image)) continue;
|
||||
var_dump($image);
|
||||
$productImage = new \Thelia\Model\ProductImage();
|
||||
$productImage
|
||||
->setProduct($product)
|
||||
->setFile($image)
|
||||
->save();
|
||||
$fileSystem->copy(THELIA_ROOT . 'install/import/images/'.$image, THELIA_ROOT . 'local/media/images/product/'.$image, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* $product = new Thelia\Model\Product();
|
||||
$product->setRef($category->getId() . '_' . $position . '_' . $faker->randomNumber(8));
|
||||
$product->addCategory($category);
|
||||
$product->setVisible(1);
|
||||
$productCategories = $product->getProductCategories();
|
||||
$collection = new \Propel\Runtime\Collection\Collection();
|
||||
$collection->prepend($productCategories[0]->setDefaultCategory(1));
|
||||
$product->setProductCategories($collection);
|
||||
$product->setVisible(1);
|
||||
$product->setPosition($position);
|
||||
$product->setTaxRuleId(1);
|
||||
$product->setTemplate($template);
|
||||
|
||||
setI18n($faker, $product);
|
||||
|
||||
$product->save();
|
||||
$productId = $product->getId();
|
||||
$productIdList[] = $productId;
|
||||
|
||||
$image = new \Thelia\Model\ProductImage();
|
||||
$image->setProductId($productId);
|
||||
generate_image($image, 1, 'product', $productId);
|
||||
|
||||
$document = new \Thelia\Model\ProductDocument();
|
||||
$document->setProductId($productId);
|
||||
generate_document($document, 1, 'product', $productId);*/
|
||||
echo "end creating products\n";
|
||||
}
|
||||
|
||||
function createBrand()
|
||||
{
|
||||
echo "start creating brands feature\n";
|
||||
@@ -86,6 +211,8 @@ function createBrand()
|
||||
fclose($handle);
|
||||
}
|
||||
echo "brands feature created successfully\n";
|
||||
|
||||
return $feature;
|
||||
}
|
||||
|
||||
function createCategories()
|
||||
@@ -103,11 +230,11 @@ function createCategories()
|
||||
->setPosition($row-1)
|
||||
->setParent(0)
|
||||
->setLocale('fr_FR')
|
||||
->setTitle($data[0])
|
||||
->setTitle(trim($data[0]))
|
||||
->setLocale('en_US')
|
||||
->setTitle($data[1])
|
||||
->setTitle(trim($data[1]))
|
||||
->save();
|
||||
$categories[$data[1]] = $category->getId();
|
||||
$categories[trim($data[1])] = $category;
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
@@ -144,6 +271,7 @@ function createColors()
|
||||
fclose($handle);
|
||||
}
|
||||
echo "colors attributes created with success\n";
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
function clearTables()
|
||||
|
||||
Reference in New Issue
Block a user