简体   繁体   中英

Woocommerce Live PHP Order Feed in XML

Okay so I've been trying to get my head around the API for the past couple of hours and don't seem to be able too, it seems the only way to output the order details in the specific order, or at least similar... is JSON but I need it in XML.

This is the format I need:

<xml>
  <order>
    <billingInfo>
      <method>Paypal</method>
      <firstName>Paul</firstName>
      <lastName>Smith</lastName>
      <company/>
      <address>Address</address>
      <address2/>
      <city>City</city>
      <state>State</state>
      <postcode>A12 3BC</postcode>
      <country>GB</country>
      <email>JohnSmith@email.com</email>
      <phone>07123456789</phone>
    </billingInfo>
    <shippingInfo>
      <firstName>Paul</firstName>
      <lastName>Smith</lastName>
      <company/>
      <address>Address</address>
      <address2/>
      <city>City</city>
      <state>State</state>
      <postcode>A12 3BC</postcode>
      <country>GB</country>
      <shippingTotal>123</shippingTotal>
      <shippingTax>12</shippingTax>
      <shippingMethod>Shipping Method</shippingMethod>
    </shippingInfo>
    <customerInfo>
      <email/>
      <firstName/>
      <lastName/>
      <username/>
      <taxExempt>true/false</taxExempt>
    </customerInfo>
    <orderInfo>
      <orderId>1234</orderId>
      <transactionId/>
      <customerIp>1.234.56.789</customerIp>
      <datePaid/>
      <taxTotal>100</taxTotal>
      <grandTotal>500.00</grandTotal>
      <customerNote/>
      <taxMethod>VAT 20%</taxMethod>
      <lineItems>
        <item>
          <productId>1234</productId>
          <quantity>1</quantity>
          <name>
            Product Name
          </name>
          <subtotal>500</subtotal>
          <subtotalTax>100</subtotalTax>
        </item>
      </lineItems>
    </orderInfo>
  </order>

The end goal is to have this script run on a CRON job hourly to pull all orders in.

Thank you for any help/advice.

So I figured it out, here's my solution to anybody else in the future who may need help getting Woocommerce order data through XML.

Here's a basic example of what sort of data you can get:

<?php


header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

    include(__DIR__ . "/../wp-load.php");
    global $woocommerce;

    $filters = array(
        'post_status' => 'any',
        'post_type' => 'shop_order',
        'posts_per_page' => -1,
        'paged' => 1,
        'orderby' => 'modified',
        'order' => 'ASC'
    );

    $loop = new WP_Query($filters);

    while ($loop->have_posts()) {

        $loop->the_post();

        $order = new WC_Order(get_the_ID());
        $customer = new WC_Customer($order->get_order_number());        

        foreach ($order->get_items() as $key => $lineItem) {

            $output .= '<xml>';
            $output .= '<paymentInfo>';
            $output .= '<method>' . $order->payment_method . '</method>';
            $output .= '</paymentInfo>';
            $output .= '<personalDetails>';
            $output .= '<firstName>' . $order->billing_first_name . '</firstName>';
            $output .= '<lastName>' . $order->billing_last_name . '</lastName>';
            $output .= '</personalDetails>';

            $output .= '<item>';
            $output .= '<productId>' . $lineItem['product_id'] . '</productId>';
            $output .= '<quantity>' . $lineItem['quantity'] . '</quantity>';
            $output .= '<name>' . $lineItem['name'] . '</name>';
            $output .= '<subtotal>' . $order->cart_tax . '</subtotal>';
            $output .= '<subtotalTax>' . $order->total_tax . '</subtotalTax>';
            $output .= '</item>';

           $output .= '</xml>';

           echo $output;

        }
    }
?>

And the output:

 <xml> <paymentInfo> <method>other</method> </paymentInfo> <personalDetails> <firstName>first K</firstName> <lastName>second K</lastName> </personalDetails> <item> <productId>561</productId> <quantity>1</quantity> <name> Woodlets Premium Wood Pellets - Full Pallet - 980kg </name> <subtotal>0</subtotal> <subtotalTax>0</subtotalTax> </item> </xml> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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