wc-admin-functions.php
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* WooCommerce Admin Functions
*
* @author WooThemes
* @category Core
* @package WooCommerce/Admin/Functions
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Get all WooCommerce screen ids.
*
* @return array
*/
function wc_get_screen_ids() {
$wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
$screen_ids = array(
'toplevel_page_' . $wc_screen_id,
$wc_screen_id . '_page_wc-reports',
$wc_screen_id . '_page_wc-shipping',
$wc_screen_id . '_page_wc-settings',
$wc_screen_id . '_page_wc-status',
$wc_screen_id . '_page_wc-addons',
'toplevel_page_wc-reports',
'product_page_product_attributes',
'edit-product',
'product',
'edit-shop_coupon',
'shop_coupon',
'edit-product_cat',
'edit-product_tag',
'profile',
'user-edit'
);
foreach ( wc_get_order_types() as $type ) {
$screen_ids[] = $type;
$screen_ids[] = 'edit-' . $type;
}
return apply_filters( 'woocommerce_screen_ids', $screen_ids );
}
/**
* Create a page and store the ID in an option.
*
* @param mixed $slug Slug for the new page
* @param string $option Option name to store the page's ID
* @param string $page_title (default: '') Title for the new page
* @param string $page_content (default: '') Content for the new page
* @param int $post_parent (default: 0) Parent for the new page
* @return int page ID
*/
function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
global $wpdb;
$option_value = get_option( $option );
if ( $option_value > 0 ) {
$page_object = get_post( $option_value );
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
// Valid page is already in place
return $page_object->ID;
}
}
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode)
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
} else {
// Search for an existing page with the specified page slug
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) );
}
$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
if ( $valid_page_found ) {
if ( $option ) {
update_option( $option, $valid_page_found );
}
return $valid_page_found;
}
// Search for a matching valid trashed page
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode)
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
} else {
// Search for an existing page with the specified page slug
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
}
if ( $trashed_page_found ) {
$page_id = $trashed_page_found;
$page_data = array(
'ID' => $page_id,
'post_status' => 'publish',
);
wp_update_post( $page_data );
} else {
$page_data = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $slug,
'post_title' => $page_title,
'post_content' => $page_content,
'post_parent' => $post_parent,
'comment_status' => 'closed'
);
$page_id = wp_insert_post( $page_data );
}
if ( $option ) {
update_option( $option, $page_id );
}
return $page_id;
}
/**
* Output admin fields.
*
* Loops though the woocommerce options array and outputs each field.
*
* @param array $options Opens array to output
*/
function woocommerce_admin_fields( $options ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include 'class-wc-admin-settings.php';
}
WC_Admin_Settings::output_fields( $options );
}
/**
* Update all settings which are passed.
*
* @param array $options
*/
function woocommerce_update_options( $options ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include 'class-wc-admin-settings.php';
}
WC_Admin_Settings::save_fields( $options );
}
/**
* Get a setting from the settings API.
*
* @param mixed $option_name
* @param mixed $default
* @return string
*/
function woocommerce_settings_get_option( $option_name, $default = '' ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include 'class-wc-admin-settings.php';
}
return WC_Admin_Settings::get_option( $option_name, $default );
}
/**
* Save order items.
*
* @since 2.2
* @param int $order_id Order ID
* @param array $items Order items to save
*/
function wc_save_order_items( $order_id, $items ) {
// Allow other plugins to check change in order items before they are saved
do_action( 'woocommerce_before_save_order_items', $order_id, $items );
global $wpdb;
// Order items + fees
$subtotal = 0;
$total = 0;
$subtotal_tax = 0;
$total_tax = 0;
$taxes = array( 'items' => array(), 'shipping' => array() );
if ( isset( $items['order_item_id'] ) ) {
$line_total = $line_subtotal = $line_tax = $line_subtotal_tax = array();
foreach ( $items['order_item_id'] as $item_id ) {
$item_id = absint( $item_id );
if ( isset( $items['order_item_name'][ $item_id ] ) ) {
$wpdb->update(
$wpdb->prefix . 'woocommerce_order_items',
array( 'order_item_name' => wc_clean( wp_unslash( $items['order_item_name'][ $item_id ] ) ) ),
array( 'order_item_id' => $item_id ),
array( '%s' ),
array( '%d' )
);
}
if ( isset( $items['order_item_qty'][ $item_id ] ) ) {
wc_update_order_item_meta( $item_id, '_qty', wc_stock_amount( $items['order_item_qty'][ $item_id ] ) );
}
if ( isset( $items['order_item_tax_class'][ $item_id ] ) ) {
wc_update_order_item_meta( $item_id, '_tax_class', wc_clean( $items['order_item_tax_class'][ $item_id ] ) );
}
// Get values. Subtotals might not exist, in which case copy value from total field
$line_total[ $item_id ] = isset( $items['line_total'][ $item_id ] ) ? $items['line_total'][ $item_id ] : 0;
$line_subtotal[ $item_id ] = isset( $items['line_subtotal'][ $item_id ] ) ? $items['line_subtotal'][ $item_id ] : $line_total[ $item_id ];
$line_tax[ $item_id ] = isset( $items['line_tax'][ $item_id ] ) ? $items['line_tax'][ $item_id ] : array();
$line_subtotal_tax[ $item_id ] = isset( $items['line_subtotal_tax'][ $item_id ] ) ? $items['line_subtotal_tax'][ $item_id ] : $line_tax[ $item_id ];
// Format taxes
$line_taxes = array_map( 'wc_format_decimal', $line_tax[ $item_id ] );
$line_subtotal_taxes = array_map( 'wc_format_decimal', $line_subtotal_tax[ $item_id ] );
// Update values
wc_update_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $line_subtotal[ $item_id ] ) );
wc_update_order_item_meta( $item_id, '_line_total', wc_format_decimal( $line_total[ $item_id ] ) );
wc_update_order_item_meta( $item_id, '_line_subtotal_tax', array_sum( $line_subtotal_taxes ) );
wc_update_order_item_meta( $item_id, '_line_tax', array_sum( $line_taxes ) );
// Save line tax data - Since 2.2
wc_update_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $line_taxes, 'subtotal' => $line_subtotal_taxes ) );
$taxes['items'][] = $line_taxes;
// Total up
$subtotal += wc_format_decimal( $line_subtotal[ $item_id ] );
$total += wc_format_decimal( $line_total[ $item_id ] );
$subtotal_tax += array_sum( $line_subtotal_taxes );
$total_tax += array_sum( $line_taxes );
// Clear meta cache
wp_cache_delete( $item_id, 'order_item_meta' );
}
}
// Save meta
$meta_keys = isset( $items['meta_key'] ) ? $items['meta_key'] : array();
$meta_values = isset( $items['meta_value'] ) ? $items['meta_value'] : array();
foreach ( $meta_keys as $id => $meta_key ) {
$meta_value = ( empty( $meta_values[ $id ] ) && ! is_numeric( $meta_values[ $id ] ) ) ? '' : $meta_values[ $id ];
// Delele blank item meta entries
if ( $meta_key === '' && $meta_value === '' ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_id = %d", $id ) );
} else {
$wpdb->update(
$wpdb->prefix . 'woocommerce_order_itemmeta',
array(
'meta_key' => wp_unslash( $meta_key ),
'meta_value' => wp_unslash( $meta_value )
),
array( 'meta_id' => $id ),
array( '%s', '%s' ),
array( '%d' )
);
}
}
// Shipping Rows
$order_shipping = 0;
if ( isset( $items['shipping_method_id'] ) ) {
foreach ( $items['shipping_method_id'] as $item_id ) {
$item_id = absint( $item_id );
$method_id = isset( $items['shipping_method'][ $item_id ] ) ? wc_clean( $items['shipping_method'][ $item_id ] ) : '';
$method_title = isset( $items['shipping_method_title'][ $item_id ] ) ? wc_clean( wp_unslash( $items['shipping_method_title'][ $item_id ] ) ) : '';
$cost = isset( $items['shipping_cost'][ $item_id ] ) ? wc_format_decimal( $items['shipping_cost'][ $item_id ] ) : '';
$ship_taxes = isset( $items['shipping_taxes'][ $item_id ] ) ? array_map( 'wc_format_decimal', $items['shipping_taxes'][ $item_id ] ) : array();
$wpdb->update(
$wpdb->prefix . 'woocommerce_order_items',
array( 'order_item_name' => $method_title ),
array( 'order_item_id' => $item_id ),
array( '%s' ),
array( '%d' )
);
wc_update_order_item_meta( $item_id, 'method_id', $method_id );
wc_update_order_item_meta( $item_id, 'cost', $cost );
wc_update_order_item_meta( $item_id, 'taxes', $ship_taxes );
$taxes['shipping'][] = $ship_taxes;
$order_shipping += $cost;
}
}
// Taxes
$order_taxes = isset( $items['order_taxes'] ) ? $items['order_taxes'] : array();
$taxes_items = array();
$taxes_shipping = array();
$total_tax = 0;
$total_shipping_tax = 0;
// Sum items taxes
foreach ( $taxes['items'] as $rates ) {
foreach ( $rates as $id => $value ) {
if ( isset( $taxes_items[ $id ] ) ) {
$taxes_items[ $id ] += $value;
} else {
$taxes_items[ $id ] = $value;
}
}
}
// Sum shipping taxes
foreach ( $taxes['shipping'] as $rates ) {
foreach ( $rates as $id => $value ) {
if ( isset( $taxes_shipping[ $id ] ) ) {
$taxes_shipping[ $id ] += $value;
} else {
$taxes_shipping[ $id ] = $value;
}
}
}
// Update order taxes
foreach ( $order_taxes as $item_id => $rate_id ) {
if ( isset( $taxes_items[ $rate_id ] ) ) {
$_total = wc_format_decimal( $taxes_items[ $rate_id ] );
wc_update_order_item_meta( $item_id, 'tax_amount', $_total );
$total_tax += $_total;
}
if ( isset( $taxes_shipping[ $rate_id ] ) ) {
$_total = wc_format_decimal( $taxes_shipping[ $rate_id ] );
wc_update_order_item_meta( $item_id, 'shipping_tax_amount', $_total );
$total_shipping_tax += $_total;
}
}
// Update order shipping total
update_post_meta( $order_id, '_order_shipping', $order_shipping );
// Update cart discount from item totals
update_post_meta( $order_id, '_cart_discount', $subtotal - $total );
update_post_meta( $order_id, '_cart_discount_tax', $subtotal_tax - $total_tax );
// Update totals
update_post_meta( $order_id, '_order_total', wc_format_decimal( $items['_order_total'] ) );
// Update tax
update_post_meta( $order_id, '_order_tax', wc_format_decimal( $total_tax ) );
update_post_meta( $order_id, '_order_shipping_tax', wc_format_decimal( $total_shipping_tax ) );
// Remove old values
delete_post_meta( $order_id, '_shipping_method' );
delete_post_meta( $order_id, '_shipping_method_title' );
// Set the currency
add_post_meta( $order_id, '_order_currency', get_woocommerce_currency(), true );
// Update version after saving
update_post_meta( $order_id, '_order_version', WC_VERSION );
// Inform other plugins that the items have been saved
do_action( 'woocommerce_saved_order_items', $order_id, $items );
}