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(); } }
/** * Find product in current store by id * * @param int $id * * @return \App\Model\Product */ function product($id) { if (store() === null) { return App\Models\Product::find($id); } return store()->products->find($id); }
public static function getMenu() { $category = new \App\Models\Category(); $product = new \App\Models\Product(); $categories = $category->getCategories(0); $data['categories'] = array(); foreach ($categories as $category) { $children_data = array(); if ($category['category_id'] == $data['category_id']) { $children = $category->getCategories($category['category_id']); foreach ($children as $child) { $filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true); $children_data[] = array('category_id' => $child['category_id'], 'name' => $child['name'] . (0 ? ' (' . $product->getTotalProducts($filter_data) . ')' : ''), 'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])); } } $filter_data = array('filter_category_id' => $category['category_id'], 'filter_sub_category' => true); $data['categories'][] = array('category_id' => $category['category_id'], 'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''), 'children' => $children_data, 'href' => $this->url->link('product/category', 'path=' . $category['category_id'])); } return $data; }
/** * Run the database seeds. * * @return void */ public function run() { App\Models\Product::paginate(300)->each(function ($p) { $rand = rand(1, 3); for ($i = 0; $i < $rand; $i++) { $values = factory(App\Models\Product\Price::class)->make(); $values['is_active'] = $i === 0; $p->prices()->save($values); } }); }
function getProductPopupData($lang = 'id') { $result = Cache::get('product_popup_data_' . $lang, function () use($lang) { $promo = App\Models\Product::Active()->Promo()->first(); if ($promo) { $data['photo'] = $promo->thumbnail; $data['slug'] = $promo->slug; if ($lang == 'en') { $data['product'] = $promo->name_en; $data['label'] = $promo->promo_label_en; } else { $data['product'] = $promo->name_id; $data['label'] = $promo->promo_label_id; } Cache::add('product_popup_data_' . $lang, $data, 1); return $data; } return false; }); return $result; }
/** * 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"; }
| and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('home', array('products' => App\Models\Product::all()->take(4))); }); Route::get('shop', 'PageController@shop'); Route::get('my-cart', 'PageController@myCart'); Route::post('cart/items', 'CartController@addToCart'); Route::delete('cart/items/{id}', 'CartController@removeFromCart'); Route::put('cart/items', 'CartController@changeQuantity'); Route::get('cart', 'CartController@getCart'); Route::post('cart/card', 'CartController@addCardInfo'); Route::post('address', 'AddressController@address'); Route::get('product/{id}', function ($id) { return view('product', array('product' => App\Models\Product::find($id))); }); Route::get('about', function () { return view('about', array()); }); Route::get('review', 'PageController@review'); Route::get('thank-you', 'PageController@thankYou'); $path = storage_path() . '/logs/query.log'; Event::listen('illuminate.query', function ($sql, $bindings, $time) use($path) { // Uncomment this if you want to include bindings to queries //$sql = str_replace(array('%', '?'), array('%%', '%s'), $sql); //$sql = vsprintf($sql, $bindings); $time_now = (new DateTime())->format('Y-m-d H:i:s'); $log = $time_now . ' | ' . $sql . ' | ' . $time . 'ms' . PHP_EOL; File::append($path, $log); });
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)' : ''); }