search refactor
This commit is contained in:
parent
b91f74f0dd
commit
2f3fb1ce4b
|
|
@ -1,12 +1,9 @@
|
|||
<?php
|
||||
namespace TPS\Shop\Http\Controllers;
|
||||
use TPS\Shop\Repositories\SliderRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use TPS\Shop\Repositories\ProductRepository;
|
||||
use Webkul\Shop\Http\Controllers\Controller;
|
||||
use Webkul\Velocity\Repositories\Product\ProductRepository as VelocityProductRepository;
|
||||
use Webkul\Product\Repositories\ProductRepository;
|
||||
|
||||
use Webkul\Product\Models\Product;
|
||||
use Webkul\Product\Models\ProductFlat;
|
||||
|
||||
class ShopController extends Controller
|
||||
{
|
||||
|
|
@ -17,37 +14,38 @@ class ShopController extends Controller
|
|||
*/
|
||||
|
||||
public function __construct(
|
||||
protected VelocityProductRepository $velocityProductRepository,
|
||||
protected ProductRepository $productRepository,
|
||||
)
|
||||
{
|
||||
$this->_config = request('_config');
|
||||
}
|
||||
|
||||
public function search()
|
||||
public function search(Request $request)
|
||||
{
|
||||
//$results = $this->velocityProductRepository->searchProductsFromCategory(request()->all());
|
||||
// dd($results->links());
|
||||
if (!is_null(request()->query('term'))) {
|
||||
$searchTerm = request()->query('term');
|
||||
}else{
|
||||
$searchTerm = request()->query('query');
|
||||
}
|
||||
// if (!is_null(request()->query('term'))) {
|
||||
// $searchTerm = request()->query('term');
|
||||
// }else{
|
||||
// $searchTerm = request()->query('query');
|
||||
// }
|
||||
//
|
||||
// if (is_null(request()->query('sort'))) {
|
||||
// $sort = "created_at";
|
||||
// }else{
|
||||
// $sort = request()->query('sort');
|
||||
// }
|
||||
//
|
||||
// if (is_null(request()->query('order'))) {
|
||||
// $order = "desc";
|
||||
// }else{
|
||||
// $order = request()->query('order');
|
||||
// }
|
||||
|
||||
if (is_null(request()->query('sort'))) {
|
||||
$sort = "created_at";
|
||||
}else{
|
||||
$sort = request()->query('sort');
|
||||
}
|
||||
|
||||
if (is_null(request()->query('order'))) {
|
||||
$order = "desc";
|
||||
}else{
|
||||
$order = request()->query('order');
|
||||
}
|
||||
|
||||
$results = Product::search($searchTerm)->orderBy($sort, $order)->paginate(15);
|
||||
//$results = Product::search($searchTerm)->orderBy($sort, $order)->paginate(15);
|
||||
// return view('shop::home.index', compact('sliderData'));
|
||||
return view('shop::search.search')->with('results', $results ? $results : null);
|
||||
$results = $this->productRepository->searchProductByAttribute($request->input());
|
||||
return view('shop::search.search')->with('results', $results ?: null);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class ProductRepository extends PRepository
|
|||
*/
|
||||
public function getAll($categoryId = null)
|
||||
{
|
||||
$params = request()->input();
|
||||
$params = request()->except(['mode']);
|
||||
|
||||
if (core()->getConfigData('catalog.products.storefront.products_per_page')) {
|
||||
$pages = explode(',', core()->getConfigData('catalog.products.storefront.products_per_page'));
|
||||
|
|
@ -267,7 +267,7 @@ class ProductRepository extends PRepository
|
|||
*/
|
||||
public function getDiscountedProducts($perPage = 5)
|
||||
{
|
||||
$params = request()->input();
|
||||
$params = request()->except(['mode']);
|
||||
|
||||
$page = Paginator::resolveCurrentPage('page');
|
||||
$repository = app(ProductFlatRepository::class)->scopeQuery(function ($query) use ($params) {
|
||||
|
|
@ -500,4 +500,57 @@ class ProductRepository extends PRepository
|
|||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search product by attribute.
|
||||
*
|
||||
* @param string $term
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function searchProductByAttribute($term)
|
||||
{
|
||||
$channel = core()->getRequestedChannelCode();
|
||||
|
||||
$locale = core()->getRequestedLocaleCode();
|
||||
|
||||
$queries = explode('_', $term);
|
||||
|
||||
if (core()->getConfigData('catalog.products.storefront.products_per_page')) {
|
||||
$pages = explode(',', core()->getConfigData('catalog.products.storefront.products_per_page'));
|
||||
|
||||
$perPage = isset($params['limit']) ? (! empty($params['limit']) ? $params['limit'] : 9) : current($pages);
|
||||
} else {
|
||||
$perPage = isset($params['limit']) && ! empty($params['limit']) ? $params['limit'] : 9;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$qb = app(ProductFlatRepository::class)->getModel()::search(implode(' OR ', $queries))
|
||||
->where('status', 1)
|
||||
->where('visible_individually', 1)
|
||||
->where('channel', $channel)
|
||||
->where('locale', $locale)
|
||||
->orderBy('product_id', 'desc');
|
||||
|
||||
# sort direction
|
||||
$orderDirection = 'asc';
|
||||
if (isset($params['order']) && in_array($params['order'], ['desc', 'asc'])) {
|
||||
$orderDirection = $params['order'];
|
||||
} else {
|
||||
$sortOptions = $this->getDefaultSortByOption();
|
||||
|
||||
$orderDirection = ! empty($sortOptions) ? $sortOptions[1] : 'asc';
|
||||
}
|
||||
|
||||
if (isset($params['sort'])) {
|
||||
$this->checkSortAttributeAndGenerateQuery($qb, $params['sort'], $orderDirection);
|
||||
} else {
|
||||
$sortOptions = $this->getDefaultSortByOption();
|
||||
if (! empty($sortOptions)) {
|
||||
$this->checkSortAttributeAndGenerateQuery($qb, $sortOptions[0], $orderDirection);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb->paginate($perPage);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue