open($file); foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $num => $row) { if ($num === 1) { continue; } $cells = $row->toArray(); $index = 0; $storeName = $cells[$index];// 店铺名称 $goodsName = $cells[++$index];// 商品名称 $goodsTitle = $cells[++$index];// 商品副标题 $categoryName = $cells[++$index];// 分类名称 $goodsImage = $cells[++$index];// 商品图片 $salePrice = $cells[++$index];// 销售价 $marketPrice = $cells[++$index];// 市场价 $costPrice = $cells[++$index];// 成本价 $vipPrice = $cells[++$index];// 会员价 $saleValue = $cells[++$index];// 销售值 // 属性加价 // 颜色[远峰蓝:0,石墨色:0,银色:0,金色:0,苍岭绿色:0] $rowAttrs = $cells[++$index]; // 规格展示 // 主体[上市月份:9月,品牌:Apple] $rowSpecs = $cells[++$index]; $shipTemplate = $cells[++$index];// 运费模板 $goodsWeight = $cells[++$index];// 商品重量(克) // 详细描述 // https://qiniu.abcdefg.fun/banner/banner4.png,https://qiniu.abcdefg.fun/banner/banner5.png $goodsContent = $cells[++$index]; $stock = $cells[++$index];// 库存 $store = Store::where(['title' => $storeName])->firstOrFail(); $goods = [ 'name' => $goodsName, 'subtitle' => $goodsTitle, 'shipping_template_id' => 1, ]; $category = ProductCategory::where('name', $categoryName)->firstOrFail(); $goods['category_id'] = $category->id; // 图片组 if ($goodsImage) { if (Str::startsWith($goodsImage, ['http://', 'https://'])) { $images = explode(',', $goodsImage); } else { $images = $this->getImageUrlFromPath($goodsImage); } if (count($images) > 0) { $goods['cover'] = $images[0]; $goods['images'] = $images; } } // 运费模板 if ($shipTemplate) { $goods['shipping_template_id'] = $shipTemplate; } // 重量 if ($goodsWeight) { $goods['weight'] = $goodsWeight; } // 详细描述 if ($goodsContent) { $description = ''; foreach(explode(',', $goodsContent) as $item) { $description .= '

'; } $goods['description'] = $description; } $goods = array_merge($goods, [ 'sell_price' => $salePrice * 100, 'market_price' => $marketPrice * 100, 'cost_price' => $costPrice * 100, 'vip_price' => $vipPrice * 100, 'sales_value' => $saleValue, 'stock' => $stock, ]); $product = ProductSpu::updateOrCreate(['name' => $goods['name']],$goods); // 属性规格 if ($rowAttrs) { $format_specs = $this->formatAttr($rowAttrs); $format_attrs = $this->formatAttr($rowSpecs); $spec_group = $this->getSpec($cells[0], $rowAttrs, $rowSpecs); $attrs = []; foreach($spec_group->attrs as $index => $item) { $attr = ['name' => $item['title'], 'attrs' => []]; foreach(explode(PHP_EOL, $item['value']) as $value) { array_push($attr['attrs'], ['name' => $value, 'value' => data_get($format_attrs, "$index.values.$value")]); } array_push($attrs, $attr); } $product->update(['attrs' => $attrs]); $specs = []; $product->specs()->delete(); foreach($spec_group->specs as $index => $item) { $spec = ['name' => $item['title'], 'items' => []]; foreach(explode(PHP_EOL, $item['value']) as $value) { array_push($spec['items'], ['name' => $value, 'value' => data_get($format_specs, "$index.values.$value")]); } array_push($specs, $spec); } $product->specs()->createMany($specs); } // 清空现有的sku, 重新添加 StoreProductSku::where('store_id', $store->id)->whereIn('product_sku_id', $product->skus->pluck('id'))->delete(); $product->skus()->delete(); ProductSku::createBySpu($product); // 将sku 添加到门店 $skuList = $product->skus()->get(); $skus = []; foreach($skuList as $sku) { array_push($skus, ['amount' => $stock, 'product_sku_id' => $sku->id, 'status' => 1, 'store_id' => $store->id]); } StoreProductSku::insert($skus); } } $reader->close(); } protected function getImageUrlFromPath($dir) { $disk = Storage::disk(); $files = $disk->files($dir); $images = []; foreach($files as $filename) { array_push($images, $disk->url($filename)); } return $images; } protected function getSpec($name, $specs, $attrs) { if (Str::contains($specs, ':')) { // 创建新的规格 $spec = ProductGroup::updateOrCreate([ 'name' => $name ], [ 'attrs' => $this->formatGroupAttr($this->formatAttr($attrs)), 'specs' => $this->formatGroupAttr($this->formatAttr($specs)) ]); } else { $spec = ProductGroup::where('name', $specs)->firstOrFail(); } return $spec; } /** * 颜色[灰色:0,银色:0] * 尺寸[13寸:0,14寸:500,16寸:1000] * 处理器[M1:0,M1 Pro:1000,M1 Max:1500] * 内存[8G:0,16G:1000,32G:2000] * 硬盘[256G:0,512G:1000,1TB:2000] * * [ * ['name' => '颜色', 'values' => ['灰色' => 0, '银色' => 0]], * ['name' => '尺寸', 'values' => ['13寸' => 0, '14寸' => 500, '16寸' => '1000']] * ... * ] */ protected function formatAttr($attrs) { $attr_data = []; if (!Str::contains($attrs, ':')) { return $attr_data; } foreach(explode("\n", $attrs) as $item) { $name = explode('[', $item)[0]; $attr_values = []; foreach(explode(',', Str::between($item, '[', ']')) as $item1) { $item1_explode = explode(':', $item1); $attr_values[$item1_explode[0]] = $item1_explode[1]; } array_push($attr_data, ['name' => $name, 'values' => $attr_values]); } return $attr_data; } protected function formatGroupAttr($data) { $list = []; foreach($data as $item) { array_push($list, ['title' => $item['name'], 'value' => implode(PHP_EOL, array_keys($item['values']))]); } return $list; } }