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

 require_once('/wp-content/plugins/woocommerce/includes/class-wc-order.php');

?

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);
$writer->writeToStdOut();

exit(0);

Thank you.

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


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: