结帐后,woocommerce获取订单详情

时间:2015-10-23 23:16:19

标签: php wordpress woocommerce get orders

我需要在结帐后获取以下信息:

  • 订单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']."\n";
    $kioskData.= "order date: ";
    $kioskData.= $order->order_date;
    $kioskData.= "\n";
    $kioskData.= "order ID: ";
    $kioskData.= $order->id;
    $kioskData.= "\n";
    $kioskData.= "order key: ";
    $kioskData.= $order->order_key;
    $kioskData.= "\n";
    $kioskData.= "first name: ";
    $kioskData.= $order->billing_first_name;
    $kioskData.= "\n";
    $kioskData.= "last name: ";
    $kioskData.= $order->billing_last_name;
    $kioskData.= "\n";
    $kioskData.= "phone: ";
    $kioskData.= $order->billing_phone;
    $kioskData.= "\n";
    $kioskData.= "email: ";
    $kioskData.= $order->billing_email;
    $kioskData.= "\n\n\n";

    file_put_contents($kioskDataFile, $kioskData, FILE_APPEND);
    }

作为WP / woocommerce的新手,我真的不确定如何捕获这些数据(尝试了几次谷歌搜索无济于事)。

2 个答案:

答案 0 :(得分:1)

通过更改以下代码并删除'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.= "\n";
    file_put_contents($kioskDataFile, $s_kioskData, FILE_APPEND);

我已经序列化了数组数据,因此我可以通过日志文件中的file()将其加载到数组中并对其进行排序/解析。

答案 1 :(得分:0)

您可以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对象获取的属性列表。