简体   繁体   English

如何在订购后在WooCommerce中获取购买数据并将其作为POST请求发送?

[英]How can I get the purchase data after an order, in WooCommerce and send it as a POST request?

I want to send the details of an order (when the order is placed) via an HTTP POST request (details susch as firstname, lastname, order name etc.). 我想通过HTTP POST请求发送订单的详细信息(下订单时)(将后缀作为名字,姓氏,订单名称等)。

The data should be send to my Node/Express backend, and should be as JSON. 数据应发送到我的Node / Express后端,并且应为JSON。

Here's my functions.php file: 这是我的functions.php文件:

add_action( 'woocommerce_order_status_completed', 'send_order_data' );

function send_order_data( $order_id ) {

    $order = new WC_Order( $order_id );

    $url = 'http://exemple.com/custom-url';

    if ( $order->status != 'failed' ) {

        //Get the order and customer data
        //Send the data as a HTTP POST request && the data should be as JSON

        exit;
    }

Node/Express backend: 节点/ Express后端:

const express = require('express');
const router = express.Router();

// @route   POST '/api'
// @desc    
// @access  Public
router.post('/', (req, res) => {
  // Get data from WC post request
  console.log('new WC request');
  console.log(req.body);
});

module.exports = router;

Any help would be much appreciated. 任何帮助将非常感激。

add_action( 'woocommerce_thankyou', function( $order_id ){
    $order = new WC_Order( $order_id );


    if ( $order->status != 'failed' ) { 

            $url = "https://www.test.com";

            $response = wp_remote_post(
                $url,
                array(
                    'body' => array(
                        'firstname' => $order->get_billing_address_1(),
                        'lastname' =>  $order->get_billing_address_2(),
                    )
                )
            );
    }
});

For more details - check Get order details 有关更多详细信息-选中获取订单详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM