I’m trying to create invoice number on orders which are payed with bacs payment method. I want to get the last invoice number from existing orders and than add +1 to the value which should be added to new order:

Here is my code

function my_custom_checkout_field_update_order_meta( $order_id ) {
$order = new WC_Order($order_id);

global $wpdb;
global $post;

$last_order = get_posts( array(
    'post_type'   => 'shop_order',
    'posts_per_page' => '1',
    'meta_key'  =>  '_payment_method',
    'meta_value'    =>  'bacs',
    'orderby'   => 'post_date',
    'order' =>  'DESC'

$last = $last_order[0]->ID;

$invoice = get_post_meta($last, '_billing_invoice_number');

if($_POST['payment_method'] == 'bacs') {
    update_post_meta( $order_id, '_billing_invoice_number', $invoice[0] + 1);

My code is always returning just 1. I have tried to save just $invoice[0] but it returns an empty value.

What am I doing wrong?

Read more here: Getting postmeta of last order and adding it to new order


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: