I created a code to add text in an email for a specific product.
I want to retrieve the price of the product but it does not work.
It gives me an error when ordering "There was an error processing the order. Please try again."
Here is the code :
add_action( 'woocommerce_email_before_order_table', 'esprit_add_content_specific_email', 20, 4 );
function esprit_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
global $woocommerce;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
$price = $product_id->get_price();
if ( $product_id == 3550 ) {
echo 'Message uniquement sur la carte cadeaux libre.';
echo "id";
echo $product_id;
echo "prix";
echo $price;
}
}
}
I manage to retrieve the product id but not the "get_price()".
Please, any help for me. Sorry for my english 🙂
2
Answers
The
$product_id
variable looks like it’s an integer, which doesn’t have any methods on it. Rather, try usingwc_get_product( $product_id )->get_price()
.Untested:
There are some mistakes in your code and unnecessary things.
global $woocommerce;
is not neededget_price()
using the product ID variable, as it’s not the WC_Product object.So the correct revisited code is:
Tested and works.
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3