From c9bf18d87df46c3da9ec77ae211ea1539f8e9fcb Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Mon, 25 Apr 2022 21:59:53 +0200 Subject: [PATCH] fix on shelve with available offers --- app/Models/Shop/Basket.php | 2 +- app/Models/Shop/Categorizable.php | 43 ++++++++++++++++++++++++++++ app/Models/Shop/Category.php | 23 +++++++++++++++ app/Repositories/Core/Categories.php | 2 +- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 app/Models/Shop/Categorizable.php diff --git a/app/Models/Shop/Basket.php b/app/Models/Shop/Basket.php index 9a4d4651..af1b70d3 100644 --- a/app/Models/Shop/Basket.php +++ b/app/Models/Shop/Basket.php @@ -12,6 +12,6 @@ class Basket extends Model public function Offer() { - return $this->belongsTo('App\Models\Shop\Offer'); + return $this->belongsTo(Offer::class); } } diff --git a/app/Models/Shop/Categorizable.php b/app/Models/Shop/Categorizable.php new file mode 100644 index 00000000..ca1157e9 --- /dev/null +++ b/app/Models/Shop/Categorizable.php @@ -0,0 +1,43 @@ +morphTo(); + } + + public function category() + { + return $this->belongsTo(Category::class); + } + + public function articles() + { + return $this->belongsTo(Article::class)->where('categorizable_type', Article::class); + } + + public function scopeByCategory($query, $category_id) + { + return $query->where($this->table . '.category_id', $category_id); + } + + public function scopeByCategories($query, $categories_id) + { + return $query->whereIn($this->table . '.category_id', $categories_id); + } + + public function scopeByCategoryParent($query, $category_id) + { + $category = Category::find($category_id); + return $query->whereHas('category', function ($query) use ($category) { + return $query->inRange($category->_lft, $category->_rgt); + }); + } +} diff --git a/app/Models/Shop/Category.php b/app/Models/Shop/Category.php index 69292255..d8a4ae12 100644 --- a/app/Models/Shop/Category.php +++ b/app/Models/Shop/Category.php @@ -36,11 +36,21 @@ class Category extends parentCategory return $this->morphedByMany(Article::class, 'categorizable'); } + public function ArticlesByParent() + { + + } + public function ArticlesTagged() { return $this->tags->articles; } + public function categorizables() + { + return $this->hasMany(Categorizable::class); + } + public function countArticlesTagged() { return $this->tags()->withCount('Articles'); @@ -66,6 +76,11 @@ class Category extends parentCategory return $query->where('id', '<>', 1); } + public function scopeInRange($query, $_lft, $_rgt) + { + return $query->where('_lft', '>=', $_lft)->where('_rgt', '<=', $_rgt); + } + public function scopeHasAvailableOffers($query, $sale_channel_id = false) { $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); @@ -73,4 +88,12 @@ class Category extends parentCategory $query->WithAvailableOffers($sale_channel_id); }); } + + public function scopeHasAvailableOffersByCategoryParent($query, $sale_channel_id = false) + { + $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); + return $query->whereHas('articles', function ($query) use ($sale_channel_id) { + $query->WithAvailableOffers($sale_channel_id); + }); + } } diff --git a/app/Repositories/Core/Categories.php b/app/Repositories/Core/Categories.php index 47ff288f..1e18469b 100644 --- a/app/Repositories/Core/Categories.php +++ b/app/Repositories/Core/Categories.php @@ -36,7 +36,7 @@ class Categories public static function getCategoryTreeVisibles($sale_channel_id = false) { // return self::getModel()->defaultOrder()->visible()->hasAvailableOffers($sale_channel_id)->get()->toTree(); - return self::getModel()->defaultOrder()->hasAvailableOffers()->visible()->get()->toTree(); + return self::getModel()->defaultOrder()->visible()->get()->toTree(); } public static function getChildren($data, $withFolder = false)