2
votes

In Woocommerce I am trying to find out how to get the product id for a completed order inside the Customer completed order email template to store it as a PHP variable. This way I will be able to insert it into an external database.

I already tried $product->get_id(); but it does not work.

How can get the Product ID in WooCommerce 3+ from email templates?

1

1 Answers

2
votes

You need to loop through Order items… Normally the WC_Order object is defined through the variable $order mostly defined everywhere in email templates.

So the code to get the product ID will be:

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

    // Get the product ID
    $product_id = $item->get_product_id(); 

    // Get an instance of the WC_Product object
    $product = $item->get_product(); 

    // Get product title (name)
    $product_name = $product->get_title(); // or $product->get_name();

    // Get product price
    $product_price = $product->get_price(); 
}

See this related thread: Get Order items and WC_Order_Item_Product in Woocommerce 3