/**
  * @param FormEvent $event
  */
 public function preSetData(FormEvent $event)
 {
     /** @var ProductInterface $product */
     $product = $event->getData();
     Assert::isInstanceOf($product, ProductInterface::class);
     $this->generator->generate($product);
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function create(array $options = [])
 {
     $options = $this->optionsResolver->resolve($options);
     /** @var ProductInterface $product */
     $product = $this->productFactory->createNew();
     $product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_MATCH);
     $product->setCode($options['code']);
     $product->setEnabled($options['enabled']);
     $product->setMainTaxon($options['main_taxon']);
     $product->setArchetype($options['product_archetype']);
     $product->setCreatedAt($this->faker->dateTimeBetween('-1 week', 'now'));
     foreach ($this->getLocales() as $localeCode) {
         $product->setCurrentLocale($localeCode);
         $product->setFallbackLocale($localeCode);
         $product->setName($options['name']);
         $product->setShortDescription($options['short_description']);
         $product->setDescription($options['description']);
     }
     foreach ($options['taxons'] as $taxon) {
         $product->addTaxon($taxon);
     }
     foreach ($options['channels'] as $channel) {
         $product->addChannel($channel);
     }
     foreach ($options['product_options'] as $option) {
         $product->addOption($option);
     }
     foreach ($options['product_attributes'] as $attribute) {
         $product->addAttribute($attribute);
     }
     try {
         $this->variantGenerator->generate($product);
     } catch (\InvalidArgumentException $exception) {
         /** @var ProductVariantInterface $productVariant */
         $productVariant = $this->productVariantFactory->createNew();
         $product->addVariant($productVariant);
     }
     $i = 0;
     /** @var ProductVariantInterface $productVariant */
     foreach ($product->getVariants() as $productVariant) {
         $productVariant->setAvailableOn($this->faker->dateTimeThisYear);
         $productVariant->setPrice($this->faker->randomNumber(4));
         $productVariant->setCode(sprintf('%s-variant#%d', $options['code'], $i));
         $productVariant->setOnHand($this->faker->randomNumber(1));
         foreach ($options['images'] as $imagePath) {
             /** @var ProductVariantImageInterface $productVariantImage */
             $productVariantImage = $this->productVariantImageFactory->createNew();
             $productVariantImage->setFile(new UploadedFile($imagePath, basename($imagePath)));
             $this->imageUploader->upload($productVariantImage);
             $productVariant->addImage($productVariantImage);
         }
         ++$i;
     }
     return $product;
 }