首页 > 解决方案 > 如何在 WooCommerce 中下订单后获取购买数据并将其作为 POST 请求发送?

问题描述

我想通过 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;
    }

节点/快递后端:

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;

任何帮助将非常感激。

标签: phpwordpresspostwoocommercehook-woocommerce

解决方案


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

有关更多详细信息 - 请查看获取订单详细信息


推荐阅读