Search code examples
phpwordpressweb-servicesreceiver

Receiving data from word press


I created custom PHP file in WordPress to send order data to an accounting server via its web service.

Now I need to take amount of goods from that accounting server and update the stock of each product.

I do not access that server, and they don't have any document for their web-service.

I tried to find some info about using web service to do this, but the only thing that I got was to send (not receiving) data of a WordPress site via web service.

This is my code that send order details to accounting server:

<?php
require_once('../wp-config.php');
require_once('../wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php');

$today =  new DateTime();
$today->setTime( 0, 0, 0 );

$args = array(
    'limit' => 9999,
    'return' => 'ids',
    'date_completed' => $today,
    'status' => 'completed'
);

$query = new WC_Order_Query( $args );
$orders = $query->get_orders();

$url = "http://###.###.###.###:8081/Service1.svc/InsertInvoice";

foreach( $orders as $order_id ) {
    $order = wc_get_order( $order_id );
    $order_date_completed = $order->get_date_completed();
    if (false === strtotime($order_date_completed)) {
        echo 'Invalid date for order number '.$order_id.'<br>';
    }else {
        $order_date_completed->setTime( 0, 0, 0 );
        $diff = $today->diff( $order_date_completed );
        $diffDays = (integer)$diff->format( "%R%a" );

        if($diffDays == 0){

            $items = $order->get_items();
            $InvoiceInfo = array();

            foreach( $items as $item ){

                $product = $item->get_product();

                $item_sku  = $product->get_sku();
                $saleType  = substr($item_sku, 1, 2);
                $item_code = substr($item_sku, 3, 8);
                $user_id   = substr($item_sku, 11, 6);

                $data2 = array(
                    'CustomerCode' => $user_id,
                    'SaleTypeNumber' => $saleType,
                    'ItemCode' => $item_code,
                    'Quantity' => $item->get_quantity(),
                    'Fee' => $product->get_price(),
                    'Discount' => 0,
                    'Addition' => 0,
                    'Tax' => 0,
                    'Duty' => 0,
                    'Rate' => 1,
                    'StockCode' => 5,
                );
                array_push($InvoiceInfo,$data2);
            }

            $postdata = json_encode($InvoiceInfo);
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $result = curl_exec($ch);
            curl_close($ch);
        }
    }
}
?>

Solution

  • In order to send data to WooCommerce, you can use WordPress REST API custom endpoint that can receive data from anywhere.

    But, as you didn't provide the name of the accounting server or even any information about the accounting app, you have to check if it is possible to send data from your accounting app to a webhook (with a service like Zapier).