From e09e9f2845f02719b8cf8237cb0599a4d02adf84 Mon Sep 17 00:00:00 2001 From: Devansh Date: Fri, 28 Jan 2022 16:14:07 +0530 Subject: [PATCH 1/2] Layered Navigation Without Category --- .../Http/Controllers/ProductController.php | 29 +++++------ .../list/layered-navigation.blade.php | 48 ++++++++++++++----- .../Shop/src/Routes/store-front-routes.php | 20 ++++---- .../list/layered-navigation.blade.php | 35 ++++++++++---- 4 files changed, 88 insertions(+), 44 deletions(-) diff --git a/packages/Webkul/Shop/src/Http/Controllers/ProductController.php b/packages/Webkul/Shop/src/Http/Controllers/ProductController.php index 6d46e9427..b9372082f 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/ProductController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/ProductController.php @@ -3,6 +3,7 @@ namespace Webkul\Shop\Http\Controllers; use Illuminate\Support\Facades\Storage; +use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Category\Repositories\CategoryRepository; use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Repositories\ProductDownloadableLinkRepository; @@ -158,18 +159,20 @@ class ProductController extends Controller * * @return \Illuminate\Http\Response */ - public function getFilterAttributes($categoryId) + public function getFilterAttributes($categoryId = null, AttributeRepository $attributeRepository) { - $category = $this->categoryRepository->find($categoryId); + $filterAttributes = []; - if ($category) { - return response()->json([ - 'filter_attributes' => $this->productFlatRepository->getFilterAttributes($category) - ]); + if ($category = $this->categoryRepository->find($categoryId)) { + $filterAttributes = $this->productFlatRepository->getFilterAttributes($category); + } + + if (! count($filterAttributes) > 0) { + $filterAttributes = $attributeRepository->getFilterAttributes(); } return response()->json([ - 'filter_attributes' => [] + 'filter_attributes' => $filterAttributes, ]); } @@ -178,18 +181,16 @@ class ProductController extends Controller * * @return \Illuminate\Http\Response */ - public function getCategoryProductMaximumPrice($categoryId) + public function getCategoryProductMaximumPrice($categoryId = null) { - $category = $this->categoryRepository->find($categoryId); + $maxPrice = 0; - if ($category) { - return response()->json([ - 'max_price' => $this->productFlatRepository->handleCategoryProductMaximumPrice($category) - ]); + if ($category = $this->categoryRepository->find($categoryId)) { + $maxPrice = $this->productFlatRepository->handleCategoryProductMaximumPrice($category); } return response()->json([ - 'max_price' => 0 + 'max_price' => $maxPrice, ]); } } diff --git a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php index 3447ea270..3d4bebaab 100755 --- a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php @@ -1,7 +1,17 @@
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!} - + @if (isset($category)) + + + @else + + + @endif {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
@@ -21,6 +31,7 @@ :index="index" :attribute="attribute" :appliedFilterValues="appliedFilters[attribute.code]" + :max-price-src="maxPriceSrc" @onFilterAdded="addFilters(attribute.code, $event)"> @@ -46,7 +57,12 @@
  1. - + @@ -74,11 +90,16 @@ Vue.component('layered-navigation', { template: '#layered-navigation-template', + props: [ + 'attributeSrc', + 'maxPriceSrc', + ], + data: function() { return { appliedFilters: {}, attributes: [], - } + }; }, created: function () { @@ -90,7 +111,7 @@ methods: { setFilterAttributes: function () { axios - .get('{{ route('admin.catalog.products.get-filter-attributes', $category->id) }}') + .get(this.attributeSrc) .then((response) => { this.attributes = response.data.filter_attributes; }); @@ -111,7 +132,7 @@ delete this.appliedFilters[attributeCode]; } - this.applyFilter() + this.applyFilter(); }, applyFilter: function () { @@ -119,7 +140,7 @@ for(key in this.appliedFilters) { if (key != 'page') { - params.push(key + '=' + this.appliedFilters[key].join(',')) + params.push(key + '=' + this.appliedFilters[key].join(',')); } } @@ -131,7 +152,12 @@ Vue.component('filter-attribute-item', { template: '#filter-attribute-item-template', - props: ['index', 'attribute', 'appliedFilterValues'], + props: [ + 'index', + 'attribute', + 'appliedFilterValues', + 'maxPriceSrc', + ], data: function() { return { @@ -170,7 +196,7 @@ methods: { setMaxPrice: function () { axios - .get('{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id) }}') + .get(this.maxPriceSrc) .then((response) => { let maxPrice = response.data.max_price; @@ -179,13 +205,13 @@ }, addFilter: function (e) { - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); }, priceRangeUpdated: function (value) { this.appliedFilters = value; - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); }, clearFilters: function () { @@ -195,7 +221,7 @@ this.appliedFilters = []; - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); } } }); diff --git a/packages/Webkul/Shop/src/Routes/store-front-routes.php b/packages/Webkul/Shop/src/Routes/store-front-routes.php index 7e6e93172..369f24361 100644 --- a/packages/Webkul/Shop/src/Routes/store-front-routes.php +++ b/packages/Webkul/Shop/src/Routes/store-front-routes.php @@ -29,8 +29,8 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function */ Route::fallback(\Webkul\Shop\Http\Controllers\ProductsCategoriesProxyController::class . '@index') ->defaults('_config', [ - 'product_view' => 'shop::products.view', - 'category_view' => 'shop::products.index' + 'product_view' => 'shop::products.view', + 'category_view' => 'shop::products.index', ]) ->name('shop.productOrCategory.index'); }); @@ -39,14 +39,14 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function * Store front home. */ Route::get('/', [HomeController::class, 'index'])->defaults('_config', [ - 'view' => 'shop::home.index' + 'view' => 'shop::home.index', ])->name('shop.home.index'); /** * Store front search. */ Route::get('/search', [SearchController::class, 'index'])->defaults('_config', [ - 'view' => 'shop::search.search' + 'view' => 'shop::search.search', ])->name('shop.search.index'); Route::post('/upload-search-image', [HomeController::class, 'upload'])->name('shop.image.search.upload'); @@ -62,24 +62,24 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function * Product and categories routes. */ Route::get('/reviews/{slug}', [ReviewController::class, 'show'])->defaults('_config', [ - 'view' => 'shop::products.reviews.index' + 'view' => 'shop::products.reviews.index', ])->name('shop.reviews.index'); Route::get('/product/{slug}/review', [ReviewController::class, 'create'])->defaults('_config', [ - 'view' => 'shop::products.reviews.create' + 'view' => 'shop::products.reviews.create', ])->name('shop.reviews.create'); Route::post('/product/{slug}/review', [ReviewController::class, 'store'])->defaults('_config', [ - 'redirect' => 'shop.home.index' + 'redirect' => 'shop.home.index', ])->name('shop.reviews.store'); Route::get('/downloadable/download-sample/{type}/{id}', [ProductController::class, 'downloadSample'])->name('shop.downloadable.download_sample'); Route::get('/product/{id}/{attribute_id}', [ProductController::class, 'download'])->defaults('_config', [ - 'view' => 'shop.products.index' + 'view' => 'shop.products.index', ])->name('shop.product.file.download'); - Route::get('products/get-filter-attributes/{categoryId}', [ProductController::class, 'getFilterAttributes'])->name('admin.catalog.products.get-filter-attributes'); + Route::get('products/get-filter-attributes/{categoryId?}', [ProductController::class, 'getFilterAttributes'])->name('admin.catalog.products.get-filter-attributes'); - Route::get('products/get-category-product-maximum-price/{categoryId}', [ProductController::class, 'getCategoryProductMaximumPrice'])->name('admin.catalog.products.get-category-product-maximum-price'); + Route::get('products/get-category-product-maximum-price/{categoryId?}', [ProductController::class, 'getCategoryProductMaximumPrice'])->name('admin.catalog.products.get-category-product-maximum-price'); }); diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php index 23c4514ae..7e44443d4 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php @@ -1,7 +1,17 @@
    {!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!} - + @if (isset($category)) + + + @else + + + @endif {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
    @@ -22,6 +32,7 @@ :index="index" :attribute="attribute" :appliedFilterValues="appliedFilters[attribute.code]" + :max-price-src="maxPriceSrc" @onFilterAdded="addFilters(attribute.code, $event)"> @@ -99,6 +110,11 @@ Vue.component('layered-navigation', { template: '#layered-navigation-template', + props: [ + 'attributeSrc', + 'maxPriceSrc', + ], + data: function() { return { appliedFilters: {}, @@ -115,7 +131,7 @@ methods: { setFilterAttributes: function () { axios - .get('{{ route('admin.catalog.products.get-filter-attributes', $category->id) }}') + .get(this.attributeSrc) .then((response) => { this.attributes = response.data.filter_attributes; }); @@ -144,7 +160,7 @@ for (key in this.appliedFilters) { if (key != 'page') { - params.push(key + '=' + this.appliedFilters[key].join(',')) + params.push(key + '=' + this.appliedFilters[key].join(',')); } } @@ -161,6 +177,7 @@ 'attribute', 'addFilters', 'appliedFilterValues', + 'maxPriceSrc', ], data: function() { @@ -204,21 +221,21 @@ methods: { setMaxPrice: function () { axios - .get('{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id) }}') + .get(this.maxPriceSrc) .then((response) => { let maxPrice = response.data.max_price; this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500; }); }, - + addFilter: function (e) { - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); }, priceRangeUpdated: function (value) { this.appliedFilters = value; - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); }, clearFilters: function () { @@ -228,7 +245,7 @@ this.appliedFilters = []; - this.$emit('onFilterAdded', this.appliedFilters) + this.$emit('onFilterAdded', this.appliedFilters); }, optionClicked: function (id, {target}) { @@ -255,4 +272,4 @@ } }); -@endpush \ No newline at end of file +@endpush From 39836f9a7b7b3350685ba06bafcd01caa9c879d3 Mon Sep 17 00:00:00 2001 From: Devansh Date: Fri, 28 Jan 2022 16:25:34 +0530 Subject: [PATCH 2/2] Some Cleanup Done --- .../products/list/layered-navigation.blade.php | 15 ++++----------- .../products/list/layered-navigation.blade.php | 15 ++++----------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php index 3d4bebaab..44bb9670b 100755 --- a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php @@ -1,17 +1,10 @@
    {!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!} - @if (isset($category)) - - - @else - - - @endif + + {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
    diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php index 7e44443d4..49e7a0a77 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php @@ -1,17 +1,10 @@
    {!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!} - @if (isset($category)) - - - @else - - - @endif + + {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}