Skip to content

Commit 49f4f79

Browse files
optimize order data and enable export of order with rest
1 parent 6b3b043 commit 49f4f79

File tree

3 files changed

+133
-103
lines changed

3 files changed

+133
-103
lines changed

src/Controllers/CLI/Order_Controller.php

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -27,105 +27,12 @@ function execute()
2727
]);
2828

2929
foreach ($all_ids as $order) {
30-
global $wpdb;
31-
32-
$lines = $wpdb->get_results("
33-
SELECT
34-
*
35-
36-
FROM
37-
wp_woocommerce_order_items
38-
39-
WHERE
40-
order_id = " . $order->ID . " AND
41-
order_item_type IN('line_item')
42-
43-
ORDER BY
44-
order_item_id ASC
45-
");
46-
47-
$order_lines = [];
48-
foreach ($lines as $line) {
49-
$qty = Helpers::wcGetOrderLineMeta($line->order_item_id, '_qty');
50-
$product_id = Helpers::wcGetOrderLineMeta($line->order_item_id, '_product_id');
51-
$line_total = Helpers::wcGetOrderLineMeta($line->order_item_id, '_line_total');
52-
$product_sku = \get_post_meta($product_id->meta_value, '_sku', true);
53-
# _variation_id
54-
55-
$order_lines[] = [
56-
'product_id' => (string) $product_id->meta_value,
57-
'price_total' => (float) ((int) $line_total->meta_value),
58-
'price' => (float) ((int) $line_total->meta_value/(int) $qty->meta_value),
59-
'sku' => $product_sku,
60-
'qty' => (int) $qty->meta_value,
61-
];
62-
}
63-
64-
$shipment_firstname = \get_post_meta($order->ID, '_shipping_first_name', true);
65-
$shipment_lastname = \get_post_meta($order->ID, '_shipping_last_name', true);
66-
$shipment_company = \get_post_meta($order->ID, '_shipping_company', true);
67-
$shipment_address_1 = \get_post_meta($order->ID, '_shipping_address_1', true);
68-
$shipment_city = \get_post_meta($order->ID, '_shipping_city', true);
69-
$shipment_postcode = \get_post_meta($order->ID, '_shipping_postcode', true);
70-
$shipment_country = \get_post_meta($order->ID, '_shipping_country', true);
71-
$billing_email = \get_post_meta($order->ID, '_billing_email', true);
72-
$billing_phone = \get_post_meta($order->ID, '_billing_phone', true);
73-
74-
$customer_ip_address = \get_post_meta($order->ID, '_customer_ip_address', true);
75-
$customer_user_agent = \get_post_meta($order->ID, '_customer_user_agent', true);
76-
$payment_method = \get_post_meta($order->ID, '_payment_method_title', true);
77-
78-
# _order_total
79-
# _order_key: wc_order_en5sptKclNffY
80-
8130
$shipment_data = [[
8231
'method' => 'order',
83-
'data' => [
84-
'orderNo' => (string) $order->ID,
85-
'referenceNo' => (string) $order->ID,
86-
'uniqueReferenceNo' => (string) $order->ID,
87-
'status' => $order->post_status,
88-
'description' => $order->post_excerpt,
89-
'printDeliveryNote' => false,
90-
'payment' => [
91-
'method' => $payment_method,
92-
'paid' => 1,
93-
],
94-
'client_details' => [
95-
'browser_ip' => $customer_ip_address,
96-
'user_agent' => $customer_user_agent,
97-
],
98-
'sender' => [
99-
'name' => get_bloginfo('name'),
100-
'street1' => get_option('woocommerce_store_address'),
101-
'zipcode' => get_option('woocommerce_store_postcode'),
102-
'city' => get_option('woocommerce_store_city'),
103-
'country' => get_option('woocommerce_default_country'),
104-
'phone' => '98765432',
105-
'email' => 'asdfgh@dfghj.dk',
106-
'country' => 'Denmark',
107-
"country_code" => 'DK'
108-
],
109-
'recipient' => [
110-
'name' => $shipment_firstname . ' ' . $shipment_lastname,
111-
'attention' => '',
112-
'street1' => $shipment_address_1,
113-
'zipcode' => $shipment_postcode,
114-
'city' => $shipment_city,
115-
'country' => $shipment_country,
116-
'phone' => $billing_phone,
117-
'email' => $billing_email,
118-
'country' => 'Denmark',
119-
"country_code" => 'DK'
120-
],
121-
"deliveryMethod" => 'none',
122-
"droppointId" => '',
123-
"items" => $order_lines
124-
]
32+
'data' => Helpers::getOrderData($order->ID)
12533
]];
12634

12735
$response = $webhook->push($shipment_data);
128-
print_r($response);
12936

13037
if ($response['statusCode'] === 201) {
13138
update_post_meta($order->ID, 'smartpack_wms_state', 'synced');

src/Controllers/RestRoutes_Controller.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ public function register_routes()
5858
]
5959
);
6060

61-
62-
// register_rest_route(
63-
// self::get_route_namespace(),
64-
// self::ROUTES['exportOrders'],
65-
// [
66-
// 'methods' => WP_REST_Server::CREATABLE,
67-
// 'callback' => [$this, 'exportOrders']
68-
// ]
69-
// );
61+
register_rest_route(
62+
self::get_route_namespace(),
63+
self::ROUTES['exportOrders'],
64+
[
65+
'methods' => WP_REST_Server::READABLE,
66+
'callback' => [$this, 'exportOrders']
67+
]
68+
);
7069
}
7170

7271
public function stockChanged(WP_REST_Request $request)
@@ -214,6 +213,18 @@ public function exportProducts(WP_REST_Request $request) {
214213
]);
215214
}
216215

216+
public function exportOrders() {
217+
$limit = (isset($_GET['limit']) ? (int) $_GET['limit'] : 100);
218+
$offset = (isset($_GET['offset']) ? (int) $_GET['offset'] : 0);
219+
220+
$order_data = [];
221+
foreach (Helpers::getAllOrders($limit, $offset) as $order) {
222+
$order_data[] = Helpers::getOrderData($order->ID, $order);
223+
}
224+
225+
return $order_data;
226+
}
227+
217228
public function init()
218229
{
219230
add_action('rest_api_init', function () {

src/helpers/helpers.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,116 @@ static function getProductData($product_id) {
6868

6969
return $product_data;
7070
}
71+
72+
static function getAllOrders($limit=100, $offset=0) {
73+
$orders = get_posts([
74+
'post_type' => 'shop_order',
75+
'posts_per_page' => $limit,
76+
'offset' => $offset,
77+
'post_status' => [
78+
'wc-processing'
79+
]
80+
]);
81+
82+
return $orders;
83+
}
84+
85+
static function getOrderData($order_id, $order) {
86+
global $wpdb;
87+
88+
$lines = $wpdb->get_results("
89+
SELECT
90+
*
91+
92+
FROM
93+
wp_woocommerce_order_items
94+
95+
WHERE
96+
order_id = " . $order_id . " AND
97+
order_item_type IN('line_item')
98+
99+
ORDER BY
100+
order_item_id ASC
101+
");
102+
103+
$order_lines = [];
104+
105+
foreach ($lines as $line) {
106+
$qty = Helpers::wcGetOrderLineMeta($line->order_item_id, '_qty');
107+
$product_id = Helpers::wcGetOrderLineMeta($line->order_item_id, '_product_id');
108+
$line_total = Helpers::wcGetOrderLineMeta($line->order_item_id, '_line_total');
109+
$product_sku = \get_post_meta($product_id->meta_value, '_sku', true);
110+
# _variation_id
111+
112+
$order_lines[] = [
113+
'product_id' => (string) $product_id->meta_value,
114+
'price_total' => (float) ((int) $line_total->meta_value),
115+
'price' => (float) ((int) $line_total->meta_value/(int) $qty->meta_value),
116+
'sku' => $product_sku,
117+
'qty' => (int) $qty->meta_value,
118+
];
119+
}
120+
121+
$shipment_firstname = \get_post_meta($order_id, '_shipping_first_name', true);
122+
$shipment_lastname = \get_post_meta($order_id, '_shipping_last_name', true);
123+
$shipment_company = \get_post_meta($order_id, '_shipping_company', true);
124+
$shipment_address_1 = \get_post_meta($order_id, '_shipping_address_1', true);
125+
$shipment_city = \get_post_meta($order_id, '_shipping_city', true);
126+
$shipment_postcode = \get_post_meta($order_id, '_shipping_postcode', true);
127+
$shipment_country = \get_post_meta($order_id, '_shipping_country', true);
128+
$billing_email = \get_post_meta($order_id, '_billing_email', true);
129+
$billing_phone = \get_post_meta($order_id, '_billing_phone', true);
130+
131+
$customer_ip_address = \get_post_meta($order_id, '_customer_ip_address', true);
132+
$customer_user_agent = \get_post_meta($order_id, '_customer_user_agent', true);
133+
$payment_method = \get_post_meta($order_id, '_payment_method_title', true);
134+
135+
# _order_total
136+
# _order_key: wc_order_en5sptKclNffY
137+
138+
$order_data = [
139+
'orderNo' => (string) $order_id,
140+
'referenceNo' => (string) $order_id,
141+
'uniqueReferenceNo' => (string) $order_id,
142+
'status' => $order->post_status,
143+
'description' => $order->post_excerpt,
144+
'printDeliveryNote' => false,
145+
'payment' => [
146+
'method' => $payment_method,
147+
'paid' => 1,
148+
],
149+
'client_details' => [
150+
'browser_ip' => $customer_ip_address,
151+
'user_agent' => $customer_user_agent,
152+
],
153+
'sender' => [
154+
'name' => get_bloginfo('name'),
155+
'street1' => get_option('woocommerce_store_address'),
156+
'zipcode' => get_option('woocommerce_store_postcode'),
157+
'city' => get_option('woocommerce_store_city'),
158+
'country' => get_option('woocommerce_default_country'),
159+
'phone' => '98765432',
160+
'email' => 'asdfgh@dfghj.dk',
161+
'country' => 'Denmark',
162+
"country_code" => 'DK'
163+
],
164+
'recipient' => [
165+
'name' => $shipment_firstname . ' ' . $shipment_lastname,
166+
'attention' => '',
167+
'street1' => $shipment_address_1,
168+
'zipcode' => $shipment_postcode,
169+
'city' => $shipment_city,
170+
'country' => $shipment_country,
171+
'phone' => $billing_phone,
172+
'email' => $billing_email,
173+
'country' => 'Denmark',
174+
"country_code" => 'DK'
175+
],
176+
"deliveryMethod" => 'none',
177+
"droppointId" => '',
178+
"items" => $order_lines
179+
];
180+
181+
return $order_data;
182+
}
71183
}

0 commit comments

Comments
 (0)