diff --git a/database/seeders/ProductSeeder.php b/database/seeders/ProductSeeder.php index cd72d153..b1b7c4f6 100644 --- a/database/seeders/ProductSeeder.php +++ b/database/seeders/ProductSeeder.php @@ -38,6 +38,7 @@ private function getProducts(): array return [ [ 'brand_id' => 1, + 'spu_code' => 'galaxy-glow-evening-gown', 'slug' => 'galaxy-glow-evening-gown', 'translations' => [ [ @@ -122,6 +123,7 @@ private function getProducts(): array ], [ 'brand_id' => 1, + 'spu_code' => 'urban-elite-suit-jacket', 'slug' => 'urban-elite-suit-jacket', 'translations' => [ [ diff --git a/innopacks/common/src/Libraries/MetaInfo.php b/innopacks/common/src/Libraries/MetaInfo.php new file mode 100644 index 00000000..811e1099 --- /dev/null +++ b/innopacks/common/src/Libraries/MetaInfo.php @@ -0,0 +1,100 @@ + + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +namespace InnoShop\Common\Libraries; + +use Illuminate\Support\Str; + +class MetaInfo +{ + private object $object; + + private string $type; + + /** + * @param $object + */ + public function __construct($object) + { + $this->object = $object; + $this->setType(); + } + + public static function getInstance($object): MetaInfo + { + return new self($object); + } + + /** + * @return MetaInfo + */ + public function setType(): static + { + $this->type = Str::lower(class_basename($this->object)); + + return $this; + } + + /** + * @return string + */ + public function getTitle(): string + { + $metaTitle = $this->object->translation->meta_title ?? ''; + if ($metaTitle) { + return $metaTitle; + } + + return $this->getName(); + } + + /** + * @return string + */ + public function getDescription(): string + { + $metaDescription = $this->object->translation->meta_description ?? ''; + if ($metaDescription) { + return $metaDescription; + } + + return $this->getName(); + } + + /** + * @return string + */ + public function getKeywords(): string + { + $metaKeywords = $this->object->translation->meta_keywords ?? ''; + if ($metaKeywords) { + return $metaKeywords; + } + + return $this->getName(); + } + + /** + * @return string + */ + public function getName(): string + { + $object = $this->object; + $type = $this->type; + if (in_array($type, ['category', 'product', 'tag'])) { + return $object->fallbackName('name'); + } elseif (in_array($type, ['catalog', 'article', 'page'])) { + return $object->fallbackName('title'); + } elseif ($type == 'brand') { + return $object->name; + } + + return ''; + } +} diff --git a/innopacks/common/src/Repositories/ProductRepo.php b/innopacks/common/src/Repositories/ProductRepo.php index bd0566ef..940ec662 100644 --- a/innopacks/common/src/Repositories/ProductRepo.php +++ b/innopacks/common/src/Repositories/ProductRepo.php @@ -231,8 +231,8 @@ public function handleProductData($data): array } return [ - 'spu_code' => $data['spu_code'], - 'slug' => $data['slug'], + 'spu_code' => $data['spu_code'] ?? null, + 'slug' => $data['slug'] ?? null, 'brand_id' => $data['brand_id'] ?? 0, 'product_image_id' => $data['product_image_id'] ?? 0, 'product_video_id' => $data['product_video_id'] ?? 0, diff --git a/innopacks/front/resources/views/articles/show.blade.php b/innopacks/front/resources/views/articles/show.blade.php index b8b49f42..23846046 100644 --- a/innopacks/front/resources/views/articles/show.blade.php +++ b/innopacks/front/resources/views/articles/show.blade.php @@ -1,7 +1,10 @@ @extends('layouts.app') - @section('body-class', 'page-news-details') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getKeywords()) + @section('content') diff --git a/innopacks/front/resources/views/brands/show.blade.php b/innopacks/front/resources/views/brands/show.blade.php index 33ad30f9..91e256da 100644 --- a/innopacks/front/resources/views/brands/show.blade.php +++ b/innopacks/front/resources/views/brands/show.blade.php @@ -1,6 +1,10 @@ @extends('layouts.app') @section('body-class', 'page-categories') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getKeywords()) + @section('content') diff --git a/innopacks/front/resources/views/catalogs/show.blade.php b/innopacks/front/resources/views/catalogs/show.blade.php index 438b3c06..9b19b7c2 100644 --- a/innopacks/front/resources/views/catalogs/show.blade.php +++ b/innopacks/front/resources/views/catalogs/show.blade.php @@ -1,7 +1,10 @@ @extends('layouts.app') - @section('body-class', 'page-news') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getKeywords()) + @section('content') diff --git a/innopacks/front/resources/views/categories/show.blade.php b/innopacks/front/resources/views/categories/show.blade.php index 01408e07..0b27cfcf 100644 --- a/innopacks/front/resources/views/categories/show.blade.php +++ b/innopacks/front/resources/views/categories/show.blade.php @@ -1,6 +1,10 @@ @extends('layouts.app') @section('body-class', 'page-categories') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getKeywords()) + @section('content') diff --git a/innopacks/front/resources/views/pages/show.blade.php b/innopacks/front/resources/views/pages/show.blade.php index ec38d462..6749f6e6 100644 --- a/innopacks/front/resources/views/pages/show.blade.php +++ b/innopacks/front/resources/views/pages/show.blade.php @@ -1,5 +1,9 @@ @extends('layouts.app') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getKeywords()) + @section('content') @if($page->show_breadcrumb) diff --git a/innopacks/front/resources/views/products/show.blade.php b/innopacks/front/resources/views/products/show.blade.php index 7fcb10b5..90531847 100644 --- a/innopacks/front/resources/views/products/show.blade.php +++ b/innopacks/front/resources/views/products/show.blade.php @@ -1,18 +1,9 @@ @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 - +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getKeywords()) @push('header') diff --git a/innopacks/front/resources/views/tags/show.blade.php b/innopacks/front/resources/views/tags/show.blade.php index 9474be04..9d9e66d3 100644 --- a/innopacks/front/resources/views/tags/show.blade.php +++ b/innopacks/front/resources/views/tags/show.blade.php @@ -1,7 +1,10 @@ @extends('layouts.app') - @section('body-class', 'page-news') +@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getTitle()) +@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getDescription()) +@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getKeywords()) + @section('content')