How to Get Order Details After Checkout In WooCommerce

WooCommerce Get Order Details After Checkout
After WooCommerce checkout, you can customize the order after the checkout to display the various order details or add more features to the order.

For you to customize the order or add new features to the order you need to know how you can get order details after checkout.

In this post, I will share with you briefly how you can get order details after checkout.

WooCommerce Get Order Details After Checkout

To get the order details after checkout you need to get the order ID after checkout and you can achieve this using the code snippet below :

global $wp;

//Get Order ID
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

After you get the order ID you can now obtain the order object and then loop through the order to get the items in the order as in the code snippet below :

// Get an instance of the WC_Order object

     $order = new WC_Order( $order_id );

        $items = $order->get_items();

          foreach ( $items as $key => $item ): 

              //$product_id = $item['product_id'];
                  $product = $item->get_product();

                      if ( $product ) {
                         $productsInOrderIds[] = $product->get_id();
                       }

                return $productsInOrderIds; 

         endforeach; 

As you can see in the code above we have obtained the order details and the most important detail we need from the order is the products that are in the order.

We have used the get_items() function to get products in the order and we have gone ahead to obtain the products order IDs that we can repurpose in the code for more comparison as in the case of redirect after checkout based on the product in the order.

Similar Articles

  1. How to Hide Tax Label In WooCommerce