title: Create an order programmatically in Woocommerce Subscription
body: <p>I want to create an order (programmatically) of a Subscription Product. I found code from this question: <a href="https://stackoverflow.com/questions/36729701/programmatically-creating-new-order-in-woocommerce">Programmatically creating new order in Woocommerce</a>, which I used to try make it work. </p>
  
  <p>I was not able to make it work neither with a "Simple Subscription" nor a "Simple Product".</p>
  
  <pre><code> function create_vip_order() {
  
    global $woocommerce;
    $product_id = 123;
  
    $address = array(
        'first_name' =&gt; '111Joe',
        'last_name'  =&gt; 'Conlin',
        'company'    =&gt; 'Speed Society',
        'email'      =&gt; 'joe@testing.com',
        'phone'      =&gt; '760-555-1212',
        'address_1'  =&gt; '123 Main st.',
        'address_2'  =&gt; '104',
        'city'       =&gt; 'San Diego',
        'state'      =&gt; 'Ca',
        'postcode'   =&gt; '92121',
        'country'    =&gt; 'US'
    );
  
    // Now we create the order
    $order = wc_create_order();
  
    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
    $order-&gt;add_product( get_product( $product_id ), 1 ); // This is an existing SIMPLE product
    $order-&gt;set_address( $address, 'billing' );
    //
    $order-&gt;calculate_totals();
    $order-&gt;update_status("Completed", 'Imported order', TRUE);
  
  }
  
  add_action( 'woocommerce_checkout_process', 'create_vip_order' );
  </code></pre>
  
  <p>If you have any solution I would really appreciate it. :)</p>
  
  <p>Best Regards, Ledung</p>
  
  <p>EDIT: It seem the way my Product were set up was causing the function not to work. I am currently using a Simple Subscription Product, with the "Virtual" parameter checked and a subscription length of 3 months. </p>
  
  <p>Here you can see the current code that work with my setup:</p>
  
  <pre><code>    function create_test_sub() {
  
      $email = 'test@test.se';
  
      $start_date = date( "Y-m-d H:i:s", strtotime( "now" ) );
  
  
      $address = array(
          'first_name' =&gt; 'Firstname',
          'last_name'  =&gt; 'Secondname',
          'company'    =&gt; 'Company',
          'email'      =&gt; $email,
          'phone'      =&gt; '',
          'address_1'  =&gt; 'Streetname 123',
          'address_2'  =&gt; '',
          'city'       =&gt; 'City',
          'postcode'   =&gt; '12345',
          'country'    =&gt; 'Country'
      );
  
      $default_password = wp_generate_password();
  
      if ( ! $user = get_user_by( 'login', $email ) ) $user = wp_create_user( $email, $default_password, $email );
  
      // I've used one product with multiple variations
  
      $parent_product = wc_get_product( 3901 );
  
      $args = array(
          'attribute_billing-period'    =&gt; 'Yearly',
          'attribute_subscription-type' =&gt; 'Both'
      );
  
      $product_variation = $parent_product-&gt;get_matching_variation( $args );
  
      $product = wc_get_product( $product_variation );
  
      $product_month_length = $parent_product-&gt;subscription_length;
      $end_date = date( "Y-m-d H:i:s", strtotime( $product_month_length . "months" ) );
  
      $quantity = 1;
  
      // As far as I can see, you need to create the order first, then the sub
  
      $order = wc_create_order( array( 'customer_id' =&gt; $user-&gt;id ) );
  
      $order-&gt;add_product( $parent_product, $quantity, $args );
      $order-&gt;set_address( $address, 'billing' );
      $order-&gt;set_address( $address, 'shipping' );
  
  
      $order-&gt;calculate_totals();
  
      $order-&gt;update_status( "completed", 'Imported order', TRUE );
  
      // Order created, now create sub attached to it -- optional if you're not creating a subscription, obvs
  
      // Each variation has a different subscription period
  
      $period = WC_Subscriptions_Product::get_period( $parent_product );
      $interval = WC_Subscriptions_Product::get_interval( $product );
  
  
      $sub = wcs_create_subscription( array( 'status' =&gt; 'active', 'order_id' =&gt; $order-&gt;id, 'billing_period' =&gt; $period, 'billing_interval' =&gt; $interval, 'start_date' =&gt; $start_date, 'end' =&gt; $end_date ) );
  
      if ( ! is_wp_error( $sub ) ){
        $sub-&gt;add_product( $parent_product, $quantity, $args );
  
        $sub-&gt;set_address( $address, 'billing' );
        $sub-&gt;set_address( $address, 'shipping' );
        $dates = array(
          'end' =&gt; $end_date,
        );
        $sub-&gt;update_dates( $dates );
        $sub-&gt;calculate_totals();
      }
      WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );
  
      print "&lt;a href='/wp-admin/post.php?post=" . $sub-&gt;id . "&amp;action=edit'&gt;Sub created! Click here to edit&lt;/a&gt;";
      $returnarray = array(
        'subscription_id' =&gt; $sub-&gt;id,
        'order_id'        =&gt; $order-&gt;id,
      );
  
      return  $returnarray;
  }
  $test = create_test_sub();
  </code></pre>