feat: add status field to product variant form and ensure it defaults to 'active'
This commit is contained in:
parent
1a2b741209
commit
afdd0905db
@ -220,38 +220,6 @@ function () use ($validated, $product, $user, $canEditDirectly): void {
|
||||
if ($canEditDirectly) {
|
||||
$payload = $this->enrichPayload($this->buildPayloadFromValidated($validated));
|
||||
$this->applyPayloadToProduct($product, $payload);
|
||||
|
||||
foreach ($validated['variants'] as $index => $variantData) {
|
||||
if (! empty($variantData['id'])) {
|
||||
$variant = $product->variants()->find($variantData['id']);
|
||||
if ($variant) {
|
||||
$this->syncVariantImages($variant, $variantData, $index);
|
||||
if (! empty($variantData['prices'])) {
|
||||
foreach ($variantData['prices'] as $type => $priceValue) {
|
||||
$variant->prices()->updateOrCreate(
|
||||
['type' => $type],
|
||||
['price' => $priceValue]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$variant = $product->variants()->create([
|
||||
'name' => $variantData['name'],
|
||||
'stock' => $variantData['stock'],
|
||||
'retail_stock' => $variantData['retail_stock'],
|
||||
]);
|
||||
$this->syncVariantImages($variant, $variantData, $index);
|
||||
if (! empty($variantData['prices'])) {
|
||||
foreach ($variantData['prices'] as $type => $priceValue) {
|
||||
$variant->prices()->create([
|
||||
'type' => $type,
|
||||
'price' => $priceValue,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$verificationRequest = OwnerVerificationRequest::create([
|
||||
'action' => OwnerVerificationAction::UPDATE,
|
||||
@ -581,6 +549,8 @@ private function applyPayloadToProduct(
|
||||
|
||||
if ($verificationRequest !== null) {
|
||||
$this->applyVariantImageChanges($verificationRequest, $variant, $variantData, (int) $index);
|
||||
} else {
|
||||
$this->syncVariantImages($variant, $variantData, $index);
|
||||
}
|
||||
|
||||
if (! empty($variantData['prices'])) {
|
||||
@ -604,6 +574,8 @@ private function applyPayloadToProduct(
|
||||
|
||||
if ($verificationRequest !== null) {
|
||||
$this->copyRequestVariantImages($verificationRequest, (int) $index, $variant);
|
||||
} else {
|
||||
$this->syncVariantImages($variant, $variantData, $index);
|
||||
}
|
||||
|
||||
if (! empty($variantData['prices'])) {
|
||||
@ -803,6 +775,8 @@ private function buildPayloadFromValidated(array $validated): array
|
||||
'retail_stock' => $variantData['retail_stock'],
|
||||
'prices' => $variantData['prices'] ?? [],
|
||||
'remove_media_ids' => $variantData['remove_media_ids'] ?? [],
|
||||
'images' => $variantData['images'] ?? null,
|
||||
's3_keys' => $variantData['s3_keys'] ?? null,
|
||||
])
|
||||
->all(),
|
||||
];
|
||||
@ -812,6 +786,7 @@ private function syncVariantImages(
|
||||
ProductVariant $variant,
|
||||
array $variantData,
|
||||
int $index,
|
||||
bool $required = true,
|
||||
): void {
|
||||
$this->mediaService->syncCollection(
|
||||
$variant,
|
||||
@ -819,7 +794,7 @@ private function syncVariantImages(
|
||||
$variantData['images'] ?? null,
|
||||
$variantData['remove_media_ids'] ?? null,
|
||||
self::MAX_VARIANT_IMAGES,
|
||||
required: true,
|
||||
required: $required,
|
||||
errorKey: "variants.{$index}.s3_keys",
|
||||
s3Keys: $variantData['s3_keys'] ?? null,
|
||||
);
|
||||
|
||||
@ -35,6 +35,7 @@ const editForm = useForm({
|
||||
name: '',
|
||||
description: '',
|
||||
category_ids: [] as number[],
|
||||
status: 'active',
|
||||
variants: [] as any[],
|
||||
});
|
||||
|
||||
@ -53,6 +54,7 @@ function populateForm(variant: Variant | null) {
|
||||
editForm.name = props.product.name;
|
||||
editForm.description = props.product.description ?? '';
|
||||
editForm.category_ids = (props.product.categories ?? []).map((c) => c.id);
|
||||
editForm.status = props.product.status ?? 'active';
|
||||
|
||||
editForm.variants = (props.product.variants ?? []).map((v) => {
|
||||
const prices: Record<string, string> = {
|
||||
@ -122,6 +124,7 @@ function submit() {
|
||||
formData.append('_method', 'PUT');
|
||||
formData.append('name', editForm.name.trim());
|
||||
formData.append('description', editForm.description.trim());
|
||||
formData.append('status', editForm.status);
|
||||
|
||||
editForm.category_ids.forEach((id) => {
|
||||
formData.append('category_ids[]', String(id));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user