| 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('manage_options')) { |
| 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 |
$products_data = []; |
| 65 |
if (!empty($items)) { |
| 66 |
foreach ($items as $item_id => $item) { |
| 67 |
$product = $item->get_product(); |
| 68 |
$product_name = $item->get_name(); |
| 69 |
$item_qty = $item->get_quantity(); |
| 70 |
$item_total = $item->get_total(); |
| 71 |
$product_id = $item->get_product_id(); |
| 72 |
$variation_id = $item->get_variation_id(); |
| 73 |
$product_sku = $product->get_sku(); |
| 74 |
$product_price = $product->get_price() * $item_qty; |
| 75 |
|
| 76 |
// Fetch product image URL |
| 77 |
$product_image_id = $product->get_image_id(); |
| 78 |
$product_image_url = wp_get_attachment_url($product_image_id); |
| 79 |
|
| 80 |
if (!$product_image_url) { |
| 81 |
$product_image_url = wc_placeholder_img_src(); |
| 82 |
} |
| 83 |
|
| 84 |
$attributes = []; |
| 85 |
$meta_data = $item->get_meta_data(); |
| 86 |
|
| 87 |
foreach ($meta_data as $meta) { |
| 88 |
// Check for product attribute (pa_ is the prefix for standard attributes) |
| 89 |
if (strpos($meta->key, 'pa_') === 0) { |
| 90 |
// Standard product attribute |
| 91 |
$formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product)); |
| 92 |
$formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value |
| 93 |
|
| 94 |
// Add the attribute to the list with HTML markup |
| 95 |
$attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value); |
| 96 |
|
| 97 |
} else { |
| 98 |
|
| 99 |
$formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : ''; |
| 100 |
$formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; |
| 101 |
|
| 102 |
// Add the custom meta to the list with HTML markup |
| 103 |
$attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value); |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$product_attributes = !empty($attributes) ? implode(', ', $attributes) : ''; |
| 109 |
|
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
$billing_name = $order->get_formatted_billing_full_name(); |
| 114 |
$status = $order->get_status(); |
| 115 |
|
| 116 |
|
| 117 |
$shipping_first_name = $order->get_shipping_first_name(); |
| 118 |
$shipping_last_name = $order->get_shipping_last_name(); |
| 119 |
$shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name; |
| 120 |
|
| 121 |
|
| 122 |
$billing_country_code = $order->get_billing_country(); |
| 123 |
$billing_country_full_name = WC()->countries->countries[ $billing_country_code ]; |
| 124 |
$billing_state_code = $order->get_billing_state(); |
| 125 |
$billing_states = WC()->countries->get_states($billing_country_code); |
| 126 |
|
| 127 |
$billing_state_full_name = is_array($billing_states) && isset($billing_states[$billing_state_code]) ? $billing_states[$billing_state_code] : $billing_state_code; |
| 128 |
$shipping_country_code = $order->get_shipping_country(); |
| 129 |
$shipping_country_full_name = (!empty($shipping_country_code) && isset(WC()->countries->countries[$shipping_country_code])) |
| 130 |
? WC()->countries->countries[$shipping_country_code] |
| 131 |
: ''; |
| 132 |
|
| 133 |
$shipping_states = WC()->countries->get_states($shipping_country_code); |
| 134 |
$shipping_state_code = $order->get_shipping_state(); |
| 135 |
|
| 136 |
$shipping_full_state_name = is_array($shipping_states) && isset($shipping_states[$shipping_state_code]) ? $shipping_states[$shipping_state_code] : $shipping_state_code; |
| 137 |
|
| 138 |
$order_date = $order->get_date_created()->format('Y-m-d H:i:s'); |
| 139 |
|
| 140 |
// Convert the order date to the site's local timezone |
| 141 |
$localized_date = get_date_from_gmt($order_date); |
| 142 |
|
| 143 |
// Get the site's date and time formats |
| 144 |
$date_format = get_option('date_format'); |
| 145 |
$time_format = get_option('time_format'); |
| 146 |
|
| 147 |
// Format the date and time separately |
| 148 |
$formatted_date = date_i18n($date_format, strtotime($localized_date)); |
| 149 |
$formatted_time = date_i18n($time_format, strtotime($localized_date)); |
| 150 |
|
| 151 |
$item_data = [ |
| 152 |
'order_id' => $last_order_id, |
| 153 |
'billing_name' => $billing_name, |
| 154 |
'status' => $status, |
| 155 |
"order_number" => $order->get_order_number(), |
| 156 |
"order_fully_refunded" => wc_price($order->get_total_refunded()), |
| 157 |
'partial_refund_amount' => '-'.wc_price($order->get_total() - $order->get_total_refunded()), |
| 158 |
"order_currency" => $order->get_currency(), |
| 159 |
"billing_phone" => $order->get_billing_phone(), |
| 160 |
"shipping_total" => wc_price($order->get_shipping_total()), |
| 161 |
"order_subtotal" => wc_price($order->get_subtotal()), |
| 162 |
"shipping_tax_total" => wc_format_decimal($order->get_shipping_tax(), 2), |
| 163 |
"order_date" => $formatted_date, |
| 164 |
'order_time' => $formatted_time, |
| 165 |
"shipping_method" => $order->get_shipping_method(), |
| 166 |
"payment_method" => $order->get_payment_method_title(), |
| 167 |
"total" => wc_price($order->get_total(), 2), |
| 168 |
"billing_first_name" => $order->get_billing_first_name(), |
| 169 |
"billing_last_name" => $order->get_billing_last_name(), |
| 170 |
"billing_company" => $order->get_billing_company(), |
| 171 |
"billing_address_1" => $order->get_billing_address_1(), |
| 172 |
"billing_address_2" => $order->get_billing_address_2(), |
| 173 |
"billing_city" => $order->get_billing_city(), |
| 174 |
"billing_state" => $billing_state_full_name, |
| 175 |
"billing_postcode" => $order->get_billing_postcode(), |
| 176 |
"billing_country" => $billing_country_full_name, |
| 177 |
"billing_email" => $order->get_billing_email(), |
| 178 |
"shipping_name" => $shipping_full_name, |
| 179 |
"shipping_company" => $order->get_shipping_company(), |
| 180 |
"shipping_address_1" => $order->get_shipping_address_1(), |
| 181 |
"shipping_address_2" => $order->get_shipping_address_2(), |
| 182 |
"shipping_city" => $order->get_shipping_city(), |
| 183 |
"shipping_state" => $shipping_full_state_name, |
| 184 |
"shipping_postcode" => $order->get_shipping_postcode(), |
| 185 |
"shipping_country" => $shipping_country_full_name, |
| 186 |
"shipping_phone" => $order->get_shipping_phone(), |
| 187 |
"customer_note" => $order->get_customer_note(), |
| 188 |
"product_name" => $product_name, |
| 189 |
"product_image_url" => $product_image_url, |
| 190 |
"product_price" => wc_price($product_price), |
| 191 |
"product_sku" => $product_sku, |
| 192 |
"product_attributes" => $product_attributes, |
| 193 |
"quantity" => $item_qty, |
| 194 |
]; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
return [ |
| 199 |
'status' => 'success', |
| 200 |
'data' => $item_data, |
| 201 |
'message' => __( 'Last order item retrieved successfully.', 'emailkit' ), |
| 202 |
]; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return [ |
| 207 |
'status' => 'fail', |
| 208 |
'message' => __( 'No processing orders found or no items in the last order.', 'emailkit' ), |
| 209 |
]; |
| 210 |
} |
| 211 |
} |