feat: enhance product detail retrieval to support variant selection and update resource fields
This commit is contained in:
@@ -279,7 +279,7 @@ class ProductService
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function getProductDetail(Tenant $tenant, Product $product): Product
|
||||
public function getProductDetail(Tenant $tenant, Product $product, ?int $variantId = null): Product
|
||||
{
|
||||
$product->load([
|
||||
'attachments',
|
||||
@@ -288,15 +288,27 @@ class ProductService
|
||||
'attributes.options',
|
||||
]);
|
||||
|
||||
$defaultVariant = $product->variants()
|
||||
$selectedVariantQuery = $product->variants()
|
||||
->with([
|
||||
'attachments',
|
||||
'definitions.attribute.options',
|
||||
])
|
||||
->first();
|
||||
]);
|
||||
|
||||
if ($defaultVariant) {
|
||||
$product->setDefaultVariant($defaultVariant);
|
||||
$selectedVariant = $variantId !== null
|
||||
? $selectedVariantQuery->whereKey($variantId)->first()
|
||||
: null;
|
||||
|
||||
if ($selectedVariant === null) {
|
||||
$selectedVariant = $product->variants()
|
||||
->with([
|
||||
'attachments',
|
||||
'definitions.attribute.options',
|
||||
])
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($selectedVariant) {
|
||||
$product->setSelectedVariant($selectedVariant);
|
||||
}
|
||||
|
||||
return $product;
|
||||
|
||||
Reference in New Issue
Block a user