Fetch Woocommerce order information when an order is placed using PHP

Fetch Woocommerce order information when an order is placed

I recently needed to generate an XML file (Yes, an XML file, don’t ask why) for every order placed on a WordPress and Woocommerce based website. Wow.. headaches for ages thanks to using the incorrect Woocommerce hook but I’ll get into that in the tutorial.

Let’s start with some basics…

Understanding Hooks

There are two types of Hooks, Actions and Filters. They allow you to interact with or modify any (well mostly any) existing functionality within WordPress. Woocommerce has its own set of actions and filters which allow you to interact with its own E-Commerce functionality.

In this tutorial I’m using the Woocommerce hook “woocommerce_checkout_order_processed” as it runs after the order is processed, you can find more here. It is extremely important to use the correct hook as hooks run at different times in the process, If you use a hook that runs before the order is processed  then you will not get the order information as the order does not yet exist. I used the incorrect hook and thus ended up not getting the information of the products that were ordered. The below ( using php ) sets up the function “create_xml_order” to run every time an order is successfully processed.

add_action('woocommerce_checkout_order_processed', 'create_xml_order', 10, 1);

Now, we put together our “create_xml_order” function…

function create_xml_order($order_id) {
    //get order data      
    $order = new WC_Order( $order_id );          
    //Some order Info you can use 
    $order_number = $order->get_order_number();    
    $email = $order->get_billing_email();    
    $billing_name = $order->get_formatted_billing_full_name();
    $company_name = $order->get_billing_company();    
    $contact_number = str_replace(' ', '', $order->get_billing_phone());
    $address_1 = $order->get_billing_address_1();     
    $address_2 = $order->get_billing_address_2();    
    $city = $order->get_billing_city();     
    $postal_code = $order->get_billing_postcode();    
    $instructions = $order->get_customer_note();     
    $payment_method = $order->get_payment_method();     
    $payment_date = $order->get_date_paid(); 
    //This total will only be the cart items costs     
    $order_total = wc_format_decimal($order->get_subtotal(), 2);     
    //This total will include other costs like shipping     
    $order_total_inc = wc_format_decimal($order->get_total(), 2);   
    foreach ($order->get_items() as $item_id => $item_data) {        
        //Get the product ID        
        $product_id = $item->get_product_id(); 
        // Get the WC_Product object         
        $product = $item->get_product();         
        $product_sku    = $product->get_sku();         
        $description = get_post($item['product_id'])->post_content; 
        // Name of the product
        $item_name    = $item->get_name();    
        $quantity     = $item->get_quantity();           
        $tax_class    = $item->get_tax_class();  
        // Line subtotal (non discounted)       
        $line_subtotal     = $item->get_subtotal();
        // Line subtotal tax (non discounted)      
        $line_subtotal_tax = $item->get_subtotal_tax(); 
        // Line total (discounted)     
        $line_total        = $item->get_total(); 
        // Line total tax (discounted)        
        $line_total_tax    = $item->get_total_tax();        
    //You can now add all the above to a var for sending via email     
    // Or save it all to a DB as previously mentioned. 


Keep in mind that this tutorial can be used to do anything once an order is created, send another email, save the data to a file, save it to a database… blah blah blah.

Simply copy the above and add it to the active theme’s functions.php file, apply the functionality you need it to do and test by ordering a product from the site. You can also add this to your own custom plugin and make the magic happen via a plugin on multiple sites.

Hope this helps, Nathaniel.


Other Posts