From d44c92fb4e40ab80e7b273f4430a15e398f104b0 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Fri, 25 Aug 2023 10:49:55 +0200 Subject: [PATCH] Additional filters possibility (#326) --- config/rapidez.php | 6 ++++++ src/Models/Attribute.php | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/config/rapidez.php b/config/rapidez.php index 6b86ff619..1fa1c34a7 100644 --- a/config/rapidez.php +++ b/config/rapidez.php @@ -145,4 +145,10 @@ 'slideover' => 'z-50', ], + // From Magento only "Yes/No, Dropdown, Multiple Select and Price" attribute types + // can be configured as filter. If you'd like to have a filter for an attribute + // with, for example, the type of "Text", you can specify the attribute code here. + 'additional_filters' => [ + // Attribute codes + ], ]; diff --git a/src/Models/Attribute.php b/src/Models/Attribute.php index c79d84d33..9318ddf94 100644 --- a/src/Models/Attribute.php +++ b/src/Models/Attribute.php @@ -2,6 +2,7 @@ namespace Rapidez\Core\Models; +use Illuminate\Database\Eloquent\Casts\Attribute as CastsAttribute; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; use Rapidez\Core\Models\Scopes\Attribute\OnlyProductAttributesScope; @@ -17,6 +18,13 @@ protected static function booting() static::addGlobalScope(new OnlyProductAttributesScope); } + protected function filter(): CastsAttribute + { + return CastsAttribute::make( + get: fn ($value) => $value || in_array($this->code, config('rapidez.additional_filters')), + )->shouldCache(); + } + public static function getCachedWhere(callable $callback): array { if (! $attributes = config('cache.app.attributes.' . config('rapidez.store'))) {