I'm trying to send notification on WooCommerce new order with Telegram. I get almost all the details I want except the products list from order. If I use a shortcode to expose the order details in WordPress, my code is working. When I`m trying to do this in Telegram, product details are not shown anymore.
Here is my code:
add_action( 'woocommerce_new_order', 'telegram_notification', 1, 1 );
function telegram_notification( $order_id ) {
$order = wc_get_order( $order_id );
$text = "*" . __( 'New Order') . "*\n\n";
$text .= __( 'Order number') . ': ' . $order_id . "\n";
$text .= __( 'Status') . ': ' . wc_get_order_status_name( $order->get_status() ) . "\n";
$text .= __( 'Date') . ': ' . $order->get_date_modified() . "\n";
$text .= __( 'Email') . ': ' . $order->get_billing_email() . "\n";
$text .= __( 'Total price') . ': ' . $order->get_total() . "\n";
$text .= __( 'Payment method') . ': ' . $order->get_payment_method_title() . "\n";
$text .= "\n" . __( 'Items') . ':' . "\n";
foreach ( $order->get_items() as $item_id => $item_data ) {
$product = $item_data->get_product();
$product_name = $product->get_name();
$item_quantity = $item_data->get_quantity();
$item_total = $this->wc_price( $item_data->get_total() );
$text .= $product_name . ' × ' . $item_quantity . ' = ' . $item_total . "\n";
}
$xsl = file_get_contents("https://api.telegram.org/bot12345:xxxxx-zzzzz/sendMessage?parse_mode=html&chat_id=-1234567890&text=" . $text);
}
And here is the outcome in Telegram :
New Order__Order number: 981_Stare: Plată în așteptare_Dată: 2020-12-12T12:12:12 00:00_Email: email@yahoo.com_Total price: 10.00_Payment method: Numerar la livrare__Items:_
And nothing is shown on items...
My WooCommerce version is 4.1.0 and WordPress version 5.4.1
Thank you in advance for your time