S
S
Stanislav2020-12-28 10:39:34
WordPress
Stanislav, 2020-12-28 10:39:34

Send order details to telegram WP Woocommerce (woocommerce_new_order hook)?

Hello.
Implemented sending notifications in the functions.php file of the theme, all order data is received, except for product details...

Here is the code itself:

add_action( 'woocommerce_new_order', 'telegram_notification',  1, 1  );
function telegram_notification( $order_id ) {

            $order = wc_get_order( $order_id );
    $order_data = $order->get_data(); // The Order data	


$items = $order->get_items();

        foreach ($items as $item) {
            $product = $item->get_product();
            $qty     = $item->get_quantity() ? $item->get_quantity() : 1;
            $price   = wc_format_localized_price($item->get_total() / $qty);
            $text2    .= 'Товар :' . $product->get_name() . ' Кол-во :' . $qty . ' Цена :' . $price;
        }
    
    
$order_id = $order_data['id'];
$order_payment_method_title = $order_data['payment_method_title'];

## Creation and modified WC_DateTime Object date string ##

// Using a formated date ( with php date() function as method)
$order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
$order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');

$order_currency = $order_data['currency'];
$order_shipping_method = $order_data['shipping_method'];

$order_total = $order_data['total'];

## BILLING INFORMATION:

$order_billing_first_name = $order_data['billing']['first_name'];
$order_billing_last_name = $order_data['billing']['last_name'];
$order_billing_address_1 = $order_data['billing']['address_1'];
$order_billing_address_2 = $order_data['billing']['address_2'];
$order_billing_city = $order_data['billing']['city'];
$order_billing_state = $order_data['billing']['state'];
$order_billing_postcode = $order_data['billing']['postcode'];
$order_billing_country = $order_data['billing']['country'];
$order_billing_email = $order_data['billing']['email'];
$order_billing_phone = $order_data['billing']['phone'];

## SHIPPING INFORMATION:

      


//Далее создаем переменную, в которую помещаем PHP массив
$arr = array(
  'Номер заказа: ' => $order_id,
  'Дата: ' => $order_date_modified,
  'Название товара: ' => $text2,
  'Артикул: ' => $sku,
  'Мета продукта:' => $somemeta,
  'Цена товара: ' => $order_total,
  'Валюта: ' => $order_currency,
  'Имя: ' => $order_billing_first_name,
  'Фамилия: ' => $order_billing_last_name,
  'Телефон: ' => $order_billing_phone,
  'Email: ' => $order_billing_email,
  'Страна: ' => $order_billing_country,
  'Область: ' => $order_billing_state,
  'Город: ' => $order_billing_city,
  'Адрес1: ' => $order_billing_address_1,
  'Адрес2: ' => $order_billing_address_2,
  'Индекс: ' => $order_billing_postcode,
  'Метод доставки: ' => $shipping_data_method_title,
  'Метод оплаты: ' => $order_payment_method_title
);

//При помощи цикла перебираем массив и помещаем переменную $txt текст из массива $arr
foreach($arr as $key => $value) {
  $txt .= "<b>".$key."</b> ".$value."%0A";
};


            $xsl = file_get_contents("https://api.telegram.org/botxxxxxxxxxxxxxxxxx/sendMessage?parse_mode=html&chat_id=-xxxxx&text=" . $txt);
}


Who in the subject, please tell me how to solve this problem, thanks!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2020-12-29
@W1SE2018

Explain the question here you have the receipt of these products

foreach ($items as $item) {
            $product = $item->get_product();
            $qty     = $item->get_quantity() ? $item->get_quantity() : 1;
            $price   = wc_format_localized_price($item->get_total() / $qty);
            $text2    .= 'Товар :' . $product->get_name() . ' Кол-во :' . $qty . ' Цена :' . $price;
        }

This is how you can get anything
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product();
   $name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $custom_meta = $item->get_meta( 'custom_meta_key', true );
   $type = $item->get_type();
}

M
murrr, 2021-12-06
@murrr

Have you solved the problem? Something with an array. Fields are not transmitted
Product name:
SKU: Product
meta:
Here is the notification that comes in telegram:
===
Order number: 272
Date: 2021-12-06 22:52:55 Product
name:
SKU: Product
meta: Product
price: 8040
Currency: RUB
Name: Tatiana
Surname: Testova
Phone: 7999884455
Email: [email protected]
Country: RU
Region: Republic of Tatarstan
City: Moscow
Address1: Akademika Gubkina str., 99
Address2:
Zip code: 111111
Delivery
method: Payment method: Payment on delivery
===

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question