Site icon Hip-Hop Website Design and Development

Create Product with Woocommerce API, but Variations are not linked

I create a product with variants via Woocommerce API. These are also created, but not linked in the backend. The variants are displayed in the frontend, but not with different prices. Only when I link them manually in the backend. Why does this not work?

$data = [
    'name' => $title,
    'type' => 'variable',
    'regular_price' => $price,
    'sku' => $sku,
    'attributes'  => [
        [
            'name' => 'Size',
            'variation' => true,
            'visible'   => true,
            'options'   => [ '13x18', '20x30'],
        ],

Save the variation:

    $variation_data = [
        'regular_price' => '4.00',
        'attributes'    => [
            [
                'id'     => 3,
                'option' => '13x18',
            ],
        ],
    ];

    $woocommerce->post( "products/$newProduct->id/variations", $variation_data);

And the same request for the second variation:

    $variation_data = [
        'regular_price' => '7.00',
        'attributes'    => [
            [
                'id'     => 3,
                'option' => '20x30',
            ],
        ],
    ];

    $woocommerce->post( "products/$newProduct->id/variations", $variation_data);