结帐后,woocommerce获取订单详情
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结帐后,woocommerce获取订单详情相关的知识,希望对你有一定的参考价值。
结账后我需要获取以下信息:
- 订单ID
- 订购日期
- 客户名称(F / L)
- 客户电话
- 客户电邮
将其放入会话数组中,然后存储在单独的数据库中。目前在我的主题的functions.php文件中我有以下内容:
//get completed order data:
add_action( 'woocommerce_order_status_completed', 'my_function' );
function my_function($order_id) {
$order = new WC_Order( $order_id );
$_SESSION['kiosk_data']['order_number'] = $order->[what would I use here?];
$_SESSION['kiosk_data']['order_date'] = $order->[what would I use here?];
$_SESSION['kiosk_data']['f_name'] = $order->[what would I use here?];
$_SESSION['kiosk_data']['l_name'] = $order->[what would I use here?];
$_SESSION['kiosk_data']['phone'] = $order->[what would I use here?];
$_SESSION['kiosk_data']['email'] = $order->[what would I use here?];
//for testing:
$kioskDataFile = './testFile.txt';
$kioskData = "kioskID: ".$_SESSION['kiosk_data']['kioskID']."
";
$kioskData.= "order date: ";
$kioskData.= $order->order_date;
$kioskData.= "
";
$kioskData.= "order ID: ";
$kioskData.= $order->id;
$kioskData.= "
";
$kioskData.= "order key: ";
$kioskData.= $order->order_key;
$kioskData.= "
";
$kioskData.= "first name: ";
$kioskData.= $order->billing_first_name;
$kioskData.= "
";
$kioskData.= "last name: ";
$kioskData.= $order->billing_last_name;
$kioskData.= "
";
$kioskData.= "phone: ";
$kioskData.= $order->billing_phone;
$kioskData.= "
";
$kioskData.= "email: ";
$kioskData.= $order->billing_email;
$kioskData.= "
";
file_put_contents($kioskDataFile, $kioskData, FILE_APPEND);
}
作为WP / woocommerce的新手,我真的不确定如何捕获这些数据(尝试了几次谷歌搜索无济于事)。
答案
通过更改代码如下并删除'add_action'组件和功能,并将其移动到子主题/ woocommerce / checkout下的“thankyou.php”页面。
$kioskDataFile = './logs/kiosk_log';
$kioskData['description'] = "kiosk site - thankyou.php";
$kioskData['kiosk_id'] = $_SESSION['kiosk_data']['kioskID'];
$kioskData['order_date'] = $order->order_date;
$kioskData['month'] = date("m");
$kioskData['year'] = date("Y");
$kioskData['order_number'] = $order->get_order_number();
$kioskData['order_id'] = $order->id;
$kioskData['order_key'] = $order->order_key;
$kioskData['first_name'] = $order->billing_first_name;
$kioskData['last_name'] = $order->billing_last_name;
$kioskData['phone'] = $order->billing_phone;
$kioskData['email'] = $order->billing_email;
$s_kioskData = serialize($kioskData);
$s_kioskData.= "
";
file_put_contents($kioskDataFile, $s_kioskData, FILE_APPEND);
我已经序列化了数组数据,因此我可以通过日志文件中的file()将其加载到数组中并对其进行排序/解析。
另一答案
您可以var_dump订单并检查您按顺序获得的字段。
add_action( 'woocommerce_order_status_completed', 'my_function' ,10,1);
function my_function($order_id) {
$order = new WC_Order( $order_id );
$_SESSION['kiosk_data']['order_number'] = $order->id;
$_SESSION['kiosk_data']['order_date'] = $order->order_date;
$_SESSION['kiosk_data']['f_name'] = $order->billing_first_name;
$_SESSION['kiosk_data']['l_name'] = $order->billing_last_name;
$_SESSION['kiosk_data']['phone'] = $order->billing_email;
$_SESSION['kiosk_data']['email'] = $order->billing_phone;
}
您可以从here获取属性列表,您可以从WC_Order对象获取这些属性。
以上是关于结帐后,woocommerce获取订单详情的主要内容,如果未能解决你的问题,请参考以下文章
在电子邮件中显示自定义数据,订单详情 woocommerce
Woocommerce在下订单后更新order_comments
Woocommerce 在结帐页面下订单后替换购物车中的产品