Exemple #1
0
 $stock->setRef($productId . '_' . $pse_idx . '_' . $faker->randomNumber(8));
 $stock->setQuantity($faker->numberBetween(1, 50));
 $stock->setPromo($faker->numberBetween(0, 1));
 $stock->setNewness($faker->numberBetween(0, 1));
 $stock->setWeight($faker->randomFloat(2, 1, 5));
 $stock->setIsDefault($pse_idx == 0 ? true : false);
 $stock->setEanCode(substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 13));
 $stock->save();
 // associate document to virtual product
 if (array_key_exists($productId, $virtualProductList)) {
     $virtualDocument = new \Thelia\Model\MetaData();
     $virtualDocument->setMetaKey('virtual')->setElementKey(\Thelia\Model\MetaData::PSE_KEY)->setElementId($stock->getId())->setValue($virtualProductList[$productId])->save();
 }
 $price = $faker->randomFloat(2, 20, 250);
 $promoPrice = $price * $faker->randomFloat(2, 0, 1);
 $productPrice = new \Thelia\Model\ProductPrice();
 $productPrice->setProductSaleElements($stock);
 $productPrice->setCurrency($currency);
 $productPrice->setPrice($price);
 $productPrice->setPromoPrice($promoPrice);
 $productPrice->save();
 //associate attributes - or not - to PSE
 $alreadyPicked = array();
 $minAttrCount = $pse_count == 1 ? 0 : 1;
 for ($attrIdx = 0; $attrIdx < rand($minAttrCount, count($attributeList)); $attrIdx++) {
     $featureProduct = new Thelia\Model\AttributeCombination();
     do {
         $pick = array_rand($attributeList, 1);
     } while (in_array($pick, $alreadyPicked));
     $alreadyPicked[] = $pick;
     $featureProduct->setAttributeId($pick)->setAttributeAvId($attributeList[$pick][array_rand($attributeList[$pick], 1)])->setProductSaleElements($stock)->save();
Exemple #2
0
function createProduct($faker, $categories, $brands, $contents, $template, $attribute, $feature, $con)
{
    echo "start creating products\n";
    $fileSystem = new \Symfony\Component\Filesystem\Filesystem();
    if (($handle = fopen(THELIA_ROOT . '/setup/import/products.csv', "r")) !== FALSE) {
        $row = 0;
        while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {
            $row++;
            if ($row == 1) {
                continue;
            }
            $product = new \Thelia\Model\Product();
            $product->setRef($data[0])->setVisible(1)->setTaxRuleId(1)->setTemplate($template);
            $productCategories = explode(';', $data[15]);
            foreach ($productCategories as $productCategory) {
                $productCategory = trim($productCategory);
                if (array_key_exists($productCategory, $categories)) {
                    $product->addCategory($categories[$productCategory]);
                }
            }
            $brand = $data[11];
            if (array_key_exists($brand, $brands)) {
                $product->setBrand($brands[$brand]);
            }
            $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($con);
            $productCategories = $product->getProductCategories()->getFirst();
            $productCategories->setDefaultCategory(true)->save($con);
            // Set the position
            $product->setPosition($product->getNextPosition())->save($con);
            $images = explode(';', $data[10]);
            foreach ($images as $image) {
                $image = trim($image);
                if (empty($image)) {
                    continue;
                }
                $productImage = new \Thelia\Model\ProductImage();
                $productImage->setProduct($product)->setFile($image)->save($con);
                $fileSystem->copy(THELIA_ROOT . 'setup/import/images/' . $image, THELIA_ROOT . 'local/media/images/product/' . $image, true);
            }
            $pses = explode(";", $data[12]);
            foreach ($pses as $pse) {
                if (empty($pse)) {
                    continue;
                }
                $stock = new \Thelia\Model\ProductSaleElements();
                $stock->setProduct($product);
                $stock->setRef($product->getId() . '_' . uniqid('', true));
                $stock->setQuantity($faker->numberBetween(1, 50));
                if (!empty($data[9])) {
                    $stock->setPromo(1);
                } else {
                    $stock->setPromo(0);
                }
                $stock->setNewness($faker->numberBetween(0, 1));
                $stock->setWeight($faker->randomFloat(2, 1, 30));
                $stock->save($con);
                $productPrice = new \Thelia\Model\ProductPrice();
                $productPrice->setProductSaleElements($stock);
                $productPrice->setCurrencyId(1);
                $productPrice->setPrice($data[8]);
                $productPrice->setPromoPrice($data[9]);
                $productPrice->save($con);
                $attributeAv = \Thelia\Model\AttributeAvI18nQuery::create()->filterByLocale('en_US')->filterByTitle($pse)->findOne($con);
                $attributeCombination = new \Thelia\Model\AttributeCombination();
                $attributeCombination->setAttributeId($attribute->getId())->setAttributeAvId($attributeAv->getId())->setProductSaleElements($stock)->save($con);
            }
            $productSaleElements = $product->getProductSaleElementss()->getFirst();
            $productSaleElements->setIsDefault(1)->save($con);
            // associated content
            $associatedContents = explode(";", $data[14]);
            foreach ($associatedContents as $associatedContent) {
                $content = new ProductAssociatedContent();
                if (!array_key_exists($associatedContent, $contents)) {
                    continue;
                }
                $content->setProduct($product)->setContent($contents[$associatedContent])->save($con);
            }
            // feature
            $features = explode(";", $data[13]);
            foreach ($features as $aFeature) {
                $featurAv = \Thelia\Model\FeatureAvI18nQuery::create()->filterByLocale('en_US')->filterByTitle($aFeature)->findOne($con);
                $featureProduct = new Thelia\Model\FeatureProduct();
                $featureProduct->setProduct($product)->setFeatureId($feature->getId())->setFeatureAvId($featurAv->getId())->save($con);
            }
        }
    }
    echo "end creating products\n";
}