|
|
@@ -10,6 +10,7 @@ use Longyi\Core\Repositories\ProductOptionRepository;
|
|
|
use Longyi\Core\Repositories\ProductOptionValueRepository;
|
|
|
use Longyi\Core\Repositories\ProductVariantRepository;
|
|
|
use Longyi\Core\Helpers\FlexibleVariantOption;
|
|
|
+use Webkul\Product\Helpers\Indexers\Price as PriceIndexer;
|
|
|
use Webkul\Product\Repositories\ProductRepository;
|
|
|
|
|
|
class FlexibleVariantController extends Controller
|
|
|
@@ -99,6 +100,7 @@ class FlexibleVariantController extends Controller
|
|
|
public function createOption(Request $request): JsonResponse
|
|
|
{
|
|
|
$this->validate($request, [
|
|
|
+ 'product_id' => 'required|exists:products,id',
|
|
|
'label' => 'required|string|max:255',
|
|
|
'code' => 'required|string|max:100|unique:product_options,code',
|
|
|
'type' => 'required|in:select,radio,checkbox,color,button',
|
|
|
@@ -110,8 +112,20 @@ class FlexibleVariantController extends Controller
|
|
|
'values.*.position' => 'nullable|integer',
|
|
|
'values.*.meta' => 'nullable|array',
|
|
|
]);
|
|
|
-
|
|
|
- $option = $this->productOptionRepository->createWithValues($request->all());
|
|
|
+ $product = $this->productRepository->find($request->product_id);
|
|
|
+ if (!$product) {
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => 'Product not found',
|
|
|
+ ], 404);
|
|
|
+ }
|
|
|
+ $option = $this->productOptionRepository->createWithValues($request->except('product_id'));
|
|
|
+
|
|
|
+ $product->options()->attach($option->id, [
|
|
|
+ 'position' => $request->position ?? 0,
|
|
|
+ 'is_required' => $request->is_required ?? false,
|
|
|
+ 'meta' => isset($request->meta) ? json_encode($request->meta) : null,
|
|
|
+ ]);
|
|
|
|
|
|
return response()->json([
|
|
|
'success' => true,
|
|
|
@@ -190,6 +204,7 @@ class FlexibleVariantController extends Controller
|
|
|
public function attachOptions(Request $request, int $productId): JsonResponse
|
|
|
{
|
|
|
$this->validate($request, [
|
|
|
+ 'product_id' => 'required|exists:products,id',
|
|
|
'options' => 'required|array',
|
|
|
'options.*.id' => 'required|exists:product_options,id',
|
|
|
'options.*.position' => 'nullable|integer',
|
|
|
@@ -345,8 +360,12 @@ class FlexibleVariantController extends Controller
|
|
|
], 404);
|
|
|
}
|
|
|
|
|
|
+ $productId = $variant->product_id;
|
|
|
+
|
|
|
$this->productVariantRepository->delete($id);
|
|
|
|
|
|
+ $this->reindexProduct($productId);
|
|
|
+
|
|
|
return response()->json([
|
|
|
'success' => true,
|
|
|
'message' => 'Variant deleted successfully',
|
|
|
@@ -387,10 +406,18 @@ class FlexibleVariantController extends Controller
|
|
|
'status' => 'required|boolean',
|
|
|
]);
|
|
|
|
|
|
+ $productIds = [];
|
|
|
+
|
|
|
foreach ($request->variant_ids as $variantId) {
|
|
|
- $this->productVariantRepository->update([
|
|
|
+ $variant = $this->productVariantRepository->update([
|
|
|
'status' => $request->status,
|
|
|
], $variantId);
|
|
|
+
|
|
|
+ $productIds[$variant->product_id] = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (array_keys($productIds) as $productId) {
|
|
|
+ $this->reindexProduct($productId);
|
|
|
}
|
|
|
|
|
|
return response()->json([
|
|
|
@@ -398,4 +425,24 @@ class FlexibleVariantController extends Controller
|
|
|
'message' => 'Status updated successfully',
|
|
|
]);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Trigger price reindex for a product and its flexible variants.
|
|
|
+ */
|
|
|
+ protected function reindexProduct(int $productId): void
|
|
|
+ {
|
|
|
+ $product = $this->productRepository->with([
|
|
|
+ 'attribute_family',
|
|
|
+ 'attribute_values',
|
|
|
+ 'price_indices',
|
|
|
+ 'customer_group_prices',
|
|
|
+ 'flexibleVariants',
|
|
|
+ 'flexibleVariants.price_indices',
|
|
|
+ 'flexibleVariants.customer_group_prices',
|
|
|
+ ])->find($productId);
|
|
|
+
|
|
|
+ if ($product) {
|
|
|
+ app(PriceIndexer::class)->reindexBatch([$product]);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|