配置说明:https://developer.work.weixin.qq.com/document/path/91770
//通用版
function wx_robot_post($url,$data = array(),$dataType='') {
$post_data=json_encode($data,'320');
if (empty($url) || empty($post_data)) {
return false;
}
$curlPost = $post_data;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($dataType=='json'){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: ' . strlen($curlPost)
)
);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
return $data;
}
//WP版,直接调用wp_remote_post()
function wx_robot_post($url, $data = array(), $dataType = '', $retry = 3) {
$args = array(
'body' => json_encode($data),
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length' => strlen(json_encode($data))
),
'timeout' => 30,
'sslverify' => false
);
if ($dataType == 'json') {
$args['headers']['Content-Type'] = 'application/json';
}
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
// 处理错误,如记录日志或发送通知
if ($retry > 0) {
// 重新调用函数,减少重试次数
return wx_robot_post($url, $data, $dataType, $retry - 1);
}
// 返回适当的错误响应
}
$data = wp_remote_retrieve_body($response);
return $data;
}
//woocomerce订单提醒
add_action('woocommerce_payment_complete', 'get_customer_details');
function get_customer_details($order_id) {
$order = wc_get_order($order_id);
$order_number = $order->get_order_number();
$order_total = $order->get_total();
$customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$customer_phone = $order->get_billing_phone();
// 获取客户收货地址
$customer_address = array(
'地址1' => $order->get_shipping_address_1(),
'地址2' => $order->get_shipping_address_2(),
'城市' => $order->get_shipping_city(),
);
// 获取订单中的产品名称和数量
$items = $order->get_items();
$purchased_products = '';
foreach ($items as $item) {
$product = $item->get_product();
$product_name = $product->get_name();
$product_qty = $item->get_quantity();
$purchased_products .= $product_name . ' × ' . $product_qty . ', ';
}
// 移除末尾的逗号和空格
$purchased_products = rtrim($purchased_products, ', ');
$url = 'API';
$data = array(
"msgtype" => "markdown",
"markdown" => array(
"content" => "商城订单已付款,需要及时发货。\n
>下单产品:".$purchased_products."
>订单编号:".$order_number."
>订单金额:".$order_total."
>客户姓名:".$customer_name."
>客户地址:".implode(', ', $customer_address) ."
>客户电话:".$customer_phone."
>订单列表:[点击查看](url)"
)
);
wx_robot_post($url,$data,'json');
}
add_action('woocommerce_new_order', 'get_new_order');
function get_new_order(){
$url = 'API';
$data = array(
"msgtype" => "markdown",
"markdown" => array(
"content" => "商城有新的订单,请相关同事注意。\n
>订单列表:[点击查看](url)"
)
);
wx_robot_post($url,$data,'json');
}
function wx_robot_post($url, $data = array(), $dataType = '', $retry = 3) {
$args = array(
'body' => json_encode($data),
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length' => strlen(json_encode($data))
),
'timeout' => 30,
'sslverify' => false
);
if ($dataType == 'json') {
$args['headers']['Content-Type'] = 'application/json';
}
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
// 处理错误,如记录日志或发送通知
if ($retry > 0) {
// 重新调用函数,减少重试次数
return wx_robot_post($url, $data, $dataType, $retry - 1);
}
// 返回适当的错误响应
}
$data = wp_remote_retrieve_body($response);
return $data;
}
function send_order_notification($order, $msg_type) {
$order_number = $order->get_order_number();
$order_total = $order->get_total();
$customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$customer_phone = $order->get_billing_phone();
$customer_address = array(
'地址1' => $order->get_shipping_address_1(),
'地址2' => $order->get_shipping_address_2(),
'城市' => $order->get_shipping_city(),
);
$items = $order->get_items();
$purchased_products = '';
foreach ($items as $item) {
$product = $item->get_product();
$product_name = $product->get_name();
$product_qty = $item->get_quantity();
$purchased_products .= $product_name . ' × ' . $product_qty . ', ';
}
$purchased_products = rtrim($purchased_products, ', ');
$url = '';
$data = array(
"msgtype" => "markdown",
"markdown" => array(
"content" => "<font color=\"info\">" . $msg_type . "</font>\n
>下单产品:<font color=\"comment\">" . $purchased_products . "</font>
>订单编号:<font color=\"comment\">" . $order_number . "</font>
>订单金额:<font color=\"warning\">" . $order_total . "</font>
>客户姓名:<font color=\"comment\">" . $customer_name . "</font>
>客户地址:<font color=\"comment\">" . implode(', ', $customer_address) . "</font>
>客户电话:<font color=\"comment\">" . $customer_phone . "</font>
>订单列表:[点击查看](https://shop.tchdvideo.com/wp-admin/edit.php?post_type=shop_order)"
)
);
return wx_robot_post($url, $data, 'json');
}
add_action('woocommerce_payment_complete', 'get_customer_details');
function get_customer_details($order_id) {
$order = wc_get_order($order_id);
return send_order_notification($order, '商城订单【已付款】,需要及时发货。');
}
add_action('woocommerce_new_order', 'get_new_order');
function get_new_order($order_id) {
$order = wc_get_order($order_id);
return send_order_notification($order, '商城有新的订单【未付款】,请相关同事注意。');
}
本文作者:ZKCOI
文章名称:企业微信群机器人功能
文章链接:https://www.zkcoi.com/365up/program/2723.html
本站资源仅供个人学习和交流,如若转载,请注明出处,详见《免责声明》。
微信扫一扫
支付宝扫一扫
