Skip to content

Commit

Permalink
migrate from enterprise: add spu_code, import products with attribute…
Browse files Browse the repository at this point in the history
…s and categories, fixed shipping pringting, fixed TDK for product detail page.
  • Loading branch information
yushine committed Jan 9, 2025
1 parent 9157eca commit 34ba2a8
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ public function up(): void
$table->unsignedInteger('product_video_id')->default(0)->index('p_pv_id')->comment('Video ID');
$table->unsignedInteger('product_sku_id')->default(0)->index('p_ps_id')->comment('SKU ID');
$table->unsignedInteger('tax_class_id')->default(0)->index('p_tc_id')->comment('Tax Class ID');
$table->string('spu_code', 128)->nullable()->unique()->comment('SPU Code');
$table->string('slug', 128)->nullable()->unique()->comment('URL Slug');
$table->json('variables')->nullable()->comment('Product variables for sku with variants');
$table->boolean('is_virtual')->default(false)->comment('Is Virtual');
Expand Down
10 changes: 10 additions & 0 deletions innopacks/common/src/Models/Attribute/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace InnoShop\Common\Models\Attribute;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use InnoShop\Common\Models\Attribute;
use InnoShop\Common\Models\BaseModel;

class Translation extends BaseModel
Expand All @@ -18,4 +20,12 @@ class Translation extends BaseModel
protected $fillable = [
'locale', 'name',
];

/**
* @return BelongsTo
*/
public function attribute(): BelongsTo
{
return $this->belongsTo(Attribute::class, 'attribute_id');
}
}
10 changes: 10 additions & 0 deletions innopacks/common/src/Models/Category/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace InnoShop\Common\Models\Category;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use InnoShop\Common\Models\BaseModel;
use InnoShop\Common\Models\Category;

class Translation extends BaseModel
{
Expand All @@ -18,4 +20,12 @@ class Translation extends BaseModel
protected $fillable = [
'category_id', 'locale', 'name', 'content', 'meta_title', 'meta_description', 'meta_keywords',
];

/**
* @return BelongsTo
*/
public function category(): BelongsTo
{
return $this->belongsTo(Category::class, 'category_id');
}
}
4 changes: 2 additions & 2 deletions innopacks/common/src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Product extends BaseModel
use HasPackageFactory, Replicate, Translatable;

protected $fillable = [
'brand_id', 'product_image_id', 'product_video_id', 'product_sku_id', 'tax_class_id', 'slug', 'is_virtual',
'variables', 'position', 'active', 'weight', 'weight_class', 'sales', 'viewed',
'brand_id', 'product_image_id', 'product_video_id', 'product_sku_id', 'tax_class_id', 'spu_code', 'slug',
'is_virtual', 'variables', 'position', 'active', 'weight', 'weight_class', 'sales', 'viewed',
];

protected $casts = [
Expand Down
51 changes: 48 additions & 3 deletions innopacks/common/src/Repositories/Attribute/ValueRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace InnoShop\Common\Repositories\Attribute;

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use InnoShop\Common\Models\Attribute\Value;
Expand All @@ -21,15 +22,17 @@ class ValueRepo extends BaseRepo
/**
* @param $attributeID
* @param $translations
* @return void
* @return mixed
*/
public function createAttribute($attributeID, $translations): void
public function createAttribute($attributeID, $translations): mixed
{
if (empty($attributeID) || empty($translations)) {
return;
return null;
}
$attrValue = Value::query()->create(['attribute_id' => $attributeID]);
$this->updateTranslations($attrValue, $translations);

return $attrValue;
}

/**
Expand Down Expand Up @@ -70,6 +73,48 @@ public function all(array $filters = []): Collection
return $this->builder($filters)->get();
}

/**
* @param $name
* @param string $locale
* @return mixed
* @throws Exception
*/
public function findByName($name, string $locale = ''): mixed
{
if (empty($locale)) {
$locale = locale_code();
}

$translation = Value\Translation::query()
->where('name', $name)
->where('locale', $locale)
->first();

return $translation->value ?? null;
}

/**
* @param $attributeRow
* @param $name
* @param string $locale
* @return mixed
* @throws Exception
*/
public function findOrCreateByName($attributeRow, $name, string $locale = ''): mixed
{
$attributeValue = $this->findByName($name, $locale);
if ($attributeValue) {
return $attributeValue;
}

$data = [];
foreach (locales() as $locale) {
$data[$locale->code] = $name;
}

return $this->createAttribute($attributeRow->id, $data);
}

/**
* @param array $filters
* @return Builder
Expand Down
41 changes: 41 additions & 0 deletions innopacks/common/src/Repositories/AttributeRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@ public function all(array $filters = []): Collection
return $this->builder($filters)->get();
}

/**
* @param $name
* @param string $locale
* @return mixed
* @throws Exception
*/
public function findByName($name, string $locale = ''): mixed
{
if (empty($locale)) {
$locale = locale_code();
}

$translation = Attribute\Translation::query()->where('name', $name)->where('locale', $locale)->first();

return $translation->attribute ?? null;
}

/**
* @param $name
* @param string $locale
* @return mixed
* @throws Throwable
*/
public function findOrCreateByName($name, string $locale = ''): mixed
{
$attribute = $this->findByName($name, $locale);
if ($attribute) {
return $attribute;
}

$data = [];
foreach (locales() as $locale) {
$data['translations'][] = [
'locale' => $locale->code,
'name' => $name,
];
}

return $this->create($data);
}

/**
* @param array $filters
* @return Builder
Expand Down
52 changes: 47 additions & 5 deletions innopacks/common/src/Repositories/CategoryRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace InnoShop\Common\Repositories;

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use InnoShop\Common\Models\Category;
Expand Down Expand Up @@ -161,7 +162,7 @@ private function createOrUpdate(Category $category, $data): void
$category->translations()->createMany($translations);

DB::commit();
} catch (\Exception $e) {
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
Expand All @@ -175,10 +176,10 @@ private function handleCategoryData($data): array
{
return [
'parent_id' => $data['parent_id'] ?? 0,
'slug' => $data['slug'],
'image' => $data['image'] ?? '',
'position' => $data['position'] ?? 0,
'active' => $data['active'] ?? true,
'slug' => $data['slug'] ?? null,
'image' => $data['image'] ?? '',
'position' => $data['position'] ?? 0,
'active' => $data['active'] ?? true,
];
}

Expand Down Expand Up @@ -280,4 +281,45 @@ public function getNameByID($id): string
{
return Category::query()->find($id)->description->name ?? '';
}

/**
* @param $name
* @param string $locale
* @return mixed
* @throws Exception
*/
public function findByName($name, string $locale = ''): mixed
{
if (empty($locale)) {
$locale = locale_code();
}

$translation = Category\Translation::query()->where('name', $name)->where('locale', $locale)->first();

return $translation->category ?? null;
}

/**
* @param $name
* @param string $locale
* @return mixed
* @throws Throwable
*/
public function findOrCreateByName($name, string $locale = ''): mixed
{
$category = $this->findByName($name, $locale);
if ($category) {
return $category;
}

$data = [];
foreach (locales() as $locale) {
$data['translations'][] = [
'locale' => $locale->code,
'name' => $name,
];
}

return $this->create($data);
}
}
27 changes: 27 additions & 0 deletions innopacks/common/src/Repositories/ProductRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public function handleProductData($data): array
}

return [
'spu_code' => $data['spu_code'],
'slug' => $data['slug'],
'brand_id' => $data['brand_id'] ?? 0,
'product_image_id' => $data['product_image_id'] ?? 0,
Expand Down Expand Up @@ -556,6 +557,32 @@ public function getListByProductIDs(mixed $productIDs): mixed
->get();
}

/**
* @param $spuCode
* @return ?Product
*/
public function findBySpuCode($spuCode): ?Product
{
if (empty($spuCode)) {
return null;
}

return Product::query()->where('spu_code', $spuCode)->first();
}

/**
* @param $slug
* @return ?Product
*/
public function findBySlug($slug): ?Product
{
if (empty($spuCode)) {
return null;
}

return Product::query()->where('slug', $slug)->first();
}

/**
* @param $keyword
* @param int $limit
Expand Down
8 changes: 4 additions & 4 deletions innopacks/common/src/Services/CheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

class CheckoutService extends BaseService
{
private int $customerID;
protected int $customerID;

private string $guestID;
protected string $guestID;

private array $cartList = [];
protected array $cartList = [];

private array $addressList = [];

Expand Down Expand Up @@ -78,7 +78,7 @@ public function __construct(int $customerID = 0, string $guestID = '')
*/
public static function getInstance(int $customerID = 0, string $guestID = ''): static
{
return new self($customerID, $guestID);
return new static($customerID, $guestID);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions innopacks/front/resources/views/products/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
@extends('layouts.app')
@section('body-class', 'page-product')

@if($product->translation->meta_title ?? '')
@section('title', $product->translation->meta_title ?? '')
@endif

@if($product->translation->meta_description ?? '')
@section('description', $product->translation->meta_description ?? '')
@endif

@if($product->translation->meta_keywords ?? '')
@section('keywords', $product->translation->meta_keywords ?? '')
@endif


@push('header')
<script src="{{ asset('vendor/swiper/swiper-bundle.min.js') }}"></script>
<link rel="stylesheet" href="{{ asset('vendor/swiper/swiper-bundle.min.css') }}">
Expand Down
Loading

0 comments on commit 34ba2a8

Please sign in to comment.