From 23853459da6b0cbbbb56b8125cd26318754c381c Mon Sep 17 00:00:00 2001 From: Martin Grossmann Date: Fri, 25 Oct 2024 13:01:47 +0200 Subject: [PATCH] sorting products by type is now more extendable --- .../Elasticsearch/ProductExportRepository.php | 18 ++++++++++++++++++ .../Scope/ProductExportFieldProvider.php | 1 + src/Model/Product/Search/FilterQuery.php | 2 +- .../Search/ProductElasticsearchConverter.php | 1 + .../ProductElasticsearchConverterTest.php | 2 ++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Model/Product/Elasticsearch/ProductExportRepository.php b/src/Model/Product/Elasticsearch/ProductExportRepository.php index e9d29904c2..efe2d477d1 100644 --- a/src/Model/Product/Elasticsearch/ProductExportRepository.php +++ b/src/Model/Product/Elasticsearch/ProductExportRepository.php @@ -187,6 +187,8 @@ protected function getExportedFieldValue(int $domainId, Product $product, string ProductExportFieldProvider::ACCESSORIES => $this->extractAccessoriesIds($product), ProductExportFieldProvider::HREFLANG_LINKS => $this->hreflangLinksFacade->getForProduct($product, $domainId), ProductExportFieldProvider::PRODUCT_TYPE => $this->extractProductType($product, $domainId), + ProductExportFieldProvider::PRIORITY_BY_PRODUCT_TYPE => $this->extractPriorityByProductType($product, $domainId), + default => throw new InvalidArgumentException(sprintf('There is no definition for exporting "%s" field to Elasticsearch', $field)), }; } @@ -331,6 +333,22 @@ protected function extractProductType(Product $product, int $domainId): string return $product->getProductType(); } + /** + * @param \Shopsys\FrameworkBundle\Model\Product\Product $product + * @param int $domainId + * @return int + */ + protected function extractPriorityByProductType(Product $product, int $domainId): int + { + $productType = $this->extractProductType($product, $domainId); + + return match ($productType) { + ProductTypeEnum::TYPE_BASIC => 20, + ProductTypeEnum::TYPE_INQUIRY => 10, + default => -100, + }; + } + /** * @param int $domainId * @param \Shopsys\FrameworkBundle\Model\Product\Product $product diff --git a/src/Model/Product/Elasticsearch/Scope/ProductExportFieldProvider.php b/src/Model/Product/Elasticsearch/Scope/ProductExportFieldProvider.php index e75f992756..5c77b9efe0 100644 --- a/src/Model/Product/Elasticsearch/Scope/ProductExportFieldProvider.php +++ b/src/Model/Product/Elasticsearch/Scope/ProductExportFieldProvider.php @@ -44,6 +44,7 @@ class ProductExportFieldProvider public const string ACCESSORIES = 'accessories'; public const string HREFLANG_LINKS = 'hreflang_links'; public const string PRODUCT_TYPE = 'product_type'; + public const string PRIORITY_BY_PRODUCT_TYPE = 'priority_by_product_type'; /** * @return string[] diff --git a/src/Model/Product/Search/FilterQuery.php b/src/Model/Product/Search/FilterQuery.php index 94428b0661..ad1fc50c0f 100644 --- a/src/Model/Product/Search/FilterQuery.php +++ b/src/Model/Product/Search/FilterQuery.php @@ -75,7 +75,7 @@ public function applyOrdering(string $orderingModeId, PricingGroup $pricingGroup ]; if ($orderingModeId === ProductListOrderingConfig::ORDER_BY_PRIORITY) { - $clone->sorting['product_type'] = 'asc'; + $clone->sorting['priority_by_product_type'] = 'desc'; $clone->sorting['ordering_priority'] = 'desc'; $clone->sorting['name.keyword'] = 'asc'; diff --git a/src/Model/Product/Search/ProductElasticsearchConverter.php b/src/Model/Product/Search/ProductElasticsearchConverter.php index 5747819c53..3f0fab70fc 100644 --- a/src/Model/Product/Search/ProductElasticsearchConverter.php +++ b/src/Model/Product/Search/ProductElasticsearchConverter.php @@ -55,6 +55,7 @@ public function fillEmptyFields(array $product): array $result['seo_meta_description'] = $product['seo_meta_description'] ?? null; $result['hreflang_links'] = $product['hreflang_links'] ?? []; $result['product_type'] = $product['product_type'] ?? ProductTypeEnum::TYPE_BASIC; + $result['priority_by_product_type'] = $product['priority_by_product_type'] ?? 0; return $result; } diff --git a/tests/Unit/Model/Product/Search/ProductElasticsearchConverterTest.php b/tests/Unit/Model/Product/Search/ProductElasticsearchConverterTest.php index 50532c3db3..5ba1a1d797 100644 --- a/tests/Unit/Model/Product/Search/ProductElasticsearchConverterTest.php +++ b/tests/Unit/Model/Product/Search/ProductElasticsearchConverterTest.php @@ -54,6 +54,7 @@ public function testFillEmptyFields(): void 'seo_meta_description' => null, 'hreflang_links' => [], 'product_type' => 'basic', + 'priority_by_product_type' => 0, ]; $converter = new ProductElasticsearchConverter(); @@ -120,6 +121,7 @@ public function testFillEmptyParameterFields(): void 'seo_meta_description' => null, 'hreflang_links' => [], 'product_type' => 'basic', + 'priority_by_product_type' => 0, ]; $converter = new ProductElasticsearchConverter();