Improved code for category page

This commit is contained in:
jitendra 2022-09-14 17:35:54 +05:30
parent 9005f26c50
commit 4832b09ae9
5 changed files with 50 additions and 53 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') !!}

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') !!}