Modifier la nomenclature des factures

Modifier la nomenclature des factures

Pensez à créer un THEME-CHILD.
Placer l’un des bouts de code ci-dessous dans votre fichier /wp-content/themes/THEME-CHILD/functions.php.
Sinon vous pouvez l’ajouter dans un fichier qui est inclus par fonction.php, ou créer un module.

/*=====================================================
    Change Woocommerce invoice number to : F-YEAR-MONTH-N
    ex : F-2018-11-46
=====================================================*/

function judev_woocommerce_change_order_number( $order_id ) {
      
   global $wpdb;
   
   // Settings
   $prefix       = 'F-';
   $suffix       = '';
   $new_order_id   = $order_id; // Default value
   
   // Order informations
   $order        = wc_get_order( $order_id );
   $order_date   = $order->get_date_created();
   
   if (!empty($order_date)){ // When we create a new order, date created is empty
      
      $date_created = $order_date->date( 'Y-m' );

      // Get all orders from the given date then get the position of the current order
      $query = "
         SELECT ID FROM {$wpdb->prefix}posts
         WHERE post_date LIKE '%".$date_created."%' AND post_type='shop_order'
         ORDER BY ID
      ";
      $result       = $wpdb->get_results( $query );
      $count        = 0;
      foreach( $result as $index => $id ) {
         if( strval($order_id) == $id->ID ) {
            $count = $index + 1;
            break;        
         }
      }
      // -

      // Construct order number
      $new_order_id = $prefix . $date_created . "-" . $count . $suffix;
   }
   
   // return
   return $new_order_id;
 }

add_filter( 'woocommerce_order_number', 'judev_woocommerce_change_order_number' );