1

I have recently just created customized emails for Woocommerce. I have everything working except one problem. Before I was bringing/getting the "product image" I was able to load the "product price" with no problems.

However now I am getting the product image, I am having trouble getting the "product price" and orders are not processed and completed.

Here is the PHP code I am using to gather all the information to display in the email.

<?php               
    foreach ($order->get_items() as $theitem_id => $theitem ) { 

    // PRODUCT NAME
    $product_name = $theitem->get_name(); 
    // PRODUCT QUANTITY
    $quantity = $theitem->get_quantity();  


    // LINE ITEM SUBTOTAL (Non discounted)
    $item_subtotal = $theitem->get_subtotal();
    $item_subtotal = number_format( $item_subtotal, 2 );

    // LINE ITEM TOTAL (discounted)
    $item_total = $theitem->get_total();
    $item_total = number_format( $item_total, 2 );


    $product_id = $theitem['product_id'];
    $product = wc_get_product( $product_id );

    // PRODUCT IMAGE
    $prodimage = $product->get_image( array( 200, 200 ) )

    // PRODUCT PRICE
    $product_price = $product->get_price(); 
?>

I am sure I am not far away from getting this right as when I comment out the "product price" PHP code then everything works fine (without the price being gathered obviously)

Any help would be great , thanks very much

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Jamie Clague
  • 55
  • 2
  • 8

1 Answers1

0

Since Woocommerce 3, the product ID is wrong (and the product Object too)… Try instead:

<?php
    // Loop through order items
    foreach ($order->get_items() as $item_id => $item ) { 

    // PRODUCT NAME
    $product_name = $item->get_name(); 
    // PRODUCT QUANTITY
    $quantity = $item->get_quantity();  

    // LINE ITEM SUBTOTAL (Non discounted)
    $item_subtotal = $item->get_subtotal();
    $item_subtotal = number_format( $item_subtotal, 2 );

    // LINE ITEM TOTAL (discounted)
    $item_total = $item->get_total();
    $item_total = number_format( $item_total, 2 );

    // Get an instance of the product Object
    $product = $item->get_product(); // <==== HERE
    $product_id = $product->get_id(); // <==== and HERE

    // PRODUCT IMAGE
    $product_image = $product->get_image( array( 200, 200 ) );

    // PRODUCT PRICE
    $product_price = $product->get_price(); 
?>

It should better work now.

Related: Get Order items and WC_Order_Item_Product in Woocommerce 3

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Unfortunately that does not seem to work. When I use this php code, the order I place does not process correctly after I submit my address details. – Jamie Clague Feb 19 '19 at 15:11
  • @user3060945 This is the right way for [order items](https://stackoverflow.com/questions/45706007/get-order-items-and-wc-order-item-product-in-woocommerce-3/45706318#45706318), so something else is making trouble. You should give more context and your complete code in your question… – LoicTheAztec Feb 19 '19 at 15:16