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; namespace Webkul\Shop\Http\Controllers;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Category\Repositories\CategoryRepository; use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
class CategoryController extends Controller class CategoryController extends Controller
{ {
@ -10,10 +12,49 @@ class CategoryController extends Controller
* Create a new controller instance. * Create a new controller instance.
* *
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository * @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository
* @return void * @return void
*/ */
public function __construct(protected CategoryRepository $categoryRepository) public function __construct(
protected CategoryRepository $categoryRepository,
protected ProductFlatRepository $productFlatRepository
)
{ {
parent::__construct(); 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 Illuminate\Support\Facades\Storage;
use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Repositories\ProductAttributeValueRepository;
use Webkul\Product\Repositories\ProductDownloadableLinkRepository; use Webkul\Product\Repositories\ProductDownloadableLinkRepository;
use Webkul\Product\Repositories\ProductDownloadableSampleRepository; use Webkul\Product\Repositories\ProductDownloadableSampleRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
use Webkul\Product\Repositories\ProductRepository;
class ProductController extends Controller class ProductController extends Controller
{ {
/** /**
* Create a new controller instance. * 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\ProductAttributeValueRepository $productAttributeValueRepository
* @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository * @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository * @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @return void * @return void
*/ */
public function __construct( public function __construct(
protected ProductRepository $productRepository,
protected ProductFlatRepository $productFlatRepository,
protected ProductAttributeValueRepository $productAttributeValueRepository, protected ProductAttributeValueRepository $productAttributeValueRepository,
protected ProductDownloadableSampleRepository $productDownloadableSampleRepository, protected ProductDownloadableSampleRepository $productDownloadableSampleRepository,
protected ProductDownloadableLinkRepository $productDownloadableLinkRepository, protected ProductDownloadableLinkRepository $productDownloadableLinkRepository
protected CategoryRepository $categoryRepository
) )
{ {
parent::__construct(); parent::__construct();
@ -100,40 +91,4 @@ class ProductController extends Controller
abort(404); 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') !!} {!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation <layered-navigation
attribute-src="{{ route('admin.catalog.products.get-filter-attributes', $category->id ?? null) }}" attribute-src="{{ route('catalog.categories.filterable-attributes', $category->id ?? null) }}"
max-price-src="{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id ?? null) }}"> max-price-src="{{ route('catalog.categories.maximum-price', $category->id ?? null) }}">
</layered-navigation> </layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!} {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
@ -155,13 +155,18 @@
data: function() { data: function() {
return { return {
appliedFilters: [], appliedFilters: [],
active: false, active: false,
sliderConfig: { sliderConfig: {
value: [0, 0], value: [0, 0],
max: 500, max: 500,
processStyle: { processStyle: {
"backgroundColor": "#FF6472" "backgroundColor": "#FF6472"
}, },
tooltipStyle: { tooltipStyle: {
"backgroundColor": "#FF6472", "backgroundColor": "#FF6472",
"borderColor": "#FF6472" "borderColor": "#FF6472"
@ -188,12 +193,20 @@
methods: { methods: {
setMaxPrice: function () { setMaxPrice: function () {
if (this.attribute['code'] != 'price') {
return;
}
axios axios
.get(this.maxPriceSrc) .get(this.maxPriceSrc)
.then((response) => { .then((response) => {
let maxPrice = response.data.max_price; let maxPrice = response.data.max_price;
this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500; 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\CMS\Http\Controllers\Shop\PagePresenterController;
use Webkul\Shop\Http\Controllers\HomeController; use Webkul\Shop\Http\Controllers\HomeController;
use Webkul\Shop\Http\Controllers\ProductController; use Webkul\Shop\Http\Controllers\ProductController;
use Webkul\Shop\Http\Controllers\CategoryController;
use Webkul\Shop\Http\Controllers\ReviewController; use Webkul\Shop\Http\Controllers\ReviewController;
use Webkul\Shop\Http\Controllers\SearchController; use Webkul\Shop\Http\Controllers\SearchController;
use Webkul\Shop\Http\Controllers\SubscriptionController; use Webkul\Shop\Http\Controllers\SubscriptionController;
@ -79,7 +80,7 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function
'view' => 'shop.products.index', 'view' => 'shop.products.index',
])->name('shop.product.file.download'); ])->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') !!} {!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation <layered-navigation
attribute-src="{{ route('admin.catalog.products.get-filter-attributes', $category->id ?? null) }}" attribute-src="{{ route('catalog.categories.filterable-attributes', $category->id ?? null) }}"
max-price-src="{{ route('admin.catalog.products.get-category-product-maximum-price', $category->id ?? null) }}"> max-price-src="{{ route('catalog.categories.maximum-price', $category->id ?? null) }}">
</layered-navigation> </layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!} {!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
@ -176,18 +176,25 @@
data: function() { data: function() {
return { return {
active: false, active: false,
appliedFilters: [], appliedFilters: [],
sliderConfig: { sliderConfig: {
max: 500, max: 500,
value: [0, 0], value: [0, 0],
processStyle: { processStyle: {
"backgroundColor": "#FF6472" "backgroundColor": "#FF6472"
}, },
tooltipStyle: { tooltipStyle: {
"borderColor": "#FF6472", "borderColor": "#FF6472",
"backgroundColor": "#FF6472", "backgroundColor": "#FF6472",
}, },
priceTo: 0, priceTo: 0,
priceFrom: 0, priceFrom: 0,
} }
} }
@ -213,11 +220,14 @@
methods: { methods: {
setMaxPrice: function () { setMaxPrice: function () {
if (this.attribute['code'] != 'price') {
return;
}
axios axios
.get(this.maxPriceSrc) .get(this.maxPriceSrc)
.then((response) => { .then((response) => {
let maxPrice = response.data.max_price; let maxPrice = response.data.max_price;
this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500; this.sliderConfig.max = maxPrice ? ((parseInt(maxPrice) !== 0 || maxPrice) ? parseInt(maxPrice) : 500) : 500;
if (! this.appliedFilterValues) { if (! this.appliedFilterValues) {