From ef2ba29d0a43550922cb0df22f00d86f52d0bf7c Mon Sep 17 00:00:00 2001 From: Amanmyrat Date: Sat, 23 Sep 2023 18:50:04 +0500 Subject: [PATCH] Add search sorting --- .../src/Repositories/ProductRepository.php | 68 ++++--------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/packages/Sarga/Shop/src/Repositories/ProductRepository.php b/packages/Sarga/Shop/src/Repositories/ProductRepository.php index 07bb7f2..cedd541 100644 --- a/packages/Sarga/Shop/src/Repositories/ProductRepository.php +++ b/packages/Sarga/Shop/src/Repositories/ProductRepository.php @@ -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); }