PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.2.0
EmailKit – Email Customizer for WooCommerce & WP v1.2.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / OrderItem.php

OrderItem.php in EmailKit – Email Customizer for WooCommerce & WP 1.2.0, at includes/Admin/Api/OrderItem.php

120 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.']
31 ];
32 }
33
34 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
35 return [
36 'status' => 'fail',
37 'message' => ['Access denied.']
38 ];
39 }
40
41 $last_order = wc_get_orders(array(
42 'limit' => 1,
43 'orderby' => 'date',
44 'order' => 'DESC',
45 'status' => array(),
46 ));
47
48 if (!empty($last_order)) {
49 $last_order_id = reset($last_order)->get_id();
50 $order = wc_get_order($last_order_id);
51 $items = $order->get_items();
52
53 if (!empty($items)) {
54 $last_item = reset($items);
55 $product = wc_get_product($last_item->get_product_id());
56
57 if ($product) {
58 $product_price = $product->get_price();
59 }
60
61 $billing_name = $order->get_formatted_billing_full_name();
62 $status = $order->get_status();
63
64
65 $shipping_first_name = $order->get_shipping_first_name();
66 $shipping_last_name = $order->get_shipping_last_name();
67 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
68
69 $item_data = [
70 'order_id' => $last_order_id,
71 'product_name' => $last_item->get_name(),
72 'billing_name' => $billing_name,
73 'status' => $status,
74 'product_price' => wc_price($product_price),
75 'quantity' => $order->get_item_count(),
76 "order_number" => $order->get_order_number(),
77 "order_currency" => $order->get_currency(),
78 "billing_phone" => $order->get_billing_phone(),
79 "shipping_total" => wc_price($order->get_shipping_total()),
80 "order_subtotal" => wc_price($order->get_subtotal()),
81 "shipping_tax_total" => wc_format_decimal($order->get_shipping_tax(), 2),
82 "order_date" => gmdate('Y-m-d H:i:s', strtotime($order->get_date_created()->format('Y-m-d H:i:s'))),
83 "shipping_method" => $order->get_shipping_method(),
84 "payment_method" => $order->get_payment_method_title(),
85 "total" => wc_price($order->get_total(), 2),
86 "billing_first_name" => $order->get_billing_first_name(),
87 "billing_last_name" => $order->get_billing_last_name(),
88 "billing_company" => $order->get_billing_company(),
89 "billing_address_1" => $order->get_billing_address_1(),
90 "billing_address_2" => $order->get_billing_address_2(),
91 "billing_city" => $order->get_billing_city(),
92 "billing_state" => $order->get_billing_state(),
93 "billing_postcode" => $order->get_billing_postcode(),
94 "billing_country" => $order->get_billing_country(),
95 "billing_email" => $order->get_billing_email(),
96 "shipping_name" => $shipping_full_name,
97 "shipping_company" => $order->get_shipping_company(),
98 "shipping_address_1" => $order->get_shipping_address_1(),
99 "shipping_address_2" => $order->get_shipping_address_2(),
100 "shipping_city" => $order->get_shipping_city(),
101 "shipping_state" => $order->get_shipping_state(),
102 "shipping_postcode" => $order->get_shipping_postcode(),
103 "shipping_country" => $order->get_shipping_country(),
104 "customer_note" => $order->get_customer_note(),
105 ];
106
107 return [
108 'status' => 'success',
109 'data' => $item_data,
110 'message' => 'Last order item retrieved successfully.',
111 ];
112 }
113 }
114
115 return [
116 'status' => 'fail',
117 'message' => 'No processing orders found or no items in the last order.',
118 ];
119 }
120 }