Programmatically create a discount code Does anyone know how to programmatically create a discount code in Drupal  Commerce?
The best solution is to always review the tests. The tests create promotions and coupons programmatically.

Here's how to create a promotion.

$promotion = Promotion::create([
  'name' =&gt; 'Promotion test',
  'order_types' =&gt; ['default'],
  'stores' =&gt; [$this-&gt;store-&gt;id()],
  'offer' =&gt; [
    'target_plugin_id' =&gt; 'order_percentage_off',
    'target_plugin_configuration' =&gt; [
      'amount' =&gt; '0.10',
    ],
  ],
  'start_date' =&gt; '2017-01-01',
  'status' =&gt; TRUE,
]);
$promotion-&gt;save();


And then coupons.

$coupon = Coupon::create([
  'promotion_id' =&gt; $promotion-&gt;id(),
  'code' =&gt; 'coupon_code',
  'usage_limit' =&gt; 1,
  'status' =&gt; TRUE,
]);
$coupon-&gt;save();