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

时间:2019-03-18 08:31:45

标签: php wordpress post woocommerce hook-woocommerce

我想通过HTTP POST请求发送订单的详细信息(下订单时)(详细信息包括名字,姓氏,订单名称等)。

数据应发送到我的Node / Express后端,并且应为JSON。

这是我的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后端:

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;

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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(),
                    )
                )
            );
    }
});

有关更多详细信息-选中Get order details