Skip to content

Instantly share code, notes, and snippets.

@Ibochkarev
Created July 21, 2025 08:21
Show Gist options
  • Save Ibochkarev/cd5a93cc2598d44b114f8e8ae63c40b8 to your computer and use it in GitHub Desktop.
Save Ibochkarev/cd5a93cc2598d44b114f8e8ae63c40b8 to your computer and use it in GitHub Desktop.
<?php
if($modx->event->name != 'msOnChangeOrderStatus') return; // Измененное событие
$status = $scriptProperties['status'];
if ($status != 2) return;
$msOrder = $scriptProperties['order'];
$token = $modx->getOption('mstelegram_token', null, false);
$recipients = explode(',', $modx->getOption('mstelegram_recipients', null, ''));
$contacts = $msOrder->getOne('Address');
$delivery = $msOrder->getOne('Delivery');
$payment = $msOrder->getOne('Payment');
$_products = $msOrder->getMany('Products');
$paymentData = $modx->runSnippet('getPaymentDataByOrderId', ['orderId' => $msOrder->id]);
$coupon = $modx->runSnippet('!pdoResources', [
'class' => 'mspc2CouponOrder',
'loadModels' => 'msPromoCode2',
'innerJoin' => [
[
'class' => 'mspc2Coupon',
'alias' => 'mspc2Coupon',
'on' => 'mspc2Coupon.id = mspc2CouponOrder.coupon',
]
],
'select' => [
'mspc2CouponOrder' => 'code, discount, discount_amount',
],
'where' => [
'mspc2CouponOrder.order' => $msOrder->get('id'),
],
'sortby' => '{"id":"ASC"}',
'return' => 'json',
]);
if($coupon){
$coupon = json_decode($coupon, true);
$coupon = $coupon[0];
}
// Список товаров в заказе
$i = 0;
$products = '';
foreach ($_products as $product) {
$i++;
$products .= "{$i}. {$product->name}: {$product->price}₽ х {$product->count} шт. = " . ($product->price * $product->count) . "₽ \n";
}
// Получаем массив данных заказа
$orderData = $msOrder->toArray();
// Выводим в лог
$modx->log(1, print_r($orderData, true));
// $modx->log(1, 'Contacts: ' . print_r($msOrder->toArray(), true));
// Преобразование суммы с разделением тысяч
$formattedCost = number_format($msOrder->get('cost'), 0, '.', ' ');
$formattedCartCost = number_format($msOrder->get('cart_cost'), 0, '.', ' ');
$formattedDeliveryCost = number_format($msOrder->get('delivery_cost'), 0, '.', ' ');
$formattedDeliveryPrice = number_format($delivery->get('price'), 0, '.', ' ');
$editUrlOrder = $modx->getOption('site_url') . 'manager/?a=mgr/orders&namespace=minishop2&order=' . $msOrder->get('id');
$message = "<b>Заказ #{$msOrder->num}</b>\n\n"
. "<b>Товары:</b>\n{$products}\n"
. "<b>Итого:</b>\n"
. "Стоимость товаров: {$formattedCost}₽\n"
. "Доставка: {$formattedDeliveryCost}₽\n"
. "Скидка по промокоду:" . (!empty($coupon['discount_amount']) ? "-{$coupon['discount_amount']}₽" : "0₽") . "\n"
. "<b>Итого к оплате: {$msOrder->get('cost')}₽</b>\n\n"
. "<b>Payment system: {$payment->name}</b>\n"
. (isset($paymentData['object']['id']) && $paymentData['object']['id']
? "<b>Payment ID: {$paymentData['object']['id']}</b>\n"
: "")
. (isset($paymentData['object']['captured_at']) && $paymentData['object']['captured_at']
? "<b>Payment time: {$paymentData['object']['captured_at']}</b>\n\n"
: "\n")
. "<b>Доставка:</b>\n"
. "Способ: {$delivery->name} ({$formattedDeliveryPrice}₽)\n"
. "Адрес: {$contacts->text_address}\n"
. "Телефон: {$contacts->phone}\n"
. "Email: {$contacts->email}\n"
. "Получатель: {$contacts->receiver}\n"
. "Комментарий: {$contacts->comment}\n\n"
. "<b>Платеж:</b>\n"
. "Способ: {$payment->name}\n";
if($msOrder->mspc2_code){
$message .= "<b>Промокод</b>: {$msOrder->mspc2_code}\n";
}
$message .= "\n<a href=\"{$editUrlOrder}\">Редактировать заказ</a>";
// . "ID платежа: {$payment->$payment_id}\n\n"
$modx->log(1, print_r([$message,$paymentData,$coupon, $total],1));
foreach($recipients as $id){
$id = trim($id);
if(!$id) continue;
$url = "https://api.telegram.org/bot{$token}/sendMessage";
$data = [
'chat_id' => $id,
'text' => $message,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data
]);
$result = curl_exec($ch);
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment