How do I set the value for field_stock? I use module commerce_stock on Drupal 8.

I get back product data and I create a product with its data. I have to create a field field_stock (Stock level) in variations, which I create with the following code.

$variation =  ProductVariation::create(['type' =&gt; 'default', 'sku' =&gt; 'sku1', 'status' =&gt; TRUE, 'price' =&gt; $price]);


How do I add the value to field_stock?
I found a solution to add the value to field_stock (Stock level).

$variation =  ProductVariation::create(['type' =&gt; 'default', 'sku' =&gt; $json['22107'], 'status' =&gt; TRUE, 'price' =&gt; $price]);
$variation-&gt;save();
//Stock
$stock = ['stock' =&gt; ['value' =&gt; 100]];
$var = \Drupal::entityManager()-&gt;getStorage('commerce_product_variation')-&gt;load($variation-&gt;id());
$var-&gt;set('field_stock', [$stock]);
$var-&gt;save();


My field_stock in database is empty but this code create a commerce_stock_transaction in database with my quantity.