在WooCommerce订购之前获取客户数据
问题描述:
在WooCommerce中,当我的网上商店获得新订单时,我有一个脚本正在运行。该脚本向我发送短信,但我想将其发送给客户。在WooCommerce订购之前获取客户数据
该脚本正在使用自定义函数脚本运行,就在Order-received页面之前,包含有关订单的信息。
如何从关于用户使用的姓名和电话号码的订单中获取自动信息?
阅读本:How to get WooCommerce order details后,并不能帮助我,我能得到我所需要的信息和订单页面失败时,我尝试......今天
我的代码是这样的:
add_action('template_redirect', 'wc_custom_redirect_after_purchase');
function wc_custom_redirect_after_purchase() {
global $wp;
if (is_checkout() && ! empty($wp->query_vars['order-received'])) {
// Query args
$query21 = http_build_query(array(
'token' => 'My-Token',
'sender' => 'medexit',
'message' => 'NEW ORDER',
'recipients.0.msisdn' => 4511111111,
));
// Send it
$result21 = file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query21);
// exit;
}
}
我需要的是获取消息中包含的名字。
我想是这样的:
$firstname = $order_billing_first_name = $order_data['billing']['first_name'];
$phone = $order_billing_phone = $order_data['billing']['phone'];
但似乎没有对我的作品。
答
相反,你可以尝试使用woocommerce_thankyou
行动钩勾住了自定义函数:
add_action('woocommerce_thankyou', 'wc_custom_sending_sms_after_purchase', 20, 1);
function wc_custom_sending_sms_after_purchase($order_id) {
if (! $order_id) return;
// Avoid SMS to be sent twice
$sms_new_order_sent = get_post_meta($order_id, '_sms_new_order_sent', true);
if('yes' == $sms_new_order_sent) return;
// Get the user complete name and billing phone
$user_complete_name = get_post_meta($order_id, '_billing_first_name', true) . ' ';
$user_complete_name .= get_post_meta($order_id, '_billing_last_name', true);
$user_phone = get_post_meta($order_id, '_billing_phone', true);
// 1st Query args (to the admin)
$query1 = http_build_query(array(
'token' => 'My-Token',
'sender' => 'medexit',
'message' => 'NEW ORDER',
'recipients.0.msisdn' => 4511111111
));
// 2nd Query args (to the customer)
$query2 = http_build_query(array(
'token' => 'My-Token',
'sender' => 'medexit',
'message' => "Hello $user_complete_name. This is your new order confirmation",
'recipients.0.msisdn' => intval($user_phone)
));
// Send both SMS
file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query1);
file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query2);
// Update (avoiding SMS to be sent twice)
update_post_meta($order_id, '_sms_new_order_sent', 'yes');
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
测试在WooCommerce 3 + ...
相关短信答案:
Sending an SMS for specific email notifications and order statuses
完美,第一部分没有工作!创建自定义功能是关键,谢谢.... – user2975926