WooCommerce – Access ‘WC_Order’ from a separate PHP File

I’m using Javascript to call a PHP file directly from my functions.php file in WooCommerce (via WordPress) which is using the ‘PHP XLSXWriter’ code available on GitHub. However, I’m having trouble accessing

$order = new WC_Order($order_id);

Can I access this function directly behind WooCommerce by perhaps using



Here is my entire PHP code which I’m calling:

$order_id = $_GET['order']; // pull the order info from the URL 
$order = new WC_Order($order_id); // this crashes this entire function!
echo $order->shipping_city; // this then fails
echo $order->shipping_country; // and this fails too

// this code doesn't then execute...
$filename = "test.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$rows = array(
    array('Shipping Service Code',  'Company',  'Consignee Name',   'Address Line 1',   'Address Line 2',   'Address Line 3'),
    array('PPS',                    '-',        '-',                '-',                '-',                '-'),

$writer = new XLSXWriter();
$writer->setAuthor('EXAMPLE AUTHOR');

foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);


Thank you.

Read more here: WooCommerce – Access ‘WC_Order’ from a separate PHP File

Leave a Reply

Your email address will not be published. Required fields are marked *