1

I'm customizing my Woocommerce email templates now and right now I only have acess to the order number, I'd like to get the price and quantity for my order but I haven't managed to figure out how.

<?php 

    // Getting the order object "$order"

$order = wc_get_order( $order_id );

// Getting the items in the order

$order_items = $order->get_items();

// Iterating through each item in the order

foreach ($order_items as $item_id => $item_data) {

    // Get the item quantity, something is wrong here..

    $item_quantity = $order->get_item_meta($item_id, '_qty', true);
    echo $item_quantity;
    // Get the price, doesn't work either..
    $item_total = $order->get_item_meta($item_id, '_line_total', true)


}

?>

The issue is that I'm unable to get the quantity and price which I can display in my order confirmation email that I'm customizing, I'm currently running woocommerce 3.2.5

Will Smith
  • 13
  • 3

1 Answers1

0

As you have the $order object, you can get the product title this way:

<?php 
    // Loop through order items
    foreach($order->get_items() as $items){
        $product = $items->get_product(); // The product object
        $product_name = $product->get_name(); // The product Name
        $quantity = $items->get_quantity(); // The product Quantity
        $line_total = $items->get_total(); // The line item total

        // Display the product name
        echo '<p>'.$product_name.'</p>';

        // Display the product quantity
        echo '<p>'.$quantity.'</p>';

        // Display the product name
        echo '<p>'.$line_total.'</p>';

        // Get the raw output to check
        echo '<pre>'; print_r(echo $items->get_data() ); '</pre>';
    }
?>

Remember that an order can have many items (so different product names).


Related trhread: Get Order items and WC_Order_Item_Product in Woocommerce 3

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi thank you for the answer, to clarify a bit I just watmed to say that the order only contains 1 object which is a subscription – Will Smith Dec 24 '17 at 11:14
  • @WillSmith Ok Thanks Mr Smith, I will make a test on a subdcription order… **Questions:** On which template are you planing to make the customization? For what email notification? You should update your question please, adding all details that we can't guess. Thanks – LoicTheAztec Dec 24 '17 at 11:29
  • Your solution worked perfectly, just had to do some tweaks. However I'm unable to get quantity and product price using my code above – Will Smith Dec 24 '17 at 12:01
  • It throws an error on line 6 "Uncaught Error: Call to a member function get_items() " Seems like $order = wc_get_order( $order_id ); causes the problem – Will Smith Dec 24 '17 at 12:26
  • @WillSmith I think that the `$order` object already exist… so you don't need it as remember that you have already `$order->order_number`… so that means that `$order` object already exist. – LoicTheAztec Dec 24 '17 at 12:41
  • Great! Is there anyway you could include so it echoes the discount amount in a percentage or the amount in dollars? – Will Smith Dec 24 '17 at 13:11
  • @WillSmith I have updated my answer again check and try the new code line at the end (the raw output for testing) to see what you get related to prices. You can also try to replace `$item->get_total()` by `$item->get_subtotal()` to see what you get – LoicTheAztec Dec 24 '17 at 13:12
  • It shows an array afterwards, could you possibly move this to chat if you can? Thank you! – Will Smith Dec 24 '17 at 13:23
  • @WillSmith if you want to chat, do it on skype, my ID is `marsloic`… – LoicTheAztec Dec 24 '17 at 13:38