OiO.lk Blog PHP WooCommerce custom order number prepending product sku
PHP

WooCommerce custom order number prepending product sku


I am building a WooCommerce web-store with different and I want to get different series of order numbers or invoice numbers based on this thread.

I need a similar solution. The codes I’ve tried:

1st try

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );

function change_woocommerce_order_number( $order_id ) {
    $order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_key => $item )
    $product = $order->get_product_from_item( $item );
    $sku = $product->get_sku();
    $new_order_id = $sku . $order_id;
    return $new_order_id;
}

2nd try:

function change_woocommerce_order_number($order_id) {
    $order = new WC_Order($order_id);
    $items = $order->get_items();
    $first_item = $items[0];
    $product_id = $first_item->get_product_id();
    $product = new WC_Product($product_id);
    $sku = $product->get_sku();

    return $sku.$order_id;
}
add_filter('woocommerce_order_number', 'change_woocommerce_order_number');

Now, whenever I use those code snippets, the checkout page stops working and gives the error "there has been a critical error on this website". I’m a complete novice in regard to custom coding.



You need to sign in to view this answers

Exit mobile version