Created
January 22, 2025 16:51
-
-
Save RustemAqtau/ecae59a910e6a91b59bd7d09eb213f34 to your computer and use it in GitHub Desktop.
woo send message to telegram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('woocommerce_new_order', 'new_order_send_tg', 1, 1); | |
function new_order_send_tg($order_id) { | |
$order = wc_get_order($order_id); // Get the order object | |
// Customer information | |
$customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); | |
$customer_phone = $order->get_billing_phone(); | |
$customer_email = $order->get_billing_email(); | |
$customer_address = $order->get_billing_address_1() . ', ' . $order->get_billing_city() . ', ' . $order->get_billing_country(); | |
// Order items | |
$items_info = ''; | |
foreach ($order->get_items() as $item_id => $item) { | |
$product_name = $item->get_name(); // Product name | |
$quantity = $item->get_quantity(); // Quantity | |
$item_total = html_entity_decode(strip_tags(wc_price($item->get_total()))); // Item total (strip HTML tags and decode entities) | |
$items_info .= "\n- " . $product_name . " x " . $quantity . " - " . $item_total; | |
} | |
// Order total | |
$order_total = html_entity_decode(strip_tags(wc_price($order->get_total()))); // Strip HTML tags and decode entities | |
// Prepare the message | |
$msg = "*Магазин ismart.kz*\n\n"; | |
$msg .= "🛍 *Новый заказ:* #" . $order_id . "\n\n"; | |
$msg .= "👤 *Информация о клиенте:*\n"; | |
$msg .= "Имя: " . $customer_name . "\n"; | |
$msg .= "Телефон: " . $customer_phone . "\n"; | |
$msg .= "Email: " . $customer_email . "\n"; | |
$msg .= "Адрес: " . $customer_address . "\n\n"; | |
$msg .= "📦 *Состав заказа:*" . $items_info . "\n\n"; | |
$msg .= "💰 *Итого:* " . $order_total; | |
// Telegram API credentials | |
$userId = 'userid'; // Ваш id в телеграм | |
$token = 'tokenname'; // Token бота | |
// Send the message | |
$url = 'https://api.telegram.org/bot' . $token . '/sendMessage?chat_id=' . $userId . '&text=' . urlencode($msg) . '&parse_mode=markdown'; | |
file_get_contents($url); // Отправляем сообщение | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment