Creating Variants and Options programmatically


#1

Hi, not sure if this belong to this category…
I am trying to create import command to import products from CSV file.
All works fine but creating variants from options, like described here: https://docs.sylius.com/en/1.3/book/products/products.html

I have followed all to the letter, I can create product, assign channel, create price, quantity, options… but when trying to create variants with:

$variantGenerator = $this->vGenerator;
$variantGenerator->generate($product);

Gives this error: An exception occurred while executing ‘INSERT INTO sylius_product_variant (code, created_at, updated_at, position, product_id, on_hold, on_hand, tracked, width, height, depth, weight, shipping_requir
ed, tax_category_id, shipping_category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)’ with params [null, “2018-12-24 15:41:46”, “2018-12-24 15:41:46”, 0, 90, 0, 0, 0, null, null, null, nul
l, 1, null, null]: Integrity constraint violation: 1048 Column ‘code’ cannot be null

It is obvious what is a problem, but… I’ve checked the source code of the ProductVariantGenerator and code IS in the $optionSet[] array. Anybody has any idea?

I can create all variants with variant factory and repository, but ProductVariantGenerator function already exist, why not use it?
So far I couldn’t figure out why this is happening, and don’t want to change the source code, of course.
Any help is welcome.

Thanks


#2

Thank you all for help… not.
I have found the way, using factories and repositories …
The ProductVariantGenerator doesn’t work.