$email, "phone" => $phone, "order_history" => self::getOrderHistoryObject($email, $phone) ]; wp_send_json( $data ); } public static function getOrderHistoryObject($email, $phone) { $result = []; if (!$email) return $result; $sha256 = hash('sha256', json_encode([$email, $phone])); $tr_name = 'tabby_oh_' . $sha256; if (($order_history = get_transient($tr_name)) !== false) { return $order_history; } $wc2tabby = [ //'pending' => 'processing', //'processing' => 'processing', //'on-hold' => 'processing', 'completed' => 'complete', 'cancelled' => 'canceled', 'refunded' => 'refunded', 'failed' => 'canceled', ]; $ids = wc_get_orders(['return' => 'ids', 'email' => $email, 'status' => array_keys($wc2tabby)]); if ($phone) { $ids = array_merge($ids, wc_get_orders(['return' => 'ids', 'billing_phone' => $phone, 'status' => array_keys($wc2tabby)])); $ids = array_unique($ids); } rsort($ids, SORT_NUMERIC); $ids = array_slice($ids, 0, 10); $orders = array_filter( array_map( 'wc_get_order', $ids ) ); foreach ($orders as $order) { if (array_key_exists($order->get_status(), $wc2tabby)) { $result[] = self::getOrderHistoryOrderObject($order, $wc2tabby[$order->get_status()]); } } set_transient($tr_name, $result, HOUR_IN_SECONDS); return $result; } protected static function getOrderHistoryOrderObject($order, $tabby_status) { return [ "amount" => $order->get_total(), "payment_method" => $order->get_payment_method(), "purchased_at" => date(\DateTime::RFC3339, strtotime($order->get_date_created())), "status" => $tabby_status, "buyer" => self::getOrderHistoryOrderBuyerObject($order), "shipping_address" => self::getOrderHistoryOrderShippingAddressObject($order), "items" => self::getOrderHistoryOrderItemsObject($order) ]; } protected static function getOrderHistoryOrderBuyerObject($order) { return [ "name" => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), "phone" => $order->get_billing_phone(), "email" => $order->get_billing_email() ]; } protected static function getOrderHistoryOrderShippingAddressObject($order) { $address = $order->get_shipping_address_1() . ($order->get_shipping_address_2() ? ', '.$order->get_shipping_address_2() :''); if (empty($address)) { $address = $order->get_billing_address_1() . ($order->get_billing_address_2() ? ', '.$order->get_billing_address_2() :''); }; $city = $order->get_shipping_city(); if (empty($city)) { $city = $order->get_billing_city(); } return [ "address" => $address, "city" => $city ]; } protected static function getOrderHistoryOrderItemsObject($order) { $items = []; foreach ($order->get_items() as $item) { $items[] = [ "quantity" => $item->get_quantity(), "title" => $item->get_name(), "unit_price" => $order->get_item_total($item, true), "reference_id" => '' . $item->get_product_id() . ( $item->get_variation_id() ? '|' . $item->get_variation_id() : '' ), "ordered" => $item->get_quantity(), "captured" => $item->get_quantity(), "shipped" => $item->get_quantity() - $order->get_qty_refunded_for_item($item), "refunded" => $order->get_qty_refunded_for_item($item) ]; } return $items; } }