Add search sorting

This commit is contained in:
Amanmyrat 2023-09-23 18:50:04 +05:00
parent ad316e65ee
commit ef2ba29d0a
1 changed files with 15 additions and 53 deletions

View File

@ -123,6 +123,20 @@ class ProductRepository extends WProductRepository
->leftJoin('marketplace_sellers', 'marketplace_sellers.id', '=', 'marketplace_products.marketplace_seller_id')
->addSelect('marketplace_sellers.shop_title');
if (! core()->getConfigData('catalog.products.homepage.out_of_stock_items')) {
$query = $this->checkOutOfStockItem($query);
}
$query->where('product_flat.status', 1)
->where('product_flat.visible_individually', 1)
->where(function ($subQuery) use ($term) {
$queries = explode('_', $term);
foreach (array_map('trim', $queries) as $value) {
$subQuery->orWhere('product_flat.name', 'like', '%' . urldecode($value) . '%')
->orWhere('product_flat.short_description', 'like', '%' . urldecode($value) . '%');
}
});
/* added for api as per the documentation */
if (isset($params['name'])) {
@ -152,46 +166,6 @@ class ProductRepository extends WProductRepository
}
}
if ($priceFilter = request('price')) {
$priceRange = explode(',', $priceFilter);
if (count($priceRange) > 0) {
$customerGroupId = null;
if (Cart::getCurrentCustomer()->check()) {
$customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id;
} else {
$customerGuestGroup = app('Webkul\Customer\Repositories\CustomerGroupRepository')->getCustomerGuestGroup();
if ($customerGuestGroup) {
$customerGroupId = $customerGuestGroup->id;
}
}
$query
->leftJoin('catalog_rule_product_prices', 'catalog_rule_product_prices.product_id', '=', 'variants.product_id')
->leftJoin('product_customer_group_prices', 'product_customer_group_prices.product_id', '=', 'variants.product_id')
->where(function ($query) use ($priceRange, $customerGroupId) {
$query->where(function ($query) use ($priceRange){
$query
->where('variants.min_price', '>=', core()->convertToBasePrice($priceRange[0]))
->where('variants.min_price', '<=', core()->convertToBasePrice(end($priceRange)));
})
->orWhere(function ($query) use ($priceRange) {
$query
->where('catalog_rule_product_prices.price', '>=', core()->convertToBasePrice($priceRange[0]))
->where('catalog_rule_product_prices.price', '<=', core()->convertToBasePrice(end($priceRange)));
})
->orWhere(function ($query) use ($priceRange, $customerGroupId) {
$query
->where('product_customer_group_prices.value', '>=', core()->convertToBasePrice($priceRange[0]))
->where('product_customer_group_prices.value', '<=', core()->convertToBasePrice(end($priceRange)))
->where('product_customer_group_prices.customer_group_id', '=', $customerGroupId);
});
});
}
}
$attributeFilters = $this->attributeRepository
->getProductDefaultAttributes(array_keys(
request()->except(['price'])
@ -236,20 +210,8 @@ class ProductRepository extends WProductRepository
$query->groupBy('variants.id');
$query->havingRaw('COUNT(*) = ' . count($attributeFilters));
}
if (! core()->getConfigData('catalog.products.homepage.out_of_stock_items')) {
$query = $this->checkOutOfStockItem($query);
}
return $query->where('product_flat.status', 1)
->where('product_flat.visible_individually', 1)
->where(function ($subQuery) use ($term) {
$queries = explode('_', $term);
foreach (array_map('trim', $queries) as $value) {
$subQuery->orWhere('product_flat.name', 'like', '%' . urldecode($value) . '%')
->orWhere('product_flat.short_description', 'like', '%' . urldecode($value) . '%');
}
});
return $query->groupBy('product_flat.id');
// ->orderBy('product_id', 'desc');
})->paginate($perPage);
}