Okay
  Public Ticket #2750643
adding prefix name to order number
Closed

Comments

  •  1
    low weihao started the conversation

    Hi Stefan ,i had just saw your document site there got code that can be add prefix word in front of  order for b2b or b2c ,can i use it specific for some user role or group

  •  2,219
    WebWizards replied

    Hi Glendon,

    Yes, you can do that.


    Some examples:

    1) Show for every order, the user's group as prefix:

    add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
    function change_woocommerce_order_number( $order_id ) {
        // get if customer is B2B
        $order = wc_get_order($order_id);
        $customer_id = $order->get_customer_id();
        $customer_b2b = get_user_meta($customer_id,'b2bking_b2buser', true);
        if ($customer_b2b === 'yes'){
            $customer_group = get_user_meta($customer_id,'b2bking_customergroup', true);
            $title = get_the_title($customer_group);
            $prefix = $title.'-';
            $suffix = '';
            $order_id = $prefix . $order_id . $suffix;
        }
        return $order_id;
    }
    

    Looks like:

    9357297562.png

    2) Show Prefix only for VIP Group (VIP group has ID 27):

    add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
    function change_woocommerce_order_number( $order_id ) {
        // get if customer is B2B
        $order = wc_get_order($order_id);
        $customer_id = $order->get_customer_id();
        $customer_b2b = get_user_meta($customer_id,'b2bking_b2buser', true);
        $customer_group = get_user_meta($customer_id,'b2bking_customergroup', true);
        if ($customer_b2b === 'yes' && $customer_group == 27){
            $prefix = 'PRO'.'-';
            $suffix = '';
            $order_id = $prefix . $order_id . $suffix;
        }
        return $order_id;
    }
    

    Kind regards,

    Stefan