1

I want to add shop name in order number.

For example, there are two vendors on my marketplace:

  • Sepatu Shop
  • Baju Shop

If the customer orders products from both the Sepatu Shop and Baju Shop vendors, the respective order numbers must be like this:

  • VK/4571-Sepatu Shop
  • VK/4572-Baju Shop

The structure must be: {prefix}/{order_id}-{vendor_name}

The code I've tried so far is this but it doesn't work:

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
    $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
    $prefix = 'VK/';
    $suffix = $shop_name;
    $new_order_id = $prefix . $order_id . $suffix;
    return $new_order_id;
}
Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32

1 Answers1

0

The Dokan plugin manages vendors as users with the seller (Vendor) user role.

When the customer places the order, an "admin" order is created which includes the products of all vendors (including shipping costs, etc.) and as many other sub-orders as there are vendors from which the customer is buying.

For each sub-order, the post meta _dokan_vendor_id is also stored, the value of which is the user id of the vendor.

Also, if the frontend of the marketplace also shows products that do not belong to any vendor, you can add an additional check to verify that that user actually has the "seller" (Vendor) user role.

Because the plugin stores the post meta _dokan_vendor_id with the product author id, it does not check the seller user role.

Then, you can change the order id like this:

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 99, 2 );
function change_woocommerce_order_number( $order_id ) {

    // Gets the vendor id from the order.
    $vendor_id = get_post_meta( $order_id, '_dokan_vendor_id', true );
    
    // For vendor orders only
    if ( $vendor_id && user_can( $vendor_id, 'seller' ) ) {
        $vendor      = get_user_by( 'id', $vendor_id );
        $vendor_name = $vendor->display_name;
        return "VK/$order_id-$vendor_name";
    }

    return $order_id;

}

The code has been tested and works. Add it to your active theme's functions.php.

NOTE

Obviously the order number modified through the woocommerce_order_number hook is only in display. The order ID on the database is always and only numeric. So if you want to get the order number with this structure (for example in the export of orders) you will need to create a custom function.

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32