Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created February 20, 2023 12:45
Show Gist options
  • Save pavlo-bondarchuk/0d5a99ed6245b028de0a7713ca451e1f to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/0d5a99ed6245b028de0a7713ca451e1f to your computer and use it in GitHub Desktop.
send notifications to telegram about wc orders
function usual_tg( $message ) {
$token = '';
$chatids = [ ];
if ( stristr( $message, 'http' ) === false ) {
foreach ( $chatids as $chatid ) {
wp_remote_get( "https://api.telegram.org/bot$token/sendMessage?chat_id=$chatid&text=$message&parse_mode=HTML" );
}
}
}
function send_msg_to_tg_new_order( $order_id, $order = false ) {
if ( ! $order ) {
$order = wc_get_order( $order_id );
}
$message = 'Нове замовлення на сайті ' . $_SERVER['HTTP_HOST'];
$message .= "\nId замовлення: " . $order->get_order_number();
$message .= "\nІм'я замовника: " . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$message .= "\nСума: " . $order->get_total() . ' грн';
if ( $order->get_total_discount() ) {
$message .= "\nЗнижка: -" . $order->get_total_discount() . ' грн';
}
if ( $order->get_billing_country() !== 'UA' ) {
$message .= "\nКраїна: " . WC()->countries->countries[ $order->get_billing_country() ];
$message .= "\nІндекс: " . $order->get_billing_postcode();
}
$message .= "\nТелефон: " . $order->get_billing_phone();
$message .= "\nПошта: " . $order->get_billing_email();
$message .= "\nМісто: " . $order->get_billing_city();
$message .= "\nАдреса: " . $order->get_billing_address_1();
if ( $order->get_meta( 'social_to_answer' ) ) {
$message .= "\nКуди надсилати номер накладної: " . ucfirst( $order->get_meta( 'social_to_answer' ) );
}
if ( $order->get_meta( 'nickname' ) ) {
$insta = str_starts_with( $order->get_meta( 'nickname' ), '@' ) ? str_replace( '@', '', $order->get_meta( 'nickname' ) ) : $order->get_meta( 'nickname' );
$message .= "\nНікнейм: " . '@' . $insta;
}
if ( $order->get_meta( 'birth_date' ) ) {
$date = DateTime::createFromFormat( 'Y-d-m', $order->get_meta( 'birth_date' ) )->format( 'd.m.Y' );
$message .= "\nДата народження: " . $date;
}
$message .= "\nСпосіб оплати: " . $order->get_payment_method_title();
if ( $order->get_customer_note() ) {
$message .= "\nДодаткова інформація: " . $order->get_customer_note();
}
$message .= "\nЩо замовили: ";
foreach ( $order->get_items() as $item ) {
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$message .= "\n " . $product_name . ' x' . $quantity;
}
usual_tg( $message );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment