public function actionCreate() { for ($n = 0; $n <= 1000; ++$n) { $randArticle = rand(99999, 999999); $randPrice = rand(9999, 99999); $randOldPrice = rand(9999, 99999); $randCount = rand(1, 999); $randCategory = rand(1, 3); $randBrand = rand(1, 5); $randDate = date('Y-m-d', mt_rand(strtotime('12-06-2006'), strtotime('1-09-2016'))); $product = new \App\Models\Product(); $product->fill(['name' => 'Product_' . $randArticle, 'article' => $randArticle, 'price' => $randPrice, 'old_price' => $randOldPrice, 'image' => '/' . $randArticle . '.jpg', 'date' => $randDate, 'count' => $randCount, 'category_id' => $randCategory, 'brand_id' => $randBrand]); $product->save(); } }
/** * Execute the console command. * * @return mixed */ public function handle() { return; $links_arr = (include '/var/www/kotik/storage/app/citynature_urls.php'); foreach ($links_arr as $link) { $file_name = 'citynature_pages/' . md5($link) . '.html'; $exists = \Storage::exists($file_name); if (!$exists) { continue; } $file_content = \Storage::get($file_name); preg_match('@<h1>(.+)</h1>@si', $file_content, $product_name); preg_match('@<span class="ean">([^<]+)</span>@si', $file_content, $article); preg_match('@<p>Категория\\: <a href="[/a-z0-9_-]{1,}">([^<]+)</a></p>@si', $file_content, $category); preg_match('@<p class="manufacturer_name">Бренд:[\\s]{0,}<a class = "bolder_cart_product" href = "[/a-z0-9_-]{1,}">([^<]+)</a>[\\s]{0,}, <a href="[/a-z0-9_-]{1,}">([^<]+)</a>[\\s]{0,}</p>@si', $file_content, $brand_country); preg_match('@<p>Линейка товаров: <a href="[/#a-z0-9_-]{1,}">([^<]+)</a></p>@si', $file_content, $products_line); preg_match('@<span>([0-9\\s]+) (мл|мг)+</span>@si', $file_content, $value); preg_match('@<div class="qty_in_stock">В наличии:[\\s]{0,}<span >([0-9]+)</span> [\\s]{0,}Мин. партия: от <span>([0-9]+)</span> шт.[\\s]{0,}</div>@si', $file_content, $part); preg_match('@<div class="jshop_prod_description"style = "margin-top:60px;">(.+?)</div>@si', $file_content, $description); preg_match_all('@<a class="lightbox" id="main_image_full_[\\d]+" href="([^"]+)"@si', $file_content, $images); $product_name = !empty($product_name) ? trim($product_name[1]) : ''; $article = !empty($article) ? trim($article[1]) : ''; $category = !empty($category) ? trim($category[1]) : ''; $brand = !empty($brand_country) ? trim($brand_country[1]) : ''; $country = !empty($brand_country) ? trim($brand_country[2]) : ''; $products_line = !empty($products_line) ? trim($products_line[1]) : ''; $images = !empty($images) ? $images[1] : []; $weight = !empty($value) ? $value[1] : ''; $weight = str_replace(' ', '', $weight); $measure_unit = !empty($value) ? trim($value[2]) : ''; $description = !empty($description) ? $description[1] : ''; $description = str_replace("</p>", "\n", $description); $description = strip_tags($description); $description = preg_replace('/Описание:/', '', $description); $description = trim($description); $in_stock = !empty($part) ? trim($part[1]) : 0; $min_party = !empty($part) ? trim($part[2]) : 0; $product_model = \App\Models\Product::create(['article' => $article, 'name' => $product_name, 'description' => $description, 'source_url' => $link, 'brand' => $brand, 'country_name' => $country, 'weight' => $weight, 'measure_unit' => $measure_unit, 'in_stock' => $in_stock, 'min_party' => $min_party, 'category_name' => $category, 'product_line' => $products_line]); if (!empty($images)) { foreach ($images as $image) { $media_model = new \App\Models\Media(); $media_model->product_id = $product_model->id; $media_model->source_url = $image; $media_model->save(); } } } return; $catalog = \App\Helpers\CitynatureHelper::getCatalogArrayFromCsvFile(base_path('storage/app/price1.csv')); $this->info('Catalog count = ' . count($catalog)); foreach ($catalog as $item) { if (\App\Models\Product::where('code', '=', $item->index)->first()) { continue; } $product = new \App\Models\Product(); $product->name = $item->title; $product->catalog_id = 1; $product->code = $item->index; $product->article = $item->article; $product->save(); $product->setAttributeById(1, $item->volume); // weight $product->setAttributeById(4, $item->article); // article $product->setPriceByColumnId(18, $item->price_1); $product->setPriceByColumnId(19, $item->price_2); $product->setPriceByColumnId(20, $item->price_3); $product->setPriceByColumnId(21, $item->price_4); echo '.'; } echo "\n"; }
public function doImport(ProductImportOptions $pio) { //\Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix() $path = $pio->getUuid() . '/' . $pio->getFileName(); $fileContents = \Storage::disk('imports')->get($path); $csv = Reader::createFromString($fileContents); $csv->setDelimiter(','); if ($pio->isIncludeHeaders()) { $csv->setOffset(1); //because we don't want to insert the header } $recordsAdded = 0; $recordsUpdated = 0; $recordsFailed = 0; $recordCount = 0; $uploader = new UploadHandler(); $csv->each(function ($row) use(&$pio, &$recordsAdded, &$recordsUpdated, &$recordsFailed, &$recordCount, &$uploader) { /* Row Structure * *CSV row format: * * Main, * Sub1, * Sub2, * Description, * Brand, * Pack, * Size, * UOM, * MPC, * Contact, * GTIN, * Halal, * IsOrganicProduct, * KosherClassification, * CalculationSize, * CalculationSizeUnitofMeasure, * Calories, * CaloriesfromFat, * Protein, * Carbohydrates, * TotalDietaryFibre, * TotalSugar, * TotalFat, * SaturatedFat, * Sodium mg, * AllergenPeanuts, * AllergenTreeNuts, * AllergenMilk, * AllergenLactose, * AllergenEggs, * AllergenFish, * AllergenShellfish, * AllergenSoy, * AllergenGluten, * Image URL, * ItemDetail, * Preparation, * IngredientsEnglish, * BenefitsEnglish * */ if ($row[0] != null) { if (!$pio->isSimulate()) { \DB::beginTransaction(); } try { // Check to see if the record already exists (update) if not ignore existing items //$product = \App\Models\Product::where('uniquekey', '=', $row[8]) // ->orWhere('uniquekey', '=', $row[11])->first(); $isExisting = false; //if(!is_null($product) && isset($product)) //{ //$isExisting = true; //} if ($isExisting && !$pio->isSimulate() && !$pio->isIgnoreExisting()) { // Clear any existing categories //\DB::table('product_categories')->where('product_id', '=', $product->id)->delete(); // Clear any existing allergens //\DB::table('product_allergens')->where('product_id', '=', $product->id)->delete(); } else { $product = new \App\Models\Product(); $product->vendor_id = $pio->getVendorId(); $product->uniquekey = isset($row[8]) ? $row[8] : $row[11]; // MPC or GTIN } $product->name = ucfirst(mb_strtolower($row[3])); $product->brand = ucfirst(mb_strtolower($row[4])); $product->pack = $row[5]; $product->size = $row[6]; $product->uom = $row[7]; $product->serving_size_uom = $row[7]; // Not sure this should be duplicated $product->mpc = $row[8]; $product->broker_contact = $row[9]; $product->gtin = $row[10]; $product->is_halal = $this->normalizeTrueFalseField($row[11]); $product->is_organic = $this->normalizeTrueFalseField($row[12]); $product->is_kosher = $this->normalizeTrueFalseField($row[13]); $product->calc_size = $row[14]; $product->calculation_size_uom = $row[15]; $product->calories = $row[16]; $product->calories_from_fat = $row[17]; $product->protein = $row[18]; $product->carbs = $row[19]; $product->fibre = $row[20]; $product->sugar = $row[21]; $product->total_fat = $row[22]; $product->saturated_fats = $row[23]; $product->sodium = $row[24]; //rows 25, 26, 27, 28, 29, 30, 31, 32, 33 are allergens // Product Image if ($row[34] !== '') { if ($pio->isDownloadImages()) { try { $newFilename = $uploader->getRemoteFile($row[34], null, public_path(config('app.product_storage'))); $product->product_image = $newFilename; } catch (Exception $ex) { /* If the image can't be downloaded, just don't set the field */ } } else { $product->product_image = $row[34]; } } $product->description = $row[35]; $product->preparation = $row[36]; $product->ingredient_deck = $row[37]; $product->features_benefits = $row[38]; $product->allergen_disclaimer = ''; $product->net_weight = 0; $product->gross_weight = 0; $product->tare_weight = 0; $product->serving_size = 0; $product->vendor_logo = ''; $product->pos_pdf = ''; if (!$isExisting) { $product->published = $pio->isAddAsActive(); } // Only create record, relationships and commit if not simulated if (!$pio->isSimulate() || $isExisting && !$pio->isIgnoreExisting()) { $product->save(); // Create any categories for the user product $catIdRoot = $this->createCategory($row[0], null); $catIdSub1 = $this->createCategory($row[1], $row[0]); $catIdSub2 = $this->createCategory($row[2], $row[1]); $this->createProductCategory($catIdRoot, $product->id); $this->createProductCategory($catIdSub1, $product->id); $this->createProductCategory($catIdSub2, $product->id); // Create any allergens for the user product if ($row[25] == 'Y') { $this->createProductAllergen(6, $product->id); } if ($row[26] == 'Y') { $this->createProductAllergen(9, $product->id); } if ($row[27] == 'Y') { $this->createProductAllergen(5, $product->id); } if ($row[28] == 'Y') { $this->createProductAllergen(4, $product->id); } if ($row[29] == 'Y') { $this->createProductAllergen(1, $product->id); } if ($row[30] == 'Y') { $this->createProductAllergen(2, $product->id); } if ($row[31] == 'Y') { $this->createProductAllergen(7, $product->id); } if ($row[32] == 'Y') { $this->createProductAllergen(8, $product->id); } if ($row[33] == 'Y') { $this->createProductAllergen(3, $product->id); } \DB::commit(); } if ($isExisting && !$pio->isIgnoreExisting()) { $recordsUpdated += 1; } else { $recordsAdded += 1; } } catch (\Exception $ex) { if (!$pio->isSimulate()) { \DB::rollBack(); } $recordsFailed += 1; } $recordCount += 1; return true; // Continue processing file } }); // Create a record of the import in the database //$productImport = new \App\Models\ProductImport(); //$productImport->user_id = $pio->getVendorId(); //$productImport->uuid = $pio->getUuid(); //$productImport->filename = $pio->getFileName(); //$productImport->save(); echo sprintf('Import complete, %s records added, %s records updated, %s records failed %s', $recordsAdded, $recordsUpdated, $recordsFailed, $pio->isSimulate() ? '(Simulated)' : ''); }