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