File: /var/www/shoetique/wp-content/plugins/Shoetique-Borealis/ajax.php
<?php
add_action('wp_ajax_pull_wc_orders', 'pull_wc_orders');
add_action('wp_ajax_nopriv_pull_wc_orders', 'pull_wc_orders');
function pull_wc_orders()
{
header('Content-Type: application/json');
$auth_key = $_GET['auth'];
if ($auth_key != AJAX_AUTH) {
response(null, "Auth key requreid!");
}
$from = date('Y-m-d', strtotime("-2 days"));
$to = date('Y-m-d');
$from_data = get_date_parameters('from');
if ($from_data) {
$from = $from_data;
}
$to_data = get_date_parameters('to');
if ($to_data) {
$to = $to_data;
}
$query = new WC_Order_Query(array(
'limit' => -1,
'date_created' => $from . '...' . $to,
'status' => array('wc-processing', 'wc-completed'),
'return' => 'ids',
));
$orders = $query->get_orders();
$orders_array = array();
$order = new \stdClass;
foreach ($orders as $order_id) {
$order = new WC_Order($order_id);
$order->OrderTime = date_format($order->date_created, "c");
$order->LocationCode = 123; //TODO
$order->DeliveryAddress = get_address_object($order);
$order->OrderNumber = $order_id;
$order->Sum = $order->get_total();
$order->Status = "New";
$order->PaymentTime = date_format($order->date_paid, "c");
$order->TransactionId = $order->get_transaction_id();
$order->CompanyInformation = $order->get_billing_company() == "" ? null : $order->get_billing_company();
$order->Notice = $order->get_customer_note();
$order->CardType = null;
$order->PaymentType = $order->get_payment_method();
$order->Buyer = get_address_object($order, true);
$order->Items = get_order_items($order);
$order->Delivery = null;
array_push($orders_array, $order);
}
response($orders_array, null);
}
function response($orders, $error){
$response = array();
$response["Orders"] = $orders;
$response["Error"] = $error;
echo json_encode($response);
wp_die();
}
function get_date_parameters($key)
{
if (!empty($_GET[$key])) {
if (!preg_match("/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/", $_GET[$key])) {
response(null, $key . " parameter with wrong format! use ex. 2022-02-28");
}
return $_GET[$key];
}
return false;
}
function get_order_items($order)
{
$items_array = array();
$item = new \stdClass;
foreach ($order->get_items() as $item_id => $product) {
$item->ActionId = "0";
$item->Price = $product->get_subtotal();
$item->PriceListId = "0";
$item->LocationCode = 123; //TODO
$item->ActionPrice = "";
$item->Quantity = $product->get_quantity();
$item->Sum = $product->get_total();
$item->Code = $product->get_product_id();
$item->WarehouseCode = "";
array_push($items_array, $item);
}
return $items_array;
}
function get_address_object($order, $is_billing = false)
{
$address = new \stdClass;
$address->Email = $order->get_billing_email();
if ($is_billing) {
$address->FirstName = $order->get_billing_first_name();
$address->LastName = $order->get_billing_last_name();
$address->Address = $order->get_billing_address_1();
$address->PostCode = $order->get_billing_postcode();
$address->Country = $order->get_billing_state();
$address->City = $order->get_billing_city();
$address->Phone = $order->get_billing_phone();
} else {
$address->FirstName = $order->get_shipping_first_name();
$address->LastName = $order->get_shipping_last_name();
$address->Address = $order->get_shipping_address_1();
$address->PostCode = $order->get_shipping_postcode();
$address->Country = $order->get_shipping_state();
$address->City = $order->get_shipping_city();
$address->Phone = $order->get_shipping_phone();
}
return $address;
}