query_vars['adscaleOrder'] = sanitize_key( wp_unslash( $_GET['adscaleOrder'] ) ); } // make sure endpoint request if ( ! isset( $wp->query_vars['adscaleOrder'] ) ) { return; } try { ob_start(); // Error handler self::setErrorHandler(); Helper::nocache_headers(); // Check Request (Guards) self::checkRequest(); ob_end_clean(); // Handle Request self::handleRequest(); // Default error Helper::send_response_formatted_error( self::makeErrorBody() ); } catch ( PluginApiException $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getApiErrorCode(), $ex->getMessage(), $ex->getApiErrorDetails() ) ); } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getCode(), $ex->getMessage() ) ); } } public static function checkRequest() { parent::checkRequest(); self::initParams(); // Id, min if ( ! self::checkParamIdForMin() ) { Helper::send_response_formatted_error( self::makeErrorBody('I001','Invalid argument value', 'id' ) ); } } public static function initParams() { self::$requestParams['id'] = Helper::get_post_body_data_value('id', 'absint'); self::$requestParams['meta'] = Helper::get_post_body_data_value('meta', 'bool'); // Missing Id guard if ( ! self::checkIfRequestParamExists( 'id' ) ) { Helper::send_response_formatted_error( self::makeErrorBody('M001','Missing argument', 'id' ) ); } // Params Type Hinting self::$requestParams['id'] = (int) self::$requestParams['id']; } /** * Check min Id * * @return bool is valid */ public static function checkParamIdForMin() { return self::$requestParams['id'] > 0; } /** * Handle API request * * @throws PluginApiException */ public static function handleRequest() { $orders_data = []; $args = [ 'orderby' => 'ID', 'order' => 'ASC', ]; add_filter( 'posts_clauses', [ self::class, 'queryFilter' ], 777, 2 ); $orders_objs = wc_get_orders( $args ); remove_filter( 'posts_clauses', [ self::class, 'queryFilter' ], 777 ); if ( ! is_array( $orders_objs ) ) { Helper::send_response_formatted_error( self::makeErrorBody('S000','Unknown error', 'Order could not be found' ) ); } foreach ( $orders_objs as $orders_obj ) { /* @var \WC_Order $orders_obj Order object */ $orders_data[] = self::getOrderData( $orders_obj ); } Helper::send_response_formatted_success( [ 'orders' => $orders_data, ] ); } /** * Filters all query clauses at once. * * @param string[] $clauses Associative array of the clauses for the query. * @param \WP_Query $query The WP_Query instance (passed by reference). * * @return string[] $clauses Filtered Array of query SQL clauses. */ public static function queryFilter( $clauses, $query ) { global $wpdb; if ( self::$requestParams['id'] ) { $clauses['where'] .= ' AND ' . $wpdb->prepare( "{$wpdb->posts}.ID = %d", (int) self::$requestParams['id'] ); } return $clauses; } /** * Get Order data * * @param \WC_Order $order * * @return array * @throws PluginApiException */ public static function getOrderData( $order ) { try { if ( ! $order instanceof \WC_Order ) { throw new \Exception( 'Not WC_Order object' ); } $order_id = (int) $order->get_id(); // Custom adscale 'cart_id' (factually it is session cookie value) $cart_id = $order->get_meta( '_adscale_cart_id', true ); $cart_id = $cart_id ? $cart_id : null; // date $date = $order->get_date_created(); $date = $date instanceof \WC_DateTime ? $date->format( 'Y-m-d H:i:s' ) : null; // payment_date $payment_date = $order->get_date_paid(); $payment_date = $payment_date instanceof \WC_DateTime ? $payment_date->format( 'Y-m-d H:i:s' ) : null; // update_date $update_date = $order->get_date_modified(); $update_date = $update_date instanceof \WC_DateTime ? $update_date->format( 'Y-m-d H:i:s' ) : null; // customer info $customer_id = $order->get_customer_id(); // Opt-in Checkbox Setup $optin = 'none'; $optinValue = $order->get_meta('_adscale__optin'); if ($optinValue) { $optin = 'yes'; } elseif ($optinValue === '0') { $optin = 'no'; } if ( $customer_id ) { $customer = new \WC_Customer( $customer_id ); $email = $customer->get_email(); // Get account email $phone = $customer->get_billing_phone(); $first_name = $customer->get_first_name(); $last_name = $customer->get_last_name(); } $email = ! empty( $email ) ? $email : $order->get_billing_email(); $phone = ! empty( $phone ) ? $phone : $order->get_billing_phone(); $first_name = ! empty( $first_name ) ? $first_name : $order->get_billing_first_name(); $last_name = ! empty( $last_name ) ? $last_name : $order->get_billing_last_name(); // country and state $shipping_country = $order->get_shipping_country(); $shipping_state = $order->get_shipping_state(); $shipping_state = Helper::get_full_state_name( $shipping_country, $shipping_state ); $billing_country = $order->get_billing_country(); $billing_state = $order->get_billing_state(); $billing_state = Helper::get_full_state_name( $billing_country, $billing_state ); // items $order_items_data = []; $order_items_objs = $order->get_items(); foreach ( $order_items_objs as $order_item_obj ) { if ( $order_item_obj instanceof \WC_Order_Item_Product ) { $order_items_data[] = self::getOrderItemData( $order_item_obj ); } } $order_meta_data = []; if ( ! empty( self::$requestParams['meta'] ) ) { foreach ( $order->get_meta_data() as $meta_item ) { if ( ! $meta_item instanceof \WC_Meta_Data ) { continue; } $meta_data = $meta_item->get_data(); if ( empty( $meta_data['key'] ) ) { continue; } $order_meta_data[ $meta_data['key'] ] = $meta_data['value']; } } $order_data = [ 'id' => $order_id, 'cart_id' => $cart_id, 'date' => $date, 'payment_date' => $payment_date, 'update_date' => $update_date, 'status' => $order->get_status(), 'payment_method' => $order->get_payment_method_title(), 'adscale_ad_link' => Helper::get_saved_adscale_ad_link($order_id), 'currency' => $order->get_currency(), 'total_price' => Helper::format_price_with_precision($order->get_total()), 'shipping_price' => Helper::format_price_with_precision(self::getResolvedTotalShipping($order)), 'discount_amount' => Helper::format_price_with_precision(self::getResolvedTotalDiscount($order)), 'ip' => $order->get_customer_ip_address(), 'email' => $email, 'phone' => $phone, 'first_name' => $first_name, 'last_name' => $last_name, 'optin_sms' => $optin, 'optin_email'=> $optin, 'shipping_country' => $shipping_country, 'shipping_state' => $shipping_state, 'shipping_city' => $order->get_shipping_city(), 'shipping_address' => $order->get_shipping_address_1(), 'shipping_address2' => $order->get_shipping_address_2(), 'shipping_zip' => $order->get_shipping_postcode(), 'billing_country' => $billing_country, 'billing_state' => $billing_state, 'billing_city' => $order->get_billing_city(), 'billing_address' => $order->get_billing_address_1(), 'billing_address2' => $order->get_billing_address_2(), 'billing_zip' => $order->get_billing_postcode(), 'items' => $order_items_data, ]; if ( ! empty( self::$requestParams['meta'] ) ) { $order_data['meta'] = $order_meta_data; } return $order_data; } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getCode(), $ex->getMessage() ) ); } } /** * Get resolved total shipping * * @param \WC_Order $order * * @return string */ public static function getResolvedTotalShipping($order) { // Show shipping including tax. $shipping_total = (float)$order->get_shipping_total(); $shipping_tax = (float)$order->get_shipping_tax(); $shipping = $shipping_total + $shipping_tax; return (string)$shipping; } /** * Get resolved total discount * * @param \WC_Order $order * * @return string */ public static function getResolvedTotalDiscount( $order ) { return (string) $order->get_total_discount( true ); } /** * Get order item product data * * @param \WC_Order_Item_Product $order_item Order Item instance * * @return array * @throws PluginApiException */ public static function getOrderItemData( $order_item ) { try { $quantity = $order_item->get_quantity(); $total_price = (string) ( $order_item->get_total() + $order_item->get_total_tax() ); // Discounted total with tax $single_item_price = $quantity > 0 ? $total_price / $quantity : null; $single_item_price_excl_tax = $quantity > 0 ? $order_item->get_total() / $quantity : null; $total_discount = $order_item->get_subtotal() - $order_item->get_total(); return [ 'product_id' => $order_item->get_product_id(), 'variant_id' => $order_item->get_variation_id(), 'quantity' => $order_item->get_quantity(), 'total_price' => Helper::format_price_with_precision( $total_price ), 'single_item_price' => Helper::format_price_with_precision( $single_item_price ), 'single_item_price_excl_tax' => Helper::format_price_with_precision( $single_item_price_excl_tax ), 'single_item_discount_amount' => '0', 'total_discount_amount' => Helper::format_price_with_precision( $total_discount ), ]; } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody('S000','Unknown error', $ex->getMessage() ) ); } } }