title: woocommerce unique order number or invoice number for different products
body: <p>I am building a WooCommerce web-store which sells four different products.</p>
  
  <p>How can I get different series of order numbers or invoice numbers based on the type of product?</p>
  
  <p>For example: all order IDs or invoice numbers for product 'AA' should start with AA and have a format like AA001, AA002, AA003, AA004...</p>
  
  <p>Similarly, either the order ID or the invoice number for product 'BB' should go like BB001, BB002, BB003 etc.</p>
  
  <p>If this is not possible, I can also live with adding a custom prefix to the invoice number or order number based on the type of product.</p>
  
  <p>For example : invoices / order numbers for product 'AA', 'BB' should include 'AA' or 'BB' as the prefix depending on what is sold.</p>
  
  <p>So my invoice or order number sequence may look like AA001, BB002, CC003, AA004, CC005... i.e. the normal sequence of invoice numbers continues, just the prefix is added.</p>
  
  <p>Edit : Each order will have only one product.</p>
  
  <p>I am bumping my way around in PHP; so any help or pointers on how this can be achieved will be very much appreciated.</p>
  
  <p>Edit 2: The code below is adding a prefix to the order ID everywhere, but I need to get the actual SKU of the order into the prefix field.</p>
  
  <pre><code>add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
  
  function change_woocommerce_order_number( $order_id ) {
      $prefix = 'the product sku should go here';
      $new_order_id = $prefix . $order_id;
      return $new_order_id;
  }
  </code></pre>
  
  <p>Edit 3 : This code works . . .</p>
  
  <pre><code>add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
  
  function change_woocommerce_order_number( $order_id ) {
      $order = new WC_Order( $order_id );
      foreach ( $order-&gt;get_items() as $item_key =&gt; $item )
      $product = $order-&gt;get_product_from_item( $item );
      $sku = $product-&gt;get_sku();
      $new_order_id = $sku . $order_id;
      return $new_order_id;
  }
  </code></pre>
  
  <p>Edited John's answer below and added a bit of code from here : <a href="https://gist.github.com/maxrice/7918315" rel="nofollow noreferrer">https://gist.github.com/maxrice/7918315</a></p>