diff --git a/packages/Sarga/Shop/src/Repositories/ProductRepository.php b/packages/Sarga/Shop/src/Repositories/ProductRepository.php index f22d0db..07bb7f2 100644 --- a/packages/Sarga/Shop/src/Repositories/ProductRepository.php +++ b/packages/Sarga/Shop/src/Repositories/ProductRepository.php @@ -123,6 +123,17 @@ class ProductRepository extends WProductRepository ->leftJoin('marketplace_sellers', 'marketplace_sellers.id', '=', 'marketplace_products.marketplace_seller_id') ->addSelect('marketplace_sellers.shop_title'); + + /* added for api as per the documentation */ + if (isset($params['name'])) { + $query->where('product_flat.name', 'like', '%' . urldecode($params['name']) . '%'); + } + + /* added for api as per the documentation */ + if (isset($params['url_key'])) { + $query->where('product_flat.url_key', 'like', '%' . urldecode($params['url_key']) . '%'); + } + # sort direction $orderDirection = 'asc'; if (isset($params['order']) && in_array($params['order'], ['desc', 'asc'])) { @@ -141,6 +152,90 @@ 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']) + )); + + if (count($attributeFilters) > 0) { + $query->where(function ($filterQuery) use ($attributeFilters) { + + foreach ($attributeFilters as $attribute) { + $filterQuery->orWhere(function ($attributeQuery) use ($attribute) { + + $column = DB::getTablePrefix() . 'product_attribute_values.' . ProductAttributeValueProxy::modelClass()::$attributeTypeFields[$attribute->type]; + + $filterInputValues = explode(',', request()->get($attribute->code)); + + # define the attribute we are filtering + $attributeQuery = $attributeQuery->where('product_attribute_values.attribute_id', $attribute->id); + + # apply the filter values to the correct column for this type of attribute. + if ($attribute->type != 'price') { + + $attributeQuery->where(function ($attributeValueQuery) use ($column, $filterInputValues) { + foreach ($filterInputValues as $filterValue) { + if (! is_numeric($filterValue)) { + continue; + } + $attributeValueQuery->orWhereRaw("find_in_set(?, {$column})", [$filterValue]); + } + }); + + } else { + $attributeQuery->where($column, '>=', core()->convertToBasePrice(current($filterInputValues))) + ->where($column, '<=', core()->convertToBasePrice(end($filterInputValues))); + } + }); + } + + }); + + # this is key! if a product has been filtered down to the same number of attributes that we filtered on, + # we know that it has matched all of the requested filters. + $query->groupBy('variants.id'); + $query->havingRaw('COUNT(*) = ' . count($attributeFilters)); + } if (! core()->getConfigData('catalog.products.homepage.out_of_stock_items')) { $query = $this->checkOutOfStockItem($query); } @@ -154,8 +249,8 @@ class ProductRepository extends WProductRepository $subQuery->orWhere('product_flat.name', 'like', '%' . urldecode($value) . '%') ->orWhere('product_flat.short_description', 'like', '%' . urldecode($value) . '%'); } - }) - ->orderBy('product_id', 'desc'); + }); +// ->orderBy('product_id', 'desc'); })->paginate($perPage); }