| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class OrderItem { |
| 8 |
|
| 9 |
public $prefix = ''; |
| 10 |
public $param = ''; |
| 11 |
public $request = null; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
|
| 15 |
add_action('rest_api_init', function () { |
| 16 |
register_rest_route('emailkit/v1', 'last-order-item', array( |
| 17 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 18 |
'callback' => [$this, 'get_last_order_item'], |
| 19 |
'permission_callback' => '__return_true', |
| 20 |
)); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_last_order_item($request) |
| 25 |
{ |
| 26 |
|
| 27 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 28 |
return [ |
| 29 |
'status' => 'fail', |
| 30 |
'message' => [ __( 'Nonce mismatch.', 'emailkit' )] |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 35 |
return [ |
| 36 |
'status' => 'fail', |
| 37 |
'message' => [ __( 'Access denied.', 'emailkit' ) ] |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
if(!function_exists('is_plugin_active')){ |
| 42 |
// Include necessary WordPress files |
| 43 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 44 |
} |
| 45 |
|
| 46 |
if(!is_plugin_active('woocommerce/woocommerce.php')){ |
| 47 |
return [ |
| 48 |
'status' => 'fail', |
| 49 |
'message' => __( 'No processing orders found or no items in the last order.', 'emailkit' ), |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
$last_order = wc_get_orders(array( |
| 54 |
'limit' => 1, |
| 55 |
'orderby' => 'date', |
| 56 |
'order' => 'DESC', |
| 57 |
'status' => array_keys( wc_get_order_statuses()), |
| 58 |
)); |
| 59 |
|
| 60 |
if (!empty($last_order)) { |
| 61 |
$last_order_id = reset($last_order)->get_id(); |
| 62 |
$order = wc_get_order($last_order_id); |
| 63 |
$items = $order->get_items(); |
| 64 |
|
| 65 |
if (!empty($items)) { |
| 66 |
$last_item = reset($items); |
| 67 |
$product = wc_get_product($last_item->get_product_id()); |
| 68 |
|
| 69 |
if ($product) { |
| 70 |
$product_price = $product->get_price(); |
| 71 |
} |
| 72 |
|
| 73 |
$billing_name = $order->get_formatted_billing_full_name(); |
| 74 |
$status = $order->get_status(); |
| 75 |
|
| 76 |
|
| 77 |
$shipping_first_name = $order->get_shipping_first_name(); |
| 78 |
$shipping_last_name = $order->get_shipping_last_name(); |
| 79 |
$shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name; |
| 80 |
|
| 81 |
$item_data = [ |
| 82 |
'order_id' => $last_order_id, |
| 83 |
'product_name' => $last_item->get_name(), |
| 84 |
'billing_name' => $billing_name, |
| 85 |
'status' => $status, |
| 86 |
'product_price' => wc_price($product_price), |
| 87 |
'quantity' => $order->get_item_count(), |
| 88 |
"order_number" => $order->get_order_number(), |
| 89 |
"order_fully_refunded" => '-'.wc_format_decimal($order->get_total_refunded(), 2), |
| 90 |
"order_currency" => $order->get_currency(), |
| 91 |
"billing_phone" => $order->get_billing_phone(), |
| 92 |
"shipping_total" => wc_price($order->get_shipping_total()), |
| 93 |
"order_subtotal" => wc_price($order->get_subtotal()), |
| 94 |
"shipping_tax_total" => wc_format_decimal($order->get_shipping_tax(), 2), |
| 95 |
"order_date" => gmdate('Y-m-d H:i:s', strtotime($order->get_date_created()->format('Y-m-d H:i:s'))), |
| 96 |
"shipping_method" => $order->get_shipping_method(), |
| 97 |
"payment_method" => $order->get_payment_method_title(), |
| 98 |
"total" => wc_price($order->get_total(), 2), |
| 99 |
"billing_first_name" => $order->get_billing_first_name(), |
| 100 |
"billing_last_name" => $order->get_billing_last_name(), |
| 101 |
"billing_company" => $order->get_billing_company(), |
| 102 |
"billing_address_1" => $order->get_billing_address_1(), |
| 103 |
"billing_address_2" => $order->get_billing_address_2(), |
| 104 |
"billing_city" => $order->get_billing_city(), |
| 105 |
"billing_state" => $order->get_billing_state(), |
| 106 |
"billing_postcode" => $order->get_billing_postcode(), |
| 107 |
"billing_country" => $order->get_billing_country(), |
| 108 |
"billing_email" => $order->get_billing_email(), |
| 109 |
"shipping_name" => $shipping_full_name, |
| 110 |
"shipping_company" => $order->get_shipping_company(), |
| 111 |
"shipping_address_1" => $order->get_shipping_address_1(), |
| 112 |
"shipping_address_2" => $order->get_shipping_address_2(), |
| 113 |
"shipping_city" => $order->get_shipping_city(), |
| 114 |
"shipping_state" => $order->get_shipping_state(), |
| 115 |
"shipping_postcode" => $order->get_shipping_postcode(), |
| 116 |
"shipping_country" => $order->get_shipping_country(), |
| 117 |
"shipping_phone" => $order->get_shipping_phone(), |
| 118 |
"customer_note" => $order->get_customer_note(), |
| 119 |
]; |
| 120 |
|
| 121 |
return [ |
| 122 |
'status' => 'success', |
| 123 |
'data' => $item_data, |
| 124 |
'message' => __( 'Last order item retrieved successfully.', 'emailkit' ), |
| 125 |
]; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'status' => 'fail', |
| 131 |
'message' => __( 'No processing orders found or no items in the last order.', 'emailkit' ), |
| 132 |
]; |
| 133 |
} |
| 134 |
} |