Search code examples
phpwordpresstemplateswoocommerceorders

Get some order and order items data in Woocommerce emails


I am currently customising the emails that customers receive after a purchase on my website (customer-processing-order, customer-completed-order etc etc). I have copied the email folder from Woocommerce to my child theme.

What I am using a doing is using my own template and then using Woocommerce Classes/API's/Functions to bring in the relevent details of the customers order.

So far I have managed to add in information for:

  • Customer Name
  • Customer Order No
  • Order Date
  • Shipping/Billing Address
  • Product Item Name
  • Product Item Quantity

I have done this mostly by using the variable function/class $order (I.E id; ?>, billing_first_name; ?>)

My problem is I am having no success in trying to get the product / item (individual) price to show. I have looked and searched for all the ways to do this but EVERYTHING I have tried has so far failed.

I am also having trouble with printing the Sub Total, Shipping, Payment Method and the Total (Overall Cost). Maybe I am going about it in the wrong way?

These are links I have used to try and guide me

How to get WooCommerce order details

https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/

Any help would be fantastic. Here is my Code ...

<?php           
foreach ($order->get_items() as $item_id => $item_data) { 
    $product = $item_data->get_product();

    //PRODUCT NAME
    $product_name = $product->get_name(); 
    echo $product_name// THIS WORKS AND PRINTS CORRECTLY
    //PRODUCT QUANTITY
    $get_quantity = WC_Order_Item::get_quantity();  
    echo $get_quantity // THIS WORKS AND PRINTS CORRECTLY

    //PRODUCT PRICE
    $get_price = new WC_Order_Item_Fee::get_amount( $context );
    echo $get_price // THIS DOES NOT WORK

    // TRYING BELOW ALSO DOES NOT WORK ...

    $getproduct = wc_get_product( $item['product_id'] );
    $price = $getproduct->get_price();  

    //HOW WOULD I GET :
    /** SUBTOTAL , SHIPPING , PAYMENT METHOD AND TOTAL **/  

?>

Solution

  • There are some errors in your code…

    1) For the order items data:

    // Loop though order line items      
    foreach ($order->get_items() as $item_id => $item ) { 
    
        // PRODUCT NAME
        $product_name = $item->get_name(); 
        echo $product_name
    
        // PRODUCT QUANTITY
        $quantity = $item->get_quantity();  
        echo $quantity;
    
        // Get the WC_Product Object instance
        $product = $item->get_product();
    
        // PRODUCT PRICE
        $product_price = $product->get_price();
        echo $product_price;
    
        // LINE ITEM SUBTOTAL (Non discounted)
        $item_subtotal = $item->get_subtotal();
        echo $item_subtotal;
    
        // LINE ITEM SUBTOTAL TAX (Non discounted)
        $item_subtotal_tax = $item->get_subtotal_tax();
        echo $item_subtotal_tax;
    
        // LINE ITEM TOTAL (discounted)
        $item_total = $item->get_total();
        echo $item_total;
    
        // LINE ITEM TOTAL TAX (discounted)
        $item_total_tax = $item->get_total_tax();
        echo $item_total_tax;
    
    endforeach; 
    

    See for reference: Get Order items and WC_Order_Item_Product in Woocommerce 3


    2) For the Order data:

    // PAYMENT METHOD:
    $payment_method = $order->get_payment_method(); // The payment method slug
    $payment_method_title = $order->get_payment_method(); // The payment method title
    
    // SHIPPING METHOD:
    $shipping_method = $order->get_shipping_method(); // The shipping method slug
    
    // SHIPPING TOTALS:
    $shipping_total     = $order->get_shipping_total(); // The shipping total
    $shipping_total_tax = $order->get_shipping_tax(); // The shipping total tax
    
    // DISCOUNT TOTAL
    $shipping_total     = $order->get_total_discount(); // The discount total
    
    // ORDER TOTALS
    $total     = $order->get_total(); // The order total
    $total_tax = $order->get_total_tax(); // The order tax total