Merge pull request #6715 from jitendra-webkul/master

Optimized performance and code
This commit is contained in:
Jitendra Singh 2022-09-14 17:59:40 +05:30 committed by GitHub
commit 5b068e47db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 55 deletions

View File

@ -2,7 +2,9 @@
namespace Webkul\Shop\Http\Controllers;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
class CategoryController extends Controller
{
@ -10,10 +12,49 @@ class CategoryController extends Controller
* Create a new controller instance.
*
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository
* @return void
*/
public function __construct(protected CategoryRepository $categoryRepository)
public function __construct(
protected CategoryRepository $categoryRepository,
protected ProductFlatRepository $productFlatRepository
)
{
parent::__construct();
}
/**
* Get filter attributes for product.
*
* @return \Illuminate\Http\Response
*/
public function getFilterAttributes($categoryId = null, AttributeRepository $attributeRepository)
{
$category = $this->categoryRepository->findOrFail($categoryId);
if (empty($filterAttributes = $category->filterableAttributes)) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
return response()->json([
'filter_attributes' => $filterAttributes,
]);
}
/**
* Get category product maximum price.
*
* @return \Illuminate\Http\Response
*/
public function getCategoryProductMaximumPrice($categoryId = null)
{
$category = $this->categoryRepository->findOrFail($categoryId);
$maxPrice = $this->productFlatRepository->handleCategoryProductMaximumPrice($category);
return response()->json([
'max_price' => $maxPrice,
]);
}
}

View File

@ -4,33 +4,24 @@ 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;
use Webkul\Product\Repositories\ProductDownloadableSampleRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
use Webkul\Product\Repositories\ProductRepository;
class ProductController extends Controller
{
/**
* Create a new controller instance.
*
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository
* @param \Webkul\Product\Repositories\ProductAttributeValueRepository $productAttributeValueRepository
* @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @return void
*/
public function __construct(
protected ProductRepository $productRepository,
protected ProductFlatRepository $productFlatRepository,
protected ProductAttributeValueRepository $productAttributeValueRepository,
protected ProductDownloadableSampleRepository $productDownloadableSampleRepository,
protected ProductDownloadableLinkRepository $productDownloadableLinkRepository,
protected CategoryRepository $categoryRepository
protected ProductDownloadableLinkRepository $productDownloadableLinkRepository
)
{
parent::__construct();
@ -100,40 +91,4 @@ class ProductController extends Controller
abort(404);
}
}
/**
* Get filter attributes for product.
*
* @return \Illuminate\Http\Response
*/
public function getFilterAttributes($categoryId = null, AttributeRepository $attributeRepository)
{
$category = $this->categoryRepository->findOrFail($categoryId);
if (empty($filterAttributes = $category->filterableAttributes)) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
return response()->json([
'filter_attributes' => $filterAttributes,
]);
}
/**
* Get category product maximum price.
*
* @return \Illuminate\Http\Response
*/
public function getCategoryProductMaximumPrice($categoryId = null)
{
$maxPrice = 0;
if ($category = $this->categoryRepository->find($categoryId)) {
$maxPrice = $this->productFlatRepository->handleCategoryProductMaximumPrice($category);
}
return response()->json([
'max_price' => $maxPrice,
]);
}
}

View File

@ -2,8 +2,8 @@
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation
attribute-src="{{ route('admin.catalog.products.get-filter-attributes', $category->id ?? null) }}"
max-price-src="{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id ?? null) }}">
attribute-src="{{ route('catalog.categories.filterable-attributes', $category->id ?? null) }}"
max-price-src="{{ route('catalog.categories.maximum-price', $category->id ?? null) }}">
</layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
@ -155,13 +155,18 @@
data: function() {
return {
appliedFilters: [],
active: false,
sliderConfig: {
value: [0, 0],
max: 500,
processStyle: {
"backgroundColor": "#FF6472"
},
tooltipStyle: {
"backgroundColor": "#FF6472",
"borderColor": "#FF6472"
@ -188,12 +193,20 @@
methods: {
setMaxPrice: function () {
if (this.attribute['code'] != 'price') {
return;
}
axios
.get(this.maxPriceSrc)
.then((response) => {
let maxPrice = response.data.max_price;
this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500;
if (! this.appliedFilterValues) {
this.sliderConfig.value = [0, this.sliderConfig.max];
this.sliderConfig.priceTo = this.sliderConfig.max;
}
});
},

View File

@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Route;
use Webkul\CMS\Http\Controllers\Shop\PagePresenterController;
use Webkul\Shop\Http\Controllers\HomeController;
use Webkul\Shop\Http\Controllers\ProductController;
use Webkul\Shop\Http\Controllers\CategoryController;
use Webkul\Shop\Http\Controllers\ReviewController;
use Webkul\Shop\Http\Controllers\SearchController;
use Webkul\Shop\Http\Controllers\SubscriptionController;
@ -79,7 +80,7 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function
'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('categories/filterable-attributes/{categoryId?}', [CategoryController::class, 'getFilterAttributes'])->name('catalog.categories.filterable-attributes');
Route::get('products/get-category-product-maximum-price/{categoryId?}', [ProductController::class, 'getCategoryProductMaximumPrice'])->name('admin.catalog.products.get-category-product-maximum-price');
Route::get('categories/maximum-price/{categoryId?}', [CategoryController::class, 'getCategoryProductMaximumPrice'])->name('catalog.categories.maximum-price');
});

View File

@ -2,8 +2,8 @@
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation
attribute-src="{{ route('admin.catalog.products.get-filter-attributes', $category->id ?? null) }}"
max-price-src="{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id ?? null) }}">
attribute-src="{{ route('catalog.categories.filterable-attributes', $category->id ?? null) }}"
max-price-src="{{ route('catalog.categories.maximum-price', $category->id ?? null) }}">
</layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
@ -176,18 +176,25 @@
data: function() {
return {
active: false,
appliedFilters: [],
sliderConfig: {
max: 500,
value: [0, 0],
processStyle: {
"backgroundColor": "#FF6472"
},
tooltipStyle: {
"borderColor": "#FF6472",
"backgroundColor": "#FF6472",
},
priceTo: 0,
priceFrom: 0,
}
}
@ -213,11 +220,14 @@
methods: {
setMaxPrice: function () {
if (this.attribute['code'] != 'price') {
return;
}
axios
.get(this.maxPriceSrc)
.then((response) => {
let maxPrice = response.data.max_price;
this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500;
if (! this.appliedFilterValues) {