diff --git a/app/Http/Controllers/Shop/CategoryController.php b/app/Http/Controllers/Shop/CategoryController.php index cb6e7eec..cb755cef 100644 --- a/app/Http/Controllers/Shop/CategoryController.php +++ b/app/Http/Controllers/Shop/CategoryController.php @@ -62,6 +62,12 @@ class CategoryController extends Controller 'display_by_rows' => $request->input('display_by_rows') ?? false, 'product_type' => $product_type, 'article_nature' => $article_nature, + 'article_natures' => Articles::getArticleNaturesWithOffers([ + 'category_id' => $category_id, + ]), + 'product_types' => Articles::getProductTypesWithOffers([ + 'category_id' => $category_id, + ]), 'tags_selected' => $request->input('tags') ?? [], 'articles' => Articles::getArticlesToSell([ 'category_id' => $category_id, @@ -71,8 +77,6 @@ class CategoryController extends Controller ]), 'tags' => TagGroups::getWithTagsAndCountOffers($category_id), ]; - // dump($data); - // exit; return view('Shop.Shelves.shelve', $data); } diff --git a/app/Models/Shop/ArticleNature.php b/app/Models/Shop/ArticleNature.php index 4bcf8048..73c86f82 100644 --- a/app/Models/Shop/ArticleNature.php +++ b/app/Models/Shop/ArticleNature.php @@ -39,4 +39,9 @@ class ArticleNature extends Model { return $query->where($this->table . '.product_type', $type); } + + public function scopeByIds($query, $ids) + { + return $query->whereIn($this->table . '.id', $ids); + } } diff --git a/app/Repositories/Shop/ArticleNatures.php b/app/Repositories/Shop/ArticleNatures.php index 5d4fbe12..6dbe7041 100644 --- a/app/Repositories/Shop/ArticleNatures.php +++ b/app/Repositories/Shop/ArticleNatures.php @@ -21,6 +21,11 @@ class ArticleNatures return $type ? self::getProductTypes()[$type] : false; } + public static function getProductTypeNameByClass($model) + { + return self::getProductTypeName(self::getProductTypeByModel($model)); + } + public static function getProductTypeName($type) { return self::getProductTypes()[$type] ?? false; @@ -30,16 +35,12 @@ class ArticleNatures { switch ($model) { case Specie::class: - $type = 1; - break; case Variety::class: - $type = 1; - break; + return 1; case Merchandise::class: - $type = 2; - break; + return 2; } - return $type ?? false; + return false; } public static function getProductTypes() @@ -83,6 +84,12 @@ class ArticleNatures return ArticleNature::orderBy('name', 'asc')->get(); } + public static function getNamesByIds($ids) + { + $names = ArticleNature::byIds($ids)->pluck('name')->toArray(); + return array_map('strtolower', $names); + } + public static function get($id) { return ArticleNature::find($id); diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index c7fa9e64..9f8df5a1 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -194,7 +194,8 @@ class Articles { return [ 'id' => $article->id, - 'description' => (!empty($article->description)) ? $article->description : $article->product->description, + 'article_nature_id' => $article->article_nature_id, + 'description' => $article->description ? $article->description : $article->product->description, 'image' => self::getFullImageByArticle($article), 'product_type' => $article->product_type, 'product_id' => $article->product_id, @@ -238,6 +239,38 @@ class Articles return $data; } + public static function getArticleNaturesWithOffers($options = false) + { + return ArticleNatures::getNamesByIds(self::getArticleNaturesIdsWithOffers($options)); + } + + public static function getArticleNaturesIdsWithOffers($options = false) + { + $sale_channel_id = $options['sale_channel_id'] ?? SaleChannels::getDefaultID(); + $model = self::getModelByOptions($options); + $data = $model->withAvailableOffers($sale_channel_id)->get()->pluck('article_nature_id')->unique(); + return array_values($data->toArray()); + } + + public static function getProductTypesWithOffers($options = false) + { + $data = []; + $classes = self::getProductTypesClassesWithOffers($options); + foreach ($classes as $class) { + $type = ArticleNatures::getProductTypeNameByClass($class); + $data[$type] = true; + } + return array_keys($data); + } + + public static function getProductTypesClassesWithOffers($options = false) + { + $sale_channel_id = $options['sale_channel_id'] ?? SaleChannels::getDefaultID(); + $model = self::getModelByOptions($options); + $data = $model->withAvailableOffers($sale_channel_id)->get()->pluck('product_type')->unique(); + return $data->toArray(); + } + public static function getModelByOptions($options = false) { $category_id = $options['category_id'] ?? false;