Viewing 4 replies - 1 through 4 (of 4 total)
  • WooCommerce uses the ID number for the order when it’s created in the database, which is why they are not sequential.

    A quick search for “WooCommerce sequential order numbers” should get you going.

    Thread Starter kiranthory

    (@kiranthory)

    Hi Robin,

    Thanks for the prompt reply!

    We can’t add plugin because the plugin files will affect the website speed. So, I have added the following code:

    // Add sequential order number
    add_filter( ‘woocommerce_order_number’, ‘prefix_add_sequential_order_number’, 1, 2 );
    function prefix_add_sequential_order_number( $order_id, $order ) {
    global $wpdb;

    // Check if order number already exists
    $existing_order_number = $wpdb->get_var( $wpdb->prepare( "
        SELECT meta_value 
        FROM {$wpdb->prefix}postmeta 
        WHERE meta_key = '_order_number' 
        AND meta_value > 0
        ORDER BY meta_value DESC
        LIMIT 1
    " ) );
    
    $sequential_order_number = $existing_order_number ? $existing_order_number + 1 : 1;
    
    return apply_filters( 'woocommerce_order_number', $sequential_order_number, $order );

    }

    // Display sequential order number in admin order list
    add_filter( ‘manage_edit-shop_order_columns’, ‘prefix_add_sequential_order_column’ );
    function prefix_add_sequential_order_column( $columns ) {
    $columns[‘sequential_order’] = ‘Sequential Order’;
    return $columns;
    }

    add_action( ‘manage_shop_order_posts_custom_column’, ‘prefix_display_sequential_order_column’, 2 );
    function prefix_display_sequential_order_column( $column ) {
    global $post, $woocommerce, $the_order;

    if ( 'sequential_order' === $column ) {
        $order_id = $the_order->get_id();
        $order_number = get_post_meta( $order_id, '_order_number', true );
    
        if ( ! empty( $order_number ) ) {
            echo $order_number;
        } else {
            echo 'N/A';
        }
    }

    }

    Please check the above code and let me know if this is correct or not.

    Thank You.

    Plugin Support Shameem – a11n

    (@shameemreza)

    Hi @kiranthory,

    For reference, these particular forums are meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Plugin Support Shameem – a11n

    (@shameemreza)

    Hi @kiranthory,

    I’m marking this topic as “resolved” due to recent inactivity. If more assistance is needed, feel free to post back here or open a new topic.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Order sequence is not proper’ is closed to new replies.