1
votes

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

1
Maybe it is just not set? Try $product->get_regular_price() or $product->get_sale_price()Andrii Filenko

1 Answers

0
votes

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