示例#1
0
 protected function createVariationProduct()
 {
     $options = ['screwSize' => ['M6' => ['M6', 'M6'], 'M8' => ['M8', 'M8'], 'M10' => ['M10', 'M10'], 'M12' => ['M12', 'M12']], 'color' => ['black' => ['BK', 'Black'], 'blue' => ['BL', 'Blue'], 'gold' => ['GO', 'Gold'], 'green' => ['GR', 'Green'], 'red' => ['RD', 'Red']], 'material' => ['plastic' => ['Plastic', 'Plastic'], 'metal' => ['Metal', 'Metal']]];
     $product = new Product();
     foreach ($options as $type => $typeOptions) {
         foreach ($typeOptions as $index => $data) {
             $product->setOption($type, $index, $data[0], $data[1]);
         }
     }
     $data = [['M6' => ['material' => 'metal', 'color' => 'black']], ['M6' => ['material' => 'metal', 'color' => 'blue']], ['M6' => ['material' => 'metal', 'color' => 'green']], ['M10' => ['material' => 'metal', 'color' => 'black']], ['M10' => ['material' => 'metal', 'color' => 'gold']], ['M10' => ['material' => 'metal', 'color' => 'green']]];
     foreach ($data as $options) {
         foreach ($options as $screwSize => $otherOptions) {
             $label = $screwSize;
             foreach ($otherOptions as $type => $name) {
                 $label .= $name;
             }
             $variation = $product->useVariation($label);
         }
         foreach ($options as $screwSize => $otherOptions) {
             $variation->setOption('screwSize', $screwSize);
             foreach ($otherOptions as $type => $index) {
                 $variation->setOption($type, $index);
             }
         }
     }
 }