Johan
Johan

Reputation: 11

Shopware 6 - Create product programmatically

I have a command script to import products from csv file.

The code i use to create the product is:

$this->productRepository->create([[
        'id'            => Uuid::randomHex(),
        'name'          => 'exampls',
        'taxId'         => 'b6e10c80b24946a48bb74e742d280c75',
        'stock'         => 999,
        'createdAt'     => '2022-01-01T10:17:05+02:00',
        'price'         => [
            [
                'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
                'gross'      => 99,
                'net'        => 99,
                'linked'     => true,
            ]
        ],
        'productNumber' => $suffix,
        'visibilities'  => [
            [
                'salesChannelId' => '5ce47a708e484e4d8c20221f8cdfa08f',
                'visibility'     => ProductVisibilityDefinition::VISIBILITY_ALL,
            ],
        ],
    ]], Context::createDefaultContext());

I get the following error:

     An exception occurred while executing 'INSERT INTO `product` (`id`, `version_id`, `parent_version_id`, `product_manufacturer_versio  
  n_id`, `tax_id`, `product_media_version_id`, `cms_page_version_id`, `price`, `product_number`, `stock`, `restock_time`, `active`, `  
  is_closeout`, `purchase_steps`, `min_purchase`, `shipping_free`, `created_at`) VALUES ('���zgL��vc�.�p','���jK¾K��u,4%','���  
  jK¾K��u,4%','���jK¾K��u,4%','��
                                 ��IF���Nt-(
                                            u','���jK¾K��u,4%','���jK¾K��u,4%','{\"cb7d2554b0ce847cd82f3ac9bd1c0dfca\":{\  
  "currencyId\":\"b7d2554b0ce847cd82f3ac9bd1c0dfca\",\"gross\":99.0,\"net\":99.0,\"linked\":true}}','62e2977934944','999',NULL,'1','0  
  ','1','1','0','2022-01-01 08:17:05.000');':           

What could i do to get it working? And if there is another way to approach this, i'm open for suggestions. Just started learning Shopware :)

Upvotes: 0

Views: 612

Answers (1)

dneustadt
dneustadt

Reputation: 13161

At first glance this looks to be correct. The error message seems to be incomplete. It should say after the query why it failed, be it a syntax error or a wrong foreign key. The latter would be my best guess at what could fail the moment, have you checked all the uuids to exist in their corresponding tables like tax, currency and sales_channel?

Upvotes: 2

Related Questions