Sylius - product variant creation - Column 'code' cannot be null

Viewed 14

I have a problem with generating variants in Sylius.

I know that product variant generator does not generate code automatically so i set it manually, but still getting error during setting variant price on line:

$this->channelPricingRepository->add($channelPricing);
 'INSERT INTO sylius_product_variant (code, created_at, updated_at, position, enabled, product_id, on_hold, on_hand, tracked, width, height, depth, weight, shipping_required, tax_category_id, shipping_category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, "2022-09-22 00:24:06", "2022-09-22 00:24:06", 1, 1, 70, 0, 0, 0, null, null, null, null, 1, null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null

There is my code:

            $option = $this->productOptionFactory->createNew();
            $option->setCode(Uuid::uuid4()->toString());
            $option->setName('Test option');

            $valuesData = [
                ['locale' => 'en_US', 'value' => 'test1'],
                ['locale' => 'en_US', 'value' => 'test2'],
                ['locale' => 'en_US', 'value' => 'test3'],
            ];

            foreach ($valuesData as $values) {
                $optionValue = $this->productOptionValueFactory->createNew();

                $optionValue->setCode(Uuid::uuid4()->toString());
                $optionValue->setFallbackLocale($values['locale']);
                $optionValue->setCurrentLocale($values['locale']);
                $optionValue->setValue($values['value']);

                $option->addValue($optionValue);
            }

            $product->addOption($option);

            $this->productVariantGenerator->generate($product);
            foreach($product->getVariants() as $variant){
                $variant->setCode(Uuid::uuid4()->toString());
                $channelPricing = $this->channelPricingFactory->createNew();
                $channelPricing->setPrice(10);
                $channelPricing->setOriginalPrice(10);
                $channelPricing->setChannelCode('FASHION_WEB');
                $channelPricing->setProductVariant($variant);
                $this->channelPricingRepository->add($channelPricing);
                //dd($variant);  
                $variant->addChannelPricing($channelPricing);
            }
            $this->productRepository->add($product);

Any ideas why does it happen?

0 Answers
Related