结帐后,woocommerce获取订单详情

问题描述 投票:1回答:2

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

  • 订单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的新手,我真的不确定如何捕获这些数据(尝试了几次谷歌搜索无济于事)。

php wordpress woocommerce get orders
2个回答
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()将其加载到数组中并对其进行排序/解析。


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对象获取这些属性。

© www.soinside.com 2019 - 2024. All rights reserved.