Romain
Romain

Reputation: 3

Add product name to order number in WooCommerce

I've searched for a solution to prefix my order number by the ordered product(s) name(s) or first letters.

I've tried to use a function on functions.php file to prefix my order number with the woocommerce_order_number filter but I can't get the product name in the order array (it's protected).

Is there a way to return this on my function using a loop for example ?

Upvotes: 0

Views: 724

Answers (1)

Ali_k
Ali_k

Reputation: 1661

You can do this:

function filter_woocommerce_order_number( $this_get_id, $instance ) { 
    $order = new WC_Order( $this_get_id );
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_name = preg_replace('/\s+/', '_', $item['name']);;
        break;
    }
    $new_id = $product_name.'_'.$this_get_id;

    return $new_id; 
}; 

add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 ); 

Upvotes: 1

Related Questions