skip to Main Content

I have made a simple webshop with woocommerce, with three payment methods. iDeal and by direct bank transfer and on account. The order ID is created based on the payment method. for example, if payment is made with iDEAL, the order id becomes ID190100; if payment is made on account, the order id becomes RK190100. I get this working with the plugin
“Sequential Order Numbers for WooCommerce” from BeRocket but these are already created before the payment is complete. The order ID must only be finalized once the payment has been made. Now orders that have not yet paid, and may not be paying, will receive a fixed order ID. So is it possible to create a temporary order id and when the order is completed change the order id based on payment method?

2

Answers


  1. Try this. It’s very quick example. Hope help.

    add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
    
    function wc_change_order_id( $order_id ) {
    
        $order = wc_get_order( $order_id );
    
        $method = $order->get_payment_method(); // need check this
    
        if ( $method === 'account' ) {
    
            $number = 'ID' . $order->get_id();
    
            $order->set_id( $number );
    
            $order->save();
    
        }
    }
    
    Login or Signup to reply.
  2. Woocommerce by default will use the post ID of the order for the order ID. This is evident when viewing the WC_Order::get_order_number() method. If you want to use a custom order number to display, you’ll need to add a filter on woocommerce_order_number to load in a different value.

    An example script would be:

    add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
    
    function wc_change_order_id( $order_id ) {
    
        $order = wc_get_order( $order_id );
    
        $method = $order->get_payment_method(); // need check this
    
        if ( $method === 'account' ) {
    
            $number = 'ID' . $order->get_id();
    
            $order->update_meta_data('_new_order_number', $number );
    
        }
    }
    add_filter('woocommerce_order_number', function($default_order_number, WC_Order $order) {
       //Load in our meta value. Return it, if it's not empty.
       $order_number = $order->get_meta('_new_order_number');
       if(!empty($order_number)) {
          return $order_number;
       }
       // use whatever the previous value was, if a plugin modified it already.
       return $default_order_number;
    },10,2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search