Нет описания

class-wc-meta-box-order-data.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. /**
  3. * Order Data
  4. *
  5. * Functions for displaying the order data meta box.
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce\Admin\Meta Boxes
  10. * @version 2.2.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * WC_Meta_Box_Order_Data Class.
  17. */
  18. class WC_Meta_Box_Order_Data {
  19. /**
  20. * Billing fields.
  21. *
  22. * @var array
  23. */
  24. protected static $billing_fields = array();
  25. /**
  26. * Shipping fields.
  27. *
  28. * @var array
  29. */
  30. protected static $shipping_fields = array();
  31. /**
  32. * Init billing and shipping fields we display + save.
  33. */
  34. public static function init_address_fields() {
  35. self::$billing_fields = apply_filters(
  36. 'woocommerce_admin_billing_fields',
  37. array(
  38. 'first_name' => array(
  39. 'label' => __( 'First name', 'woocommerce' ),
  40. 'show' => false,
  41. ),
  42. 'last_name' => array(
  43. 'label' => __( 'Last name', 'woocommerce' ),
  44. 'show' => false,
  45. ),
  46. 'company' => array(
  47. 'label' => __( 'Company', 'woocommerce' ),
  48. 'show' => false,
  49. ),
  50. 'address_1' => array(
  51. 'label' => __( 'Address line 1', 'woocommerce' ),
  52. 'show' => false,
  53. ),
  54. 'address_2' => array(
  55. 'label' => __( 'Address line 2', 'woocommerce' ),
  56. 'show' => false,
  57. ),
  58. 'city' => array(
  59. 'label' => __( 'City', 'woocommerce' ),
  60. 'show' => false,
  61. ),
  62. 'postcode' => array(
  63. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  64. 'show' => false,
  65. ),
  66. 'country' => array(
  67. 'label' => __( 'Country / Region', 'woocommerce' ),
  68. 'show' => false,
  69. 'class' => 'js_field-country select short',
  70. 'type' => 'select',
  71. 'options' => array( '' => __( 'Select a country / region&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
  72. ),
  73. 'state' => array(
  74. 'label' => __( 'State / County', 'woocommerce' ),
  75. 'class' => 'js_field-state select short',
  76. 'show' => false,
  77. ),
  78. 'email' => array(
  79. 'label' => __( 'Email address', 'woocommerce' ),
  80. ),
  81. 'phone' => array(
  82. 'label' => __( 'Phone', 'woocommerce' ),
  83. ),
  84. )
  85. );
  86. self::$shipping_fields = apply_filters(
  87. 'woocommerce_admin_shipping_fields',
  88. array(
  89. 'first_name' => array(
  90. 'label' => __( 'First name', 'woocommerce' ),
  91. 'show' => false,
  92. ),
  93. 'last_name' => array(
  94. 'label' => __( 'Last name', 'woocommerce' ),
  95. 'show' => false,
  96. ),
  97. 'company' => array(
  98. 'label' => __( 'Company', 'woocommerce' ),
  99. 'show' => false,
  100. ),
  101. 'address_1' => array(
  102. 'label' => __( 'Address line 1', 'woocommerce' ),
  103. 'show' => false,
  104. ),
  105. 'address_2' => array(
  106. 'label' => __( 'Address line 2', 'woocommerce' ),
  107. 'show' => false,
  108. ),
  109. 'city' => array(
  110. 'label' => __( 'City', 'woocommerce' ),
  111. 'show' => false,
  112. ),
  113. 'postcode' => array(
  114. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  115. 'show' => false,
  116. ),
  117. 'country' => array(
  118. 'label' => __( 'Country / Region', 'woocommerce' ),
  119. 'show' => false,
  120. 'type' => 'select',
  121. 'class' => 'js_field-country select short',
  122. 'options' => array( '' => __( 'Select a country / region&hellip;', 'woocommerce' ) ) + WC()->countries->get_shipping_countries(),
  123. ),
  124. 'state' => array(
  125. 'label' => __( 'State / County', 'woocommerce' ),
  126. 'class' => 'js_field-state select short',
  127. 'show' => false,
  128. ),
  129. 'phone' => array(
  130. 'label' => __( 'Phone', 'woocommerce' ),
  131. ),
  132. )
  133. );
  134. }
  135. /**
  136. * Output the metabox.
  137. *
  138. * @param WP_Post $post
  139. */
  140. public static function output( $post ) {
  141. global $theorder;
  142. if ( ! is_object( $theorder ) ) {
  143. $theorder = wc_get_order( $post->ID );
  144. }
  145. $order = $theorder;
  146. self::init_address_fields();
  147. if ( WC()->payment_gateways() ) {
  148. $payment_gateways = WC()->payment_gateways->payment_gateways();
  149. } else {
  150. $payment_gateways = array();
  151. }
  152. $payment_method = $order->get_payment_method();
  153. $order_type_object = get_post_type_object( $post->post_type );
  154. wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
  155. ?>
  156. <style type="text/css">
  157. #post-body-content, #titlediv { display:none }
  158. </style>
  159. <div class="panel-wrap woocommerce">
  160. <input name="post_title" type="hidden" value="<?php echo empty( $post->post_title ) ? __( 'Order', 'woocommerce' ) : esc_attr( $post->post_title ); ?>" />
  161. <input name="post_status" type="hidden" value="<?php echo esc_attr( $post->post_status ); ?>" />
  162. <div id="order_data" class="panel woocommerce-order-data">
  163. <h2 class="woocommerce-order-data__heading">
  164. <?php
  165. /* translators: 1: order type 2: order number */
  166. printf(
  167. esc_html__( '%1$s #%2$s details', 'woocommerce' ),
  168. esc_html( $order_type_object->labels->singular_name ),
  169. esc_html( $order->get_order_number() )
  170. );
  171. ?>
  172. </h2>
  173. <p class="woocommerce-order-data__meta order_number">
  174. <?php
  175. $meta_list = array();
  176. if ( $payment_method && 'other' !== $payment_method ) {
  177. /* translators: %s: payment method */
  178. $payment_method_string = sprintf(
  179. __( 'Payment via %s', 'woocommerce' ),
  180. esc_html( isset( $payment_gateways[ $payment_method ] ) ? $payment_gateways[ $payment_method ]->get_title() : $payment_method )
  181. );
  182. if ( $transaction_id = $order->get_transaction_id() ) {
  183. if ( isset( $payment_gateways[ $payment_method ] ) && ( $url = $payment_gateways[ $payment_method ]->get_transaction_url( $order ) ) ) {
  184. $payment_method_string .= ' (<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $transaction_id ) . '</a>)';
  185. } else {
  186. $payment_method_string .= ' (' . esc_html( $transaction_id ) . ')';
  187. }
  188. }
  189. $meta_list[] = $payment_method_string;
  190. }
  191. if ( $order->get_date_paid() ) {
  192. /* translators: 1: date 2: time */
  193. $meta_list[] = sprintf(
  194. __( 'Paid on %1$s @ %2$s', 'woocommerce' ),
  195. wc_format_datetime( $order->get_date_paid() ),
  196. wc_format_datetime( $order->get_date_paid(), get_option( 'time_format' ) )
  197. );
  198. }
  199. if ( $ip_address = $order->get_customer_ip_address() ) {
  200. /* translators: %s: IP address */
  201. $meta_list[] = sprintf(
  202. __( 'Customer IP: %s', 'woocommerce' ),
  203. '<span class="woocommerce-Order-customerIP">' . esc_html( $ip_address ) . '</span>'
  204. );
  205. }
  206. echo wp_kses_post( implode( '. ', $meta_list ) );
  207. ?>
  208. </p>
  209. <div class="order_data_column_container">
  210. <div class="order_data_column">
  211. <h3><?php esc_html_e( 'General', 'woocommerce' ); ?></h3>
  212. <p class="form-field form-field-wide">
  213. <label for="order_date"><?php _e( 'Date created:', 'woocommerce' ); ?></label>
  214. <input type="text" class="date-picker" name="order_date" maxlength="10" value="<?php echo esc_attr( date_i18n( 'Y-m-d', strtotime( $post->post_date ) ) ); ?>" pattern="<?php echo esc_attr( apply_filters( 'woocommerce_date_input_html_pattern', '[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])' ) ); ?>" />@
  215. &lrm;
  216. <input type="number" class="hour" placeholder="<?php esc_attr_e( 'h', 'woocommerce' ); ?>" name="order_date_hour" min="0" max="23" step="1" value="<?php echo esc_attr( date_i18n( 'H', strtotime( $post->post_date ) ) ); ?>" pattern="([01]?[0-9]{1}|2[0-3]{1})" />:
  217. <input type="number" class="minute" placeholder="<?php esc_attr_e( 'm', 'woocommerce' ); ?>" name="order_date_minute" min="0" max="59" step="1" value="<?php echo esc_attr( date_i18n( 'i', strtotime( $post->post_date ) ) ); ?>" pattern="[0-5]{1}[0-9]{1}" />
  218. <input type="hidden" name="order_date_second" value="<?php echo esc_attr( date_i18n( 's', strtotime( $post->post_date ) ) ); ?>" />
  219. </p>
  220. <p class="form-field form-field-wide wc-order-status">
  221. <label for="order_status">
  222. <?php
  223. _e( 'Status:', 'woocommerce' );
  224. if ( $order->needs_payment() ) {
  225. printf(
  226. '<a href="%s">%s</a>',
  227. esc_url( $order->get_checkout_payment_url() ),
  228. __( 'Customer payment page &rarr;', 'woocommerce' )
  229. );
  230. }
  231. ?>
  232. </label>
  233. <select id="order_status" name="order_status" class="wc-enhanced-select">
  234. <?php
  235. $statuses = wc_get_order_statuses();
  236. foreach ( $statuses as $status => $status_name ) {
  237. echo '<option value="' . esc_attr( $status ) . '" ' . selected( $status, 'wc-' . $order->get_status( 'edit' ), false ) . '>' . esc_html( $status_name ) . '</option>';
  238. }
  239. ?>
  240. </select>
  241. </p>
  242. <p class="form-field form-field-wide wc-customer-user">
  243. <!--email_off--> <!-- Disable CloudFlare email obfuscation -->
  244. <label for="customer_user">
  245. <?php
  246. _e( 'Customer:', 'woocommerce' );
  247. if ( $order->get_user_id( 'edit' ) ) {
  248. $args = array(
  249. 'post_status' => 'all',
  250. 'post_type' => 'shop_order',
  251. '_customer_user' => $order->get_user_id( 'edit' ),
  252. );
  253. printf(
  254. '<a href="%s">%s</a>',
  255. esc_url( add_query_arg( $args, admin_url( 'edit.php' ) ) ),
  256. ' ' . __( 'View other orders &rarr;', 'woocommerce' )
  257. );
  258. printf(
  259. '<a href="%s">%s</a>',
  260. esc_url( add_query_arg( 'user_id', $order->get_user_id( 'edit' ), admin_url( 'user-edit.php' ) ) ),
  261. ' ' . __( 'Profile &rarr;', 'woocommerce' )
  262. );
  263. }
  264. ?>
  265. </label>
  266. <?php
  267. $user_string = '';
  268. $user_id = '';
  269. if ( $order->get_user_id() ) {
  270. $user_id = absint( $order->get_user_id() );
  271. $user = get_user_by( 'id', $user_id );
  272. /* translators: 1: user display name 2: user ID 3: user email */
  273. $user_string = sprintf(
  274. esc_html__( '%1$s (#%2$s &ndash; %3$s)', 'woocommerce' ),
  275. $user->display_name,
  276. absint( $user->ID ),
  277. $user->user_email
  278. );
  279. }
  280. ?>
  281. <select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
  282. <option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( wp_kses_post( $user_string ) ); // htmlspecialchars to prevent XSS when rendered by selectWoo. ?></option>
  283. </select>
  284. <!--/email_off-->
  285. </p>
  286. <?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>
  287. </div>
  288. <div class="order_data_column">
  289. <h3>
  290. <?php esc_html_e( 'Billing', 'woocommerce' ); ?>
  291. <a href="#" class="edit_address"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
  292. <span>
  293. <a href="#" class="load_customer_billing" style="display:none;"><?php esc_html_e( 'Load billing address', 'woocommerce' ); ?></a>
  294. </span>
  295. </h3>
  296. <div class="address">
  297. <?php
  298. // Display values.
  299. if ( $order->get_formatted_billing_address() ) {
  300. echo '<p>' . wp_kses( $order->get_formatted_billing_address(), array( 'br' => array() ) ) . '</p>';
  301. } else {
  302. echo '<p class="none_set"><strong>' . __( 'Address:', 'woocommerce' ) . '</strong> ' . __( 'No billing address set.', 'woocommerce' ) . '</p>';
  303. }
  304. foreach ( self::$billing_fields as $key => $field ) {
  305. if ( isset( $field['show'] ) && false === $field['show'] ) {
  306. continue;
  307. }
  308. $field_name = 'billing_' . $key;
  309. if ( isset( $field['value'] ) ) {
  310. $field_value = $field['value'];
  311. } elseif ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
  312. $field_value = $order->{"get_$field_name"}( 'edit' );
  313. } else {
  314. $field_value = $order->get_meta( '_' . $field_name );
  315. }
  316. if ( 'billing_phone' === $field_name ) {
  317. $field_value = wc_make_phone_clickable( $field_value );
  318. } elseif ( 'billing_email' === $field_name ) {
  319. $field_value = '<a href="' . esc_url( 'mailto:' . $field_value ) . '">' . $field_value . '</a>';
  320. } else {
  321. $field_value = make_clickable( esc_html( $field_value ) );
  322. }
  323. if ( $field_value ) {
  324. echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . wp_kses_post( $field_value ) . '</p>';
  325. }
  326. }
  327. ?>
  328. </div>
  329. <div class="edit_address">
  330. <?php
  331. // Display form.
  332. foreach ( self::$billing_fields as $key => $field ) {
  333. if ( ! isset( $field['type'] ) ) {
  334. $field['type'] = 'text';
  335. }
  336. if ( ! isset( $field['id'] ) ) {
  337. $field['id'] = '_billing_' . $key;
  338. }
  339. $field_name = 'billing_' . $key;
  340. if ( ! isset( $field['value'] ) ) {
  341. if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
  342. $field['value'] = $order->{"get_$field_name"}( 'edit' );
  343. } else {
  344. $field['value'] = $order->get_meta( '_' . $field_name );
  345. }
  346. }
  347. switch ( $field['type'] ) {
  348. case 'select':
  349. woocommerce_wp_select( $field );
  350. break;
  351. default:
  352. woocommerce_wp_text_input( $field );
  353. break;
  354. }
  355. }
  356. ?>
  357. <p class="form-field form-field-wide">
  358. <label><?php esc_html_e( 'Payment method:', 'woocommerce' ); ?></label>
  359. <select name="_payment_method" id="_payment_method" class="first">
  360. <option value=""><?php esc_html_e( 'N/A', 'woocommerce' ); ?></option>
  361. <?php
  362. $found_method = false;
  363. foreach ( $payment_gateways as $gateway ) {
  364. if ( 'yes' === $gateway->enabled ) {
  365. echo '<option value="' . esc_attr( $gateway->id ) . '" ' . selected( $payment_method, $gateway->id, false ) . '>' . esc_html( $gateway->get_title() ) . '</option>';
  366. if ( $payment_method == $gateway->id ) {
  367. $found_method = true;
  368. }
  369. }
  370. }
  371. if ( ! $found_method && ! empty( $payment_method ) ) {
  372. echo '<option value="' . esc_attr( $payment_method ) . '" selected="selected">' . esc_html__( 'Other', 'woocommerce' ) . '</option>';
  373. } else {
  374. echo '<option value="other">' . esc_html__( 'Other', 'woocommerce' ) . '</option>';
  375. }
  376. ?>
  377. </select>
  378. </p>
  379. <?php
  380. woocommerce_wp_text_input(
  381. array(
  382. 'id' => '_transaction_id',
  383. 'label' => __( 'Transaction ID', 'woocommerce' ),
  384. 'value' => $order->get_transaction_id( 'edit' ),
  385. )
  386. );
  387. ?>
  388. </div>
  389. <?php do_action( 'woocommerce_admin_order_data_after_billing_address', $order ); ?>
  390. </div>
  391. <div class="order_data_column">
  392. <h3>
  393. <?php esc_html_e( 'Shipping', 'woocommerce' ); ?>
  394. <a href="#" class="edit_address"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
  395. <span>
  396. <a href="#" class="load_customer_shipping" style="display:none;"><?php esc_html_e( 'Load shipping address', 'woocommerce' ); ?></a>
  397. <a href="#" class="billing-same-as-shipping" style="display:none;"><?php esc_html_e( 'Copy billing address', 'woocommerce' ); ?></a>
  398. </span>
  399. </h3>
  400. <div class="address">
  401. <?php
  402. // Display values.
  403. if ( $order->get_formatted_shipping_address() ) {
  404. echo '<p>' . wp_kses( $order->get_formatted_shipping_address(), array( 'br' => array() ) ) . '</p>';
  405. } else {
  406. echo '<p class="none_set"><strong>' . __( 'Address:', 'woocommerce' ) . '</strong> ' . __( 'No shipping address set.', 'woocommerce' ) . '</p>';
  407. }
  408. if ( ! empty( self::$shipping_fields ) ) {
  409. foreach ( self::$shipping_fields as $key => $field ) {
  410. if ( isset( $field['show'] ) && false === $field['show'] ) {
  411. continue;
  412. }
  413. $field_name = 'shipping_' . $key;
  414. if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
  415. $field_value = $order->{"get_$field_name"}( 'edit' );
  416. } else {
  417. $field_value = $order->get_meta( '_' . $field_name );
  418. }
  419. if ( 'shipping_phone' === $field_name ) {
  420. $field_value = wc_make_phone_clickable( $field_value );
  421. }
  422. if ( $field_value ) {
  423. echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . wp_kses_post( $field_value ) . '</p>';
  424. }
  425. }
  426. }
  427. if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) && $post->post_excerpt ) {
  428. echo '<p class="order_note"><strong>' . __( 'Customer provided note:', 'woocommerce' ) . '</strong> ' . nl2br( esc_html( $post->post_excerpt ) ) . '</p>';
  429. }
  430. ?>
  431. </div>
  432. <div class="edit_address">
  433. <?php
  434. // Display form.
  435. if ( ! empty( self::$shipping_fields ) ) {
  436. foreach ( self::$shipping_fields as $key => $field ) {
  437. if ( ! isset( $field['type'] ) ) {
  438. $field['type'] = 'text';
  439. }
  440. if ( ! isset( $field['id'] ) ) {
  441. $field['id'] = '_shipping_' . $key;
  442. }
  443. $field_name = 'shipping_' . $key;
  444. if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
  445. $field['value'] = $order->{"get_$field_name"}( 'edit' );
  446. } else {
  447. $field['value'] = $order->get_meta( '_' . $field_name );
  448. }
  449. switch ( $field['type'] ) {
  450. case 'select':
  451. woocommerce_wp_select( $field );
  452. break;
  453. default:
  454. woocommerce_wp_text_input( $field );
  455. break;
  456. }
  457. }
  458. }
  459. if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) ) :
  460. ?>
  461. <p class="form-field form-field-wide">
  462. <label for="excerpt"><?php _e( 'Customer provided note', 'woocommerce' ); ?>:</label>
  463. <textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt" placeholder="<?php esc_attr_e( 'Customer notes about the order', 'woocommerce' ); ?>"><?php echo wp_kses_post( $post->post_excerpt ); ?></textarea>
  464. </p>
  465. <?php endif; ?>
  466. </div>
  467. <?php do_action( 'woocommerce_admin_order_data_after_shipping_address', $order ); ?>
  468. </div>
  469. </div>
  470. <div class="clear"></div>
  471. </div>
  472. </div>
  473. <?php
  474. }
  475. /**
  476. * Save meta box data.
  477. *
  478. * @param int $order_id Order ID.
  479. */
  480. public static function save( $order_id ) {
  481. self::init_address_fields();
  482. // Ensure gateways are loaded in case they need to insert data into the emails.
  483. WC()->payment_gateways();
  484. WC()->shipping();
  485. // Get order object.
  486. $order = wc_get_order( $order_id );
  487. $props = array();
  488. // Create order key.
  489. if ( ! $order->get_order_key() ) {
  490. $props['order_key'] = wc_generate_order_key();
  491. }
  492. // Update customer.
  493. $customer_id = isset( $_POST['customer_user'] ) ? absint( $_POST['customer_user'] ) : 0;
  494. if ( $customer_id !== $order->get_customer_id() ) {
  495. $props['customer_id'] = $customer_id;
  496. }
  497. // Update billing fields.
  498. if ( ! empty( self::$billing_fields ) ) {
  499. foreach ( self::$billing_fields as $key => $field ) {
  500. if ( ! isset( $field['id'] ) ) {
  501. $field['id'] = '_billing_' . $key;
  502. }
  503. if ( ! isset( $_POST[ $field['id'] ] ) ) {
  504. continue;
  505. }
  506. if ( is_callable( array( $order, 'set_billing_' . $key ) ) ) {
  507. $props[ 'billing_' . $key ] = wc_clean( wp_unslash( $_POST[ $field['id'] ] ) );
  508. } else {
  509. $order->update_meta_data( $field['id'], wc_clean( wp_unslash( $_POST[ $field['id'] ] ) ) );
  510. }
  511. }
  512. }
  513. // Update shipping fields.
  514. if ( ! empty( self::$shipping_fields ) ) {
  515. foreach ( self::$shipping_fields as $key => $field ) {
  516. if ( ! isset( $field['id'] ) ) {
  517. $field['id'] = '_shipping_' . $key;
  518. }
  519. if ( ! isset( $_POST[ $field['id'] ] ) ) {
  520. continue;
  521. }
  522. if ( is_callable( array( $order, 'set_shipping_' . $key ) ) ) {
  523. $props[ 'shipping_' . $key ] = wc_clean( wp_unslash( $_POST[ $field['id'] ] ) );
  524. } else {
  525. $order->update_meta_data( $field['id'], wc_clean( wp_unslash( $_POST[ $field['id'] ] ) ) );
  526. }
  527. }
  528. }
  529. if ( isset( $_POST['_transaction_id'] ) ) {
  530. $props['transaction_id'] = wc_clean( wp_unslash( $_POST['_transaction_id'] ) );
  531. }
  532. // Payment method handling.
  533. if ( $order->get_payment_method() !== wp_unslash( $_POST['_payment_method'] ) ) {
  534. $methods = WC()->payment_gateways->payment_gateways();
  535. $payment_method = wc_clean( wp_unslash( $_POST['_payment_method'] ) );
  536. $payment_method_title = $payment_method;
  537. if ( isset( $methods ) && isset( $methods[ $payment_method ] ) ) {
  538. $payment_method_title = $methods[ $payment_method ]->get_title();
  539. }
  540. if ( $payment_method == 'other') {
  541. $payment_method_title = esc_html__( 'Other', 'woocommerce' );
  542. }
  543. $props['payment_method'] = $payment_method;
  544. $props['payment_method_title'] = $payment_method_title;
  545. }
  546. // Update date.
  547. if ( empty( $_POST['order_date'] ) ) {
  548. $date = time();
  549. } else {
  550. $date = gmdate( 'Y-m-d H:i:s', strtotime( $_POST['order_date'] . ' ' . (int) $_POST['order_date_hour'] . ':' . (int) $_POST['order_date_minute'] . ':' . (int) $_POST['order_date_second'] ) );
  551. }
  552. $props['date_created'] = $date;
  553. // Set created via prop if new post.
  554. if ( isset( $_POST['original_post_status'] ) && $_POST['original_post_status'] === 'auto-draft' ) {
  555. $props['created_via'] = 'admin';
  556. }
  557. // Save order data.
  558. $order->set_props( $props );
  559. $order->set_status( wc_clean( wp_unslash( $_POST['order_status'] ) ), '', true );
  560. $order->save();
  561. }
  562. }