Skip to content

Commit

Permalink
sorting products by type is now more extendable
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin committed Nov 6, 2024
1 parent 6aef288 commit 2385345
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Model/Product/Elasticsearch/ProductExportRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
};
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Product/Search/FilterQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
1 change: 1 addition & 0 deletions src/Model/Product/Search/ProductElasticsearchConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2385345

Please sign in to comment.