title: Applying coupon to WooCommerce order programmatically
body: <p>I created an order in WooCommerce programmatically using <code>wc_create_order()</code>, and I also would like to apply coupon in some cases. I did it on my own way, but I'm not sure it's correct:</p>
  
  <pre><code>// Products come in $_GET
  foreach($_GET['product'] as $p) {
      if(!isset($p['name'])) {
          continue;
      }
  
      if(!empty($p['name'])) {
          // Discount
          $discount = (int)$p['discount']/100;
  
          $product-&gt;set_name($p['name']);
  
          // I set the discount price
          $product-&gt;set_price(str_replace(' ', '', $p['price'])*(1-$discount));
          $discount_total += str_replace(' ', '', $p['price'])*$discount;
          $order-&gt;add_product( $product, $p['qty']);
      }
  }
  
  // I apply the coupon code
  if(!empty($discount)) {
      $order-&gt;add_coupon('vip', $discount_total);
  }
  </code></pre>
  
  <p>It's almost perfect, but I don't see the discount amount under summary and items table.</p>
  
  <p>Is there an other way to apply the coupon?</p>
  
  <p><a href="https://i.stack.imgur.com/Ux1Z5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ux1Z5.png" alt="enter image description here"></a></p>