код из routes.php
миграция из плагина MALL в плагин Shopaholic
товары. офферы, бренды, категории
Перед использованием обязательно сделать бекап базы данных.
Для запуска переходим по url /updateFromMollToShopaholic
Если товаров много то стоит увеличить время выполнения скрипта в php и объем оперативки
<?php
use Lovata\Shopaholic\Models\{
Brand as ShopaholicBrand,
Category as ShopaholicCategory,
Product as ShopaholicProduct};
use OFFLINE\Mall\Models\{
Brand as MallBrand,
Category as MallCategory,
Product as MallProduct
};
use Illuminate\Support\Collection;
Route::get('updateFromMollToShopaholic', getUpdateFromMollClosure());
function getUpdateFromMollClosure() {
return function () {
$ids = ShopaholicCategory::all()->lists('id');
ShopaholicCategory::destroy($ids);
$ids = ShopaholicBrand::all()->lists('id');
ShopaholicBrand::destroy($ids);
$ids = ShopaholicProduct::all()->lists('id');
ShopaholicProduct::destroy($ids);
MallCategory::getAllRoot()->each(function(MallCategory $category) {
convertCategoryToShopaholic($category);
});
MallBrand::all()->each(function (MallBrand $brand) {
convertBrandToShopaholic($brand);
});
MallProduct::all()->each(function (MallProduct $product) {
convertProductToShopaholic($product);
});
};
}
function convertProductToShopaholic(MallProduct $product) {
$slug = $product->slug;
$usedProducts = ShopaholicProduct::where('slug', $slug)->get();
if ($usedProducts->isNotEmpty()) {
$slug .= random_int(0,50);
$product->slug = $slug;
$product->save();
}
$shopaholicProduct = new ShopaholicProduct();
$shopaholicProduct->active = $product->published;
$shopaholicProduct->name = $product->name;
$shopaholicProduct->slug = $product->slug;
$shopaholicProduct->code = (string)$product->user_defined_id;
$shopaholicProduct->preview_text = $product->description_short;
$shopaholicProduct->description = $product->description;
$shopaholicProduct->save();
if ($product->brand != null) {
$brand = ShopaholicBrand::where('slug', $product->brand->slug)->first();
if ($brand != null) {
$shopaholicProduct->brand = $brand;
}
}
/**
* @var Collection
*/
$mallCategories = $product->categories;
if ($mallCategories->isNotEmpty()) {
$mainCategory = $mallCategories->shift();
$shopaholicMainCategory = ShopaholicCategory::where('slug', $mainCategory->slug)->first();
if ($shopaholicMainCategory != null) {
$shopaholicProduct->category = $shopaholicMainCategory;
}
$mallCategories->each(function (MallCategory $category) use($shopaholicProduct) {
$shopaholicCategory = ShopaholicCategory::where('slug', $category->slug)->first();
if ($shopaholicCategory != null) {
$shopaholicProduct->additional_category()->attach($shopaholicCategory);
}
});
}
$images = $product->image_sets;
if ($images->isNotEmpty()) {
$images->each(function(\OFFLINE\Mall\Models\ImageSet $imageSet) use ($shopaholicProduct) {
$imagesOfImageSet = $imageSet->images;
if ($imagesOfImageSet->isNotEmpty()) {
$preview_image = $imagesOfImageSet->first();
$shopaholicProduct->preview_image = $preview_image;
// $imagesOfImageSet->each(function($image) use($shopaholicProduct) {
// $shopaholicProduct->images()->attach($image);
// });
}
});
}
$shopaholicProduct->save();
$price = $product->prices->first();
if ($price != null) {
createOffer($shopaholicProduct, $product, $price);
}
}
function createOffer(ShopaholicProduct $product, MallProduct $mallProduct, \OFFLINE\Mall\Models\ProductPrice $price)
{
$attributes = [
'active' => true,
'name' => $product->name,
'code' => $product->code,
'product_id' => $product->id,
'price' => $price->float,
'weight' => $mallProduct->weight,
'quantity' => $mallProduct->stock,
];
\Lovata\Shopaholic\Models\Offer::create($attributes);
}
function convertBrandToShopaholic(MallBrand $brand) {
$attributes = [
'active' => true,
'name' => $brand->name,
'slug' => $brand->slug,
'description' => $brand->description,
'website' => $brand->website,
];
$shopaholicBrand = ShopaholicBrand::create($attributes);
$shopaholicBrand->preview_image = $brand->logo;
$shopaholicBrand->save();
}
/**
* @param MallCategory $category
* @return ShopaholicCategory
*/
function convertCategoryToShopaholic(MallCategory $category) {
$attributes = [
'active' => true,
'name' => $category->name,
'slug' => $category->slug,
'code' => $category->code,
'description' => $category->description
];
$usedCategories = ShopaholicCategory::where('slug', $category->slug)->get();
if ($usedCategories->isNotEmpty()) {
$attributes['slug'] = $category->slug . random_int(0,50);
$category->slug = $attributes['slug'];
$category->save();
}
$shopaholicCategory = ShopaholicCategory::create($attributes);
appendSubCategories($shopaholicCategory, $category);
$shopaholicCategory->preview_image = $category->image;
if ($category->image != null) {
$shopaholicCategory->images()->add($category->image);
}
$shopaholicCategory->save();
return $shopaholicCategory;
}
function appendSubCategories(ShopaholicCategory $shopaholicCategory, MallCategory $mallCategory) {
$shopaholicSubCategories = $mallCategory->getChildren()
->map(function (MallCategory $category) {
return convertCategoryToShopaholic($category);
})
->filter(function (?ShopaholicCategory $category) {
return $category != null;
});
$shopaholicCategory->children()->addMany($shopaholicSubCategories);
}