mardi 8 septembre 2020

Creating Product Combinations in Laravel

I am trying to create product Combinations but not able to create it..

Json I get from the Form submission

 dd($request['varationsArray']); // I have formatted the below result
{
   "1":[
      2
   ],
   "2":[
      1,
      3
   ]
}
//Unformatted Result
"{"1":[2],"2":[1,3]}"

Here is the Controller Code..

 //Store and create a combination of the mapped products
public function productCombinationStore(Request $request)
{
   
//Create all possible combinations
$variation_arrays = array($request['varationsArray'], true);
foreach ($variation_arrays as  $value) {
    $decoded_json = json_decode($value, true);
    foreach ($decoded_json as $item_key => $item) {
        foreach ($item as $item_data) {
            DB::beginTransaction();
            try {
                //Save to product Variations
                $product_variations = Variants::findOrFail($item_key);
                $product_variations_options = VariationOptions::findOrFail($item_data);
                $product_variations_id =  ProductVariants::firstOrCreate([
                    'product_id' => $request['product_id'],
                    'variants_id' => $item_key,
                    'variantsName' => $product_variations->variationName,
                ]);

                $product_variants_option_values_id = ProductsVariantsOptionValues::firstOrCreate([
                    'product_variants_id' => $product_variations_id->id,
                    'variation_options_id' => $item_data,
                    'variationOptionName' => $product_variations_options->variationOptionName,
                ]);

                $collection = collect($product_variations->variationName);
                echo $collection->crossJoin($product_variations_options->variationOptionName);

                //[["Size","Medium"]][["Color","White"]][["Color","Blue"]]
                //Save to Product Variation Values
                //Update the Products value in produicts table 

                Products::where('id', $request['product_id'])->update([
                    'hasVariants' => '1'
                ]);

                $combination_string = $product_variations->variationName . '-' .  $product_variations_options->variationOptionName;
                $combination_string_str =  str_replace(' ', '', $combination_string);
                ProductCombination::firstOrCreate([
                    'product_id' => $request['product_id'],
                    'product_variants_id' => $product_variations_id->id,
                    'product_variants_option_values_id' => $product_variants_option_values_id->id,
                    'variants_id' => $item_key,
                    'variation_options_id' => $item_data,
                    'combinationString' => $combination_string_str,
                    'unique_string_id' => Hash::make($combination_string_str),
                ]);
                // db::commit();
            } catch (\Throwable $th) {
                throw $th;
            }
        }
    }
}
return response()->json(['Status' => 'OK', 'Message' => 'Combination Successfully created'],200);
}
  1. Converting the form input to JSON
  2. Using the for-loop to iterate through the results
  3. Saving the Variations name along with product_id to the table. (Variation Name : Size,Color)
  4. Saving the Variation Values along with variation_id. (Variation Value: L,XL,M,White,Blue)
  5. Trying to create combination from selection but the records are created one on one but not whole. How do I create a diverse combination

Please see the image of created combination,

Product Combnation create

I tried to use the Laravel Collect Method but I was not able to proceed further on this..

 $collection = collect($product_variations->variationName);
 echo $collection->crossJoin($product_variations_options->variationOptionName);
 //[["Size","Medium"]][["Color","White"]][["Color","Blue"]]

I am I following the correct way.. Please suggest me if there are any better alternatives..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire