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.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 /**
 16  * WC_Meta_Box_Order_Data
 17  */
 18 class WC_Meta_Box_Order_Data {
 19 
 20     private static $billing_fields;
 21     private static $shipping_fields;
 22 
 23     /**
 24      * Init billing and shipping fields we display + save
 25      */
 26     public static function init_address_fields() {
 27         self::$billing_fields = apply_filters( 'woocommerce_admin_billing_fields', array(
 28             'first_name' => array(
 29                 'label' => __( 'First Name', 'woocommerce' ),
 30                 'show'  => false
 31                 ),
 32             'last_name' => array(
 33                 'label' => __( 'Last Name', 'woocommerce' ),
 34                 'show'  => false
 35                 ),
 36             'company' => array(
 37                 'label' => __( 'Company', 'woocommerce' ),
 38                 'show'  => false
 39                 ),
 40             'address_1' => array(
 41                 'label' => __( 'Address 1', 'woocommerce' ),
 42                 'show'  => false
 43                 ),
 44             'address_2' => array(
 45                 'label' => __( 'Address 2', 'woocommerce' ),
 46                 'show'  => false
 47                 ),
 48             'city' => array(
 49                 'label' => __( 'City', 'woocommerce' ),
 50                 'show'  => false
 51                 ),
 52             'postcode' => array(
 53                 'label' => __( 'Postcode', 'woocommerce' ),
 54                 'show'  => false
 55                 ),
 56             'country' => array(
 57                 'label' => __( 'Country', 'woocommerce' ),
 58                 'show'  => false,
 59                 'type'  => 'select',
 60                 'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries()
 61                 ),
 62             'state' => array(
 63                 'label' => __( 'State/County', 'woocommerce' ),
 64                 'show'  => false
 65                 ),
 66             'email' => array(
 67                 'label' => __( 'Email', 'woocommerce' ),
 68                 ),
 69             'phone' => array(
 70                 'label' => __( 'Phone', 'woocommerce' ),
 71                 ),
 72         ) );
 73 
 74         self::$shipping_fields = apply_filters( 'woocommerce_admin_shipping_fields', array(
 75             'first_name' => array(
 76                 'label' => __( 'First Name', 'woocommerce' ),
 77                 'show'  => false
 78                 ),
 79             'last_name' => array(
 80                 'label' => __( 'Last Name', 'woocommerce' ),
 81                 'show'  => false
 82                 ),
 83             'company' => array(
 84                 'label' => __( 'Company', 'woocommerce' ),
 85                 'show'  => false
 86                 ),
 87             'address_1' => array(
 88                 'label' => __( 'Address 1', 'woocommerce' ),
 89                 'show'  => false
 90                 ),
 91             'address_2' => array(
 92                 'label' => __( 'Address 2', 'woocommerce' ),
 93                 'show'  => false
 94                 ),
 95             'city' => array(
 96                 'label' => __( 'City', 'woocommerce' ),
 97                 'show'  => false
 98                 ),
 99             'postcode' => array(
100                 'label' => __( 'Postcode', 'woocommerce' ),
101                 'show'  => false
102                 ),
103             'country' => array(
104                 'label' => __( 'Country', 'woocommerce' ),
105                 'show'  => false,
106                 'type'  => 'select',
107                 'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_shipping_countries()
108                 ),
109             'state' => array(
110                 'label' => __( 'State/County', 'woocommerce' ),
111                 'show'  => false
112                 ),
113         ) );
114     }
115 
116     /**
117      * Output the metabox
118      */
119     public static function output( $post ) {
120         global $theorder;
121 
122         if ( ! is_object( $theorder ) )
123             $theorder = new WC_Order( $post->ID );
124 
125         $order = $theorder;
126 
127         self::init_address_fields();
128 
129         wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
130         ?>
131         <style type="text/css">
132             #post-body-content, #titlediv, #major-publishing-actions, #minor-publishing-actions, #visibility, #submitdiv { display:none }
133         </style>
134         <div class="panel-wrap woocommerce">
135             <input name="post_title" type="hidden" value="<?php echo empty( $post->post_title ) ? 'Order' : esc_attr( $post->post_title ); ?>" />
136             <input name="post_status" type="hidden" value="publish" />
137             <div id="order_data" class="panel">
138 
139                 <h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2>
140                 <p class="order_number"><?php
141 
142                     echo __( 'Order number', 'woocommerce' ) . ' ' . esc_html( $order->get_order_number() ) . '. ';
143 
144                     if ( $ip_address = get_post_meta( $post->ID, '_customer_ip_address', true ) )
145                         echo __( 'Customer IP:', 'woocommerce' ) . ' ' . esc_html( $ip_address );
146                 ?></p>
147 
148                 <div class="order_data_column_container">
149                     <div class="order_data_column">
150                         <h4><?php _e( 'General Details', 'woocommerce' ); ?></h4>
151 
152                         <p class="form-field form-field-wide"><label for="order_date"><?php _e( 'Order date:', 'woocommerce' ) ?></label>
153                             <input type="text" class="date-picker-field" name="order_date" id="order_date" maxlength="10" value="<?php echo date_i18n( 'Y-m-d', strtotime( $post->post_date ) ); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />@<input type="text" class="hour" placeholder="<?php _e( 'h', 'woocommerce' ) ?>" name="order_date_hour" id="order_date_hour" maxlength="2" size="2" value="<?php echo date_i18n( 'H', strtotime( $post->post_date ) ); ?>" pattern="\-?\d+(\.\d{0,})?" />:<input type="text" class="minute" placeholder="<?php _e( 'm', 'woocommerce' ) ?>" name="order_date_minute" id="order_date_minute" maxlength="2" size="2" value="<?php echo date_i18n( 'i', strtotime( $post->post_date ) ); ?>" pattern="\-?\d+(\.\d{0,})?" />
154                         </p>
155 
156                         <p class="form-field form-field-wide"><label for="order_status"><?php _e( 'Order status:', 'woocommerce' ) ?></label>
157                         <select id="order_status" name="order_status" class="chosen_select">
158                             <?php
159                                 $statuses = (array) get_terms( 'shop_order_status', array( 'hide_empty' => 0, 'orderby' => 'id' ) );
160                                 foreach ( $statuses as $status ) {
161                                     echo '<option value="' . esc_attr( $status->slug ) . '" ' . selected( $status->slug, $order->status, false ) . '>' . esc_html__( $status->name, 'woocommerce' ) . '</option>';
162                                 }
163                             ?>
164                         </select></p>
165 
166                         <p class="form-field form-field-wide">
167                             <label for="customer_user"><?php _e( 'Customer:', 'woocommerce' ) ?></label>
168                             <select id="customer_user" name="customer_user" class="ajax_chosen_select_customer">
169                                 <option value=""><?php _e( 'Guest', 'woocommerce' ) ?></option>
170                                 <?php
171                                     if ( $order->customer_user ) {
172                                         $user = get_user_by( 'id', $order->customer_user );
173                                         echo '<option value="' . esc_attr( $user->ID ) . '" ' . selected( 1, 1, false ) . '>' . esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' &ndash; ' . esc_html( $user->user_email ) . ')</option>';
174                                     }
175                                 ?>
176                             </select>
177                         </p>
178 
179                         <?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>
180                     </div>
181                     <div class="order_data_column">
182                         <h4><?php _e( 'Billing Details', 'woocommerce' ); ?> <a class="edit_address" href="#"><img src="<?php echo WC()->plugin_url(); ?>/assets/images/icons/edit.png" alt="Edit" width="14" /></a></h4>
183                         <?php
184                             // Display values
185                             echo '<div class="address">';
186 
187                                 if ( $order->get_formatted_billing_address() )
188                                     echo '<p><strong>' . __( 'Address', 'woocommerce' ) . ':</strong>' . wp_kses( $order->get_formatted_billing_address(), array( 'br' => array() ) ) . '</p>';
189                                 else
190                                     echo '<p class="none_set"><strong>' . __( 'Address', 'woocommerce' ) . ':</strong> ' . __( 'No billing address set.', 'woocommerce' ) . '</p>';
191 
192                                 foreach ( self::$billing_fields as $key => $field ) {
193                                     if ( isset( $field['show'] ) && $field['show'] === false )
194                                         continue;
195 
196                                     $field_name = 'billing_' . $key;
197 
198                                     if ( $order->$field_name )
199                                         echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $order->$field_name ) ) . '</p>';
200                                 }
201 
202                                 if ( WC()->payment_gateways() )
203                                     $payment_gateways = WC()->payment_gateways->payment_gateways();
204 
205                                 $payment_method = ! empty( $order->payment_method ) ? $order->payment_method : '';
206 
207                                 if ( $payment_method )
208                                     echo '<p><strong>' . __( 'Payment Method', 'woocommerce' ) . ':</strong> ' . ( isset( $payment_gateways[ $payment_method ] ) ? esc_html( $payment_gateways[ $payment_method ]->get_title() ) : esc_html( $payment_method ) ) . '</p>';
209 
210                             echo '</div>';
211 
212                             // Display form
213                             echo '<div class="edit_address"><p><button class="button load_customer_billing">'.__( 'Load billing address', 'woocommerce' ).'</button></p>';
214 
215                             foreach ( self::$billing_fields as $key => $field ) {
216                                 if ( ! isset( $field['type'] ) )
217                                     $field['type'] = 'text';
218                                 switch ( $field['type'] ) {
219                                     case "select" :
220                                         woocommerce_wp_select( array( 'id' => '_billing_' . $key, 'label' => $field['label'], 'options' => $field['options'] ) );
221                                     break;
222                                     default :
223                                         woocommerce_wp_text_input( array( 'id' => '_billing_' . $key, 'label' => $field['label'] ) );
224                                     break;
225                                 }
226                             }
227 
228                             ?>
229                             <p class="form-field form-field-wide">
230                                 <label><?php _e( 'Payment Method:', 'woocommerce' ); ?></label>
231                                 <select name="_payment_method" id="_payment_method" class="first">
232                                     <option value=""><?php _e( 'N/A', 'woocommerce' ); ?></option>
233                                     <?php
234                                         $found_method   = false;
235 
236                                         foreach ( $payment_gateways as $gateway ) {
237                                             if ( $gateway->enabled == "yes" ) {
238                                                 echo '<option value="' . esc_attr( $gateway->id ) . '" ' . selected( $payment_method, $gateway->id, false ) . '>' . esc_html( $gateway->get_title() ) . '</option>';
239                                                 if ( $payment_method == $gateway->id )
240                                                     $found_method = true;
241                                             }
242                                         }
243 
244                                         if ( ! $found_method && ! empty( $payment_method ) ) {
245                                             echo '<option value="' . esc_attr( $payment_method ) . '" selected="selected">' . __( 'Other', 'woocommerce' ) . '</option>';
246                                         } else {
247                                             echo '<option value="other">' . __( 'Other', 'woocommerce' ) . '</option>';
248                                         }
249                                     ?>
250                                 </select>
251                             </p>
252                             <?php
253 
254                             echo '</div>';
255 
256                             do_action( 'woocommerce_admin_order_data_after_billing_address', $order );
257                         ?>
258                     </div>
259                     <div class="order_data_column">
260 
261                         <h4><?php _e( 'Shipping Details', 'woocommerce' ); ?> <a class="edit_address" href="#"><img src="<?php echo WC()->plugin_url(); ?>/assets/images/icons/edit.png" alt="Edit" width="14" /></a></h4>
262                         <?php
263                             // Display values
264                             echo '<div class="address">';
265 
266                                 if ( $order->get_formatted_shipping_address() )
267                                     echo '<p><strong>' . __( 'Address', 'woocommerce' ) . ':</strong>' . wp_kses( $order->get_formatted_shipping_address(), array( 'br' => array() ) ) . '</p>';
268                                 else
269                                     echo '<p class="none_set"><strong>' . __( 'Address', 'woocommerce' ) . ':</strong> ' . __( 'No shipping address set.', 'woocommerce' ) . '</p>';
270 
271                                 if ( self::$shipping_fields ) foreach ( self::$shipping_fields as $key => $field ) {
272                                     if ( isset( $field['show'] ) && $field['show'] === false )
273                                         continue;
274 
275                                     $field_name = 'shipping_' . $key;
276 
277                                     if ( ! empty( $order->$field_name ) )
278                                         echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $order->$field_name ) ) . '</p>';
279                                 }
280 
281                                 if ( apply_filters( 'woocommerce_enable_order_notes_field', get_option( 'woocommerce_enable_order_comments', 'yes' ) == 'yes' ) && $post->post_excerpt )
282                                     echo '<p><strong>' . __( 'Customer Note', 'woocommerce' ) . ':</strong> ' . nl2br( esc_html( $post->post_excerpt ) ) . '</p>';
283 
284                             echo '</div>';
285 
286                             // Display form
287                             echo '<div class="edit_address"><p><button class="button load_customer_shipping">' . __( 'Load shipping address', 'woocommerce' ) . '</button> <button class="button billing-same-as-shipping">'. __( 'Copy from billing', 'woocommerce' ) . '</button></p>';
288 
289                             if ( self::$shipping_fields ) foreach ( self::$shipping_fields as $key => $field ) {
290                                 if ( ! isset( $field['type'] ) )
291                                     $field['type'] = 'text';
292                                 switch ( $field['type'] ) {
293                                     case "select" :
294                                         woocommerce_wp_select( array( 'id' => '_shipping_' . $key, 'label' => $field['label'], 'options' => $field['options'] ) );
295                                     break;
296                                     default :
297                                         woocommerce_wp_text_input( array( 'id' => '_shipping_' . $key, 'label' => $field['label'] ) );
298                                     break;
299                                 }
300                             }
301 
302                             if ( apply_filters( 'woocommerce_enable_order_notes_field', get_option( 'woocommerce_enable_order_comments', 'yes' ) == 'yes' ) ) {
303                                 ?>
304                                 <p class="form-field form-field-wide"><label for="excerpt"><?php _e( 'Customer Note:', 'woocommerce' ) ?></label>
305                                 <textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt" placeholder="<?php _e( 'Customer\'s notes about the order', 'woocommerce' ); ?>"><?php echo wp_kses_post( $post->post_excerpt ); ?></textarea></p>
306                                 <?php
307                             }
308 
309                             echo '</div>';
310 
311                             do_action( 'woocommerce_admin_order_data_after_shipping_address', $order );
312                         ?>
313                     </div>
314                 </div>
315                 <div class="clear"></div>
316             </div>
317         </div>
318         <?php
319 
320         // Ajax Chosen Customer Selectors JS
321         wc_enqueue_js( "
322             jQuery('select.ajax_chosen_select_customer').ajaxChosen({
323                 method:         'GET',
324                 url:            '" . admin_url('admin-ajax.php') . "',
325                 dataType:       'json',
326                 afterTypeDelay: 100,
327                 minTermLength:  1,
328                 data:       {
329                     action:     'woocommerce_json_search_customers',
330                     security:   '" . wp_create_nonce("search-customers") . "'
331                 }
332             }, function (data) {
333 
334                 var terms = {};
335 
336                 $.each(data, function (i, val) {
337                     terms[i] = val;
338                 });
339 
340                 return terms;
341             });
342         " );
343     }
344 
345     /**
346      * Save meta box data
347      */
348     public static function save( $post_id, $post ) {
349         global $wpdb;
350 
351         self::init_address_fields();
352 
353         // Add key
354         add_post_meta( $post_id, '_order_key', uniqid( 'order_' ), true );
355 
356         // Update meta
357         update_post_meta( $post_id, '_customer_user', absint( $_POST['customer_user'] ) );
358 
359         if ( self::$billing_fields )
360             foreach ( self::$billing_fields as $key => $field )
361                 update_post_meta( $post_id, '_billing_' . $key, wc_clean( $_POST[ '_billing_' . $key ] ) );
362 
363         if ( self::$shipping_fields )
364             foreach ( self::$shipping_fields as $key => $field )
365                 update_post_meta( $post_id, '_shipping_' . $key, wc_clean( $_POST[ '_shipping_' . $key ] ) );
366 
367         // Payment method handling
368         if ( get_post_meta( $post_id, '_payment_method', true ) !== stripslashes( $_POST['_payment_method'] ) ) {
369 
370             $methods                = WC()->payment_gateways->payment_gateways();
371             $payment_method         = wc_clean( $_POST['_payment_method'] );
372             $payment_method_title   = $payment_method;
373 
374             if ( isset( $methods) && isset( $methods[ $payment_method ] ) )
375                 $payment_method_title = $methods[ $payment_method ]->get_title();
376 
377             update_post_meta( $post_id, '_payment_method', $payment_method );
378             update_post_meta( $post_id, '_payment_method_title', $payment_method_title );
379         }
380 
381         // Update date
382         if ( empty( $_POST['order_date'] ) ) {
383             $date = current_time('timestamp');
384         } else {
385             $date = strtotime( $_POST['order_date'] . ' ' . (int) $_POST['order_date_hour'] . ':' . (int) $_POST['order_date_minute'] . ':00' );
386         }
387 
388         $date = date_i18n( 'Y-m-d H:i:s', $date );
389 
390         $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_date = %s, post_date_gmt = %s WHERE ID = %s", $date, get_gmt_from_date( $date ), $post_id ) );
391 
392         // Order data saved, now get it so we can manipulate status
393         $order = new WC_Order( $post_id );
394 
395         // Order status
396         $order->update_status( $_POST['order_status'] );
397 
398         wc_delete_shop_order_transients( $post_id );
399     }
400 }
WooCommerce API documentation generated by ApiGen 2.8.0