| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/inc/ |
Upload File : |
<?php
/**
* Telegram notifications for newly placed WooCommerce orders.
*/
/**
* ─── Telegram notification on new order ───
* Two hooks for reliability:
* 1) woocommerce_order_status_changed — catches status transitions
* 2) woocommerce_thankyou — fallback when checkout completes
* Uses order meta flag to avoid duplicate sends.
*/
add_action( 'woocommerce_order_status_changed', 'matreshka_telegram_new_order_notify_on_status', 10, 4 );
add_action( 'woocommerce_thankyou', 'matreshka_telegram_new_order_notify_on_thankyou', 5, 1 );
function matreshka_telegram_new_order_notify_on_status( $order_id, $old_status, $new_status, $order ) {
matreshka_send_telegram_new_order( $order_id );
}
function matreshka_telegram_new_order_notify_on_thankyou( $order_id ) {
matreshka_send_telegram_new_order( $order_id );
}
function matreshka_send_telegram_new_order( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Avoid duplicate notifications
if ( $order->get_meta( '_matreshka_tg_notified' ) === 'yes' ) {
return;
}
$token = defined( 'MATRESHKA_TG_BOT_TOKEN' ) ? MATRESHKA_TG_BOT_TOKEN : '';
$chat_id = defined( 'MATRESHKA_TG_CHAT_ID' ) ? MATRESHKA_TG_CHAT_ID : '';
if ( ! $token || ! $chat_id ) {
return;
}
$total_raw = $order->get_formatted_order_total();
$total = html_entity_decode( wp_strip_all_tags( $total_raw ), ENT_QUOTES, 'UTF-8' );
$items = count( $order->get_items() );
$admin = admin_url( 'post.php?post=' . $order_id . '&action=edit' );
$text = "🧾 <b>Новый заказ #" . $order->get_order_number() . "</b>\n";
$text .= "----------------\n";
$text .= "На сайте сделали новый заказ, смотрите все детали в админке.\n";
$text .= "----------------\n";
$text .= "Сумма: " . $total . "\n";
$text .= "Товаров: " . $items . "\n";
$response = wp_remote_post( "https://api.telegram.org/bot{$token}/sendMessage", [
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => wp_json_encode( [
'chat_id' => $chat_id,
'text' => $text,
'parse_mode' => 'HTML',
] ),
'timeout' => 15,
] );
if ( is_wp_error( $response ) ) {
return;
}
$result = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $result['ok'] ) ) {
// Mark as notified to prevent duplicates
$order->update_meta_data( '_matreshka_tg_notified', 'yes' );
$order->save();
}
}