I am using a nifty piece of code (thanks 7uc1f3r) to split an order if it contains any backordered products: Split a WooCommerce order and create a new order if the original order has products in backorder.
This works great to create the second order at checkout, but it only shows the original order on the Thank You page. As mentioned by Pathfinder, there is a way to hook into the session data, pass the new order id, and show the newly created order details below the OG order table using woocommerce_after_order_details
.
I tried to get order data from the session, but I am not able to get the order_id or any order data;
this shows an empty array:
function sa_woocommerce_checkout_backorder($order){
$session = new WC_Session_Handler();
$session_data = $session->get_session_data();
echo var_dump($session_data);
}
add_action( 'woocommerce_after_order_details', 'sa_woocommerce_checkout_backorder', 10, 3 );
I have also tried to get the order data from the customer id using the wc_get_customer_last_order
method, (shoutout again 7uc1f3r)
Get products of the customer’s last order in WooCommerce:
if ( is_user_logged_in() ) {
$customer = WC()->session->get('customer');
$customer_id = $customer['id'];
$last_order = wc_get_customer_last_order( $customer_id );
// NOT empty
if ( ! empty( $last_order ) ) {
// Initalize
$product_ids = array();
// Loop
foreach ( $last_order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_product_id();
$product_name = $item->get_name();
}
echo $product_name;
}
}
}
But this pulls in the OG order info and not the newly created order.
What is the best method to pull the newly created order data from a session object?