woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-dat...

543 lines
18 KiB
PHP
Raw Normal View History

2013-08-06 10:41:20 +00:00
<?php
/**
2015-11-03 13:53:50 +00:00
* Order Data
2013-08-06 10:41:20 +00:00
*
* Functions for displaying the order data meta box.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/Meta Boxes
* @version 2.2.0
2013-08-06 10:41:20 +00:00
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-08-06 10:41:20 +00:00
/**
* WC_Meta_Box_Order_Data Class.
2013-08-06 10:41:20 +00:00
*/
class WC_Meta_Box_Order_Data {
/**
* Billing fields.
*
* @var array
*/
protected static $billing_fields = array();
/**
* Shipping fields.
*
* @var array
*/
protected static $shipping_fields = array();
2013-08-06 10:41:20 +00:00
/**
* Init billing and shipping fields we display + save.
2013-08-06 10:41:20 +00:00
*/
public static function init_address_fields() {
2013-08-06 10:41:20 +00:00
self::$billing_fields = apply_filters( 'woocommerce_admin_billing_fields', array(
'first_name' => array(
'label' => __( 'First name', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'last_name' => array(
'label' => __( 'Last name', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'company' => array(
'label' => __( 'Company', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'address_1' => array(
'label' => __( 'Address line 1', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'address_2' => array(
'label' => __( 'Address line 2', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'city' => array(
'label' => __( 'City', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'country' => array(
'label' => __( 'Country', 'woocommerce' ),
'show' => false,
'class' => 'js_field-country select short',
'type' => 'select',
'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
),
2013-08-06 10:41:20 +00:00
'state' => array(
'label' => __( 'State / County', 'woocommerce' ),
'class' => 'js_field-state select short',
'show' => false,
),
2013-08-06 10:41:20 +00:00
'email' => array(
'label' => __( 'Email address', 'woocommerce' ),
),
2013-08-06 10:41:20 +00:00
'phone' => array(
'label' => __( 'Phone', 'woocommerce' ),
),
2013-08-06 10:41:20 +00:00
) );
self::$shipping_fields = apply_filters( 'woocommerce_admin_shipping_fields', array(
'first_name' => array(
'label' => __( 'First name', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'last_name' => array(
'label' => __( 'Last name', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'company' => array(
'label' => __( 'Company', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'address_1' => array(
'label' => __( 'Address line 1', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'address_2' => array(
'label' => __( 'Address line 2', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'city' => array(
'label' => __( 'City', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'show' => false,
),
2013-08-06 10:41:20 +00:00
'country' => array(
'label' => __( 'Country', 'woocommerce' ),
'show' => false,
'type' => 'select',
'class' => 'js_field-country select short',
'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_shipping_countries(),
),
2013-08-06 10:41:20 +00:00
'state' => array(
'label' => __( 'State / County', 'woocommerce' ),
'class' => 'js_field-state select short',
'show' => false,
),
2013-08-06 10:41:20 +00:00
) );
}
/**
* Output the metabox.
*
* @param WP_Post $post
2013-08-06 10:41:20 +00:00
*/
public static function output( $post ) {
global $theorder;
if ( ! is_object( $theorder ) ) {
$theorder = wc_get_order( $post->ID );
}
2013-08-06 10:41:20 +00:00
$order = $theorder;
self::init_address_fields();
if ( WC()->payment_gateways() ) {
$payment_gateways = WC()->payment_gateways->payment_gateways();
2015-01-14 12:17:19 +00:00
} else {
$payment_gateways = array();
}
2016-11-18 19:56:17 +00:00
$payment_method = $order->get_payment_method();
$order_type_object = get_post_type_object( $post->post_type );
2013-08-06 10:41:20 +00:00
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
?>
<style type="text/css">
#post-body-content, #titlediv { display:none }
2013-08-06 10:41:20 +00:00
</style>
<div class="panel-wrap woocommerce">
2015-08-07 13:27:40 +00:00
<input name="post_title" type="hidden" value="<?php echo empty( $post->post_title ) ? __( 'Order', 'woocommerce' ) : esc_attr( $post->post_title ); ?>" />
<input name="post_status" type="hidden" value="<?php echo esc_attr( $post->post_status ); ?>" />
2013-08-06 10:41:20 +00:00
<div id="order_data" class="panel">
<h2><?php
/* translators: 1: order type 2: order number */
printf(
esc_html__( '%1$s #%2$s details', 'woocommerce' ),
$order_type_object->labels->singular_name,
$order->get_order_number()
);
?></h2>
2013-08-06 10:41:20 +00:00
<p class="order_number"><?php
if ( $payment_method ) {
2016-10-29 10:16:03 +00:00
/* translators: %s: payment method */
printf(
__( 'Payment via %s', 'woocommerce' ),
( isset( $payment_gateways[ $payment_method ] ) ? esc_html( $payment_gateways[ $payment_method ]->get_title() ) : esc_html( $payment_method ) )
);
2014-07-07 10:44:35 +00:00
if ( $transaction_id = $order->get_transaction_id() ) {
if ( isset( $payment_gateways[ $payment_method ] ) && ( $url = $payment_gateways[ $payment_method ]->get_transaction_url( $order ) ) ) {
echo ' (<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $transaction_id ) . '</a>)';
} else {
echo ' (' . esc_html( $transaction_id ) . ')';
}
}
2016-08-05 15:14:30 +00:00
if ( $order->get_date_paid() ) {
/* translators: 1: date 2: time */
2017-03-10 16:43:05 +00:00
printf( ' ' . __( 'on %1$s @ %2$s', 'woocommerce' ), wc_format_datetime( $order->get_date_paid() ), wc_format_datetime( $order->get_date_paid(), get_option( 'time_format' ) ) );
}
echo '. ';
}
2013-08-06 10:41:20 +00:00
2017-03-10 16:43:05 +00:00
if ( $ip_address = $order->get_customer_ip_address() ) {
2016-10-29 10:16:03 +00:00
/* translators: %s: IP address */
printf(
__( 'Customer IP: %s', 'woocommerce' ),
'<span class="woocommerce-Order-customerIP">' . esc_html( $ip_address ) . '</span>'
);
}
2013-08-06 10:41:20 +00:00
?></p>
<div class="order_data_column_container">
<div class="order_data_column">
<h3><?php _e( 'General Details', 'woocommerce' ); ?></h3>
2013-08-06 10:41:20 +00:00
<p class="form-field form-field-wide"><label for="order_date"><?php _e( 'Order date:', 'woocommerce' ) ?></label>
2017-03-22 18:17:35 +00:00
<input type="text" class="date-picker" name="order_date" id="order_date" maxlength="10" value="<?php echo 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])' ) ); ?>" />@&lrm;<input type="number" class="hour" placeholder="<?php esc_attr_e( 'h', 'woocommerce' ) ?>" name="order_date_hour" id="order_date_hour" min="0" max="23" step="1" value="<?php echo date_i18n( 'H', strtotime( $post->post_date ) ); ?>" pattern="([01]?[0-9]{1}|2[0-3]{1})" />:<input type="number" class="minute" placeholder="<?php esc_attr_e( 'm', 'woocommerce' ) ?>" name="order_date_minute" id="order_date_minute" min="0" max="59" step="1" value="<?php echo date_i18n( 'i', strtotime( $post->post_date ) ); ?>" pattern="[0-5]{1}[0-9]{1}" />&lrm;
</p>
<p class="form-field form-field-wide wc-order-status"><label for="order_status"><?php _e( 'Order status:', 'woocommerce' ) ?> <?php
if ( $order->needs_payment() ) {
printf( '<a href="%s">%s</a>',
esc_url( $order->get_checkout_payment_url() ),
__( 'Customer payment page &rarr;', 'woocommerce' )
);
}
?></label>
2015-01-12 13:04:19 +00:00
<select id="order_status" name="order_status" class="wc-enhanced-select">
2013-08-06 10:41:20 +00:00
<?php
$statuses = wc_get_order_statuses();
foreach ( $statuses as $status => $status_name ) {
2016-11-18 19:56:17 +00:00
echo '<option value="' . esc_attr( $status ) . '" ' . selected( $status, 'wc-' . $order->get_status( 'edit' ), false ) . '>' . esc_html( $status_name ) . '</option>';
2013-08-06 10:41:20 +00:00
}
?>
</select></p>
<p class="form-field form-field-wide wc-customer-user">
<label for="customer_user"><?php _e( 'Customer:', 'woocommerce' ) ?> <?php
2016-11-18 19:56:17 +00:00
if ( $order->get_user_id( 'edit' ) ) {
$args = array(
'post_status' => 'all',
'post_type' => 'shop_order',
2016-11-18 19:56:17 +00:00
'_customer_user' => $order->get_user_id( 'edit' ),
);
printf( '<a href="%s">%s</a>',
esc_url( add_query_arg( $args, admin_url( 'edit.php' ) ) ),
__( 'View other orders &rarr;', 'woocommerce' )
);
}
?></label>
2015-01-12 13:04:19 +00:00
<?php
$user_string = '';
$user_id = '';
2016-08-15 16:50:02 +00:00
if ( $order->get_user_id() ) {
2016-08-05 15:09:40 +00:00
$user_id = absint( $order->get_user_id() );
2015-01-12 13:04:19 +00:00
$user = get_user_by( 'id', $user_id );
2016-10-29 10:16:03 +00:00
/* translators: 1: user display name 2: user ID 3: user email */
$user_string = sprintf(
esc_html__( '%1$s (#%2$s &ndash; %3$s)', 'woocommerce' ),
$user->display_name,
absint( $user->ID ),
$user->user_email
);
2015-01-12 13:04:19 +00:00
}
?>
2016-12-21 14:18:33 +00:00
<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
2017-02-06 09:05:55 +00:00
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
2016-12-21 14:18:33 +00:00
</select>
2013-08-06 10:41:20 +00:00
</p>
<?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>
2013-08-06 10:41:20 +00:00
</div>
<div class="order_data_column">
<h3>
<?php _e( 'Billing details', 'woocommerce' ); ?>
<a href="#" class="edit_address"><?php _e( 'Edit', 'woocommerce' ); ?></a>
<span>
<a href="#" class="load_customer_billing" style="display:none;"><?php _e( 'Load billing address', 'woocommerce' ); ?></a>
</span>
</h3>
2013-08-06 10:41:20 +00:00
<?php
// Display values
echo '<div class="address">';
if ( $order->get_formatted_billing_address() ) {
echo '<p><strong>' . __( 'Address:', 'woocommerce' ) . '</strong>' . wp_kses( $order->get_formatted_billing_address(), array( 'br' => array() ) ) . '</p>';
} else {
echo '<p class="none_set"><strong>' . __( 'Address:', 'woocommerce' ) . '</strong> ' . __( 'No billing address set.', 'woocommerce' ) . '</p>';
}
2013-08-06 10:41:20 +00:00
foreach ( self::$billing_fields as $key => $field ) {
if ( isset( $field['show'] ) && false === $field['show'] ) {
2013-08-06 10:41:20 +00:00
continue;
}
2013-08-06 10:41:20 +00:00
$field_name = 'billing_' . $key;
2016-08-08 13:49:53 +00:00
if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
$field_value = $order->{"get_$field_name"}( 'edit' );
} else {
$field_value = $order->get_meta( '_' . $field_name );
}
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $field_value ) ) . '</p>';
2013-08-06 10:41:20 +00:00
}
echo '</div>';
// Display form
echo '<div class="edit_address">';
2013-08-06 10:41:20 +00:00
foreach ( self::$billing_fields as $key => $field ) {
if ( ! isset( $field['type'] ) ) {
2013-08-06 10:41:20 +00:00
$field['type'] = 'text';
}
if ( ! isset( $field['id'] ) ) {
$field['id'] = '_billing_' . $key;
}
2013-08-06 10:41:20 +00:00
switch ( $field['type'] ) {
case 'select' :
woocommerce_wp_select( $field );
2013-08-06 10:41:20 +00:00
break;
default :
woocommerce_wp_text_input( $field );
2013-08-06 10:41:20 +00:00
break;
}
}
?>
<p class="form-field form-field-wide">
<label><?php _e( 'Payment method:', 'woocommerce' ); ?></label>
<select name="_payment_method" id="_payment_method" class="first">
<option value=""><?php _e( 'N/A', 'woocommerce' ); ?></option>
<?php
$found_method = false;
foreach ( $payment_gateways as $gateway ) {
2016-09-09 00:14:28 +00:00
if ( 'yes' === $gateway->enabled ) {
echo '<option value="' . esc_attr( $gateway->id ) . '" ' . selected( $payment_method, $gateway->id, false ) . '>' . esc_html( $gateway->get_title() ) . '</option>';
if ( $payment_method == $gateway->id ) {
$found_method = true;
}
}
}
if ( ! $found_method && ! empty( $payment_method ) ) {
echo '<option value="' . esc_attr( $payment_method ) . '" selected="selected">' . __( 'Other', 'woocommerce' ) . '</option>';
} else {
echo '<option value="other">' . __( 'Other', 'woocommerce' ) . '</option>';
}
?>
</select>
</p>
<?php
woocommerce_wp_text_input( array( 'id' => '_transaction_id', 'label' => __( 'Transaction ID', 'woocommerce' ) ) );
2013-08-06 10:41:20 +00:00
echo '</div>';
do_action( 'woocommerce_admin_order_data_after_billing_address', $order );
?>
</div>
<div class="order_data_column">
<h3>
<?php _e( 'Shipping details', 'woocommerce' ); ?>
<a href="#" class="edit_address"><?php _e( 'Edit', 'woocommerce' ); ?></a>
<span>
<a href="#" class="load_customer_shipping" style="display:none;"><?php _e( 'Load shipping address', 'woocommerce' ); ?></a>
<a href="#" class="billing-same-as-shipping" style="display:none;"><?php _e( 'Copy billing address', 'woocommerce' ); ?></a>
</span>
</h3>
2013-08-06 10:41:20 +00:00
<?php
// Display values
echo '<div class="address">';
if ( $order->get_formatted_shipping_address() ) {
echo '<p><strong>' . __( 'Address:', 'woocommerce' ) . '</strong>' . wp_kses( $order->get_formatted_shipping_address(), array( 'br' => array() ) ) . '</p>';
} else {
echo '<p class="none_set"><strong>' . __( 'Address:', 'woocommerce' ) . '</strong> ' . __( 'No shipping address set.', 'woocommerce' ) . '</p>';
}
2013-08-06 10:41:20 +00:00
2015-04-01 13:33:56 +00:00
if ( ! empty( self::$shipping_fields ) ) {
foreach ( self::$shipping_fields as $key => $field ) {
if ( isset( $field['show'] ) && false === $field['show'] ) {
continue;
}
2013-08-06 10:41:20 +00:00
$field_name = 'shipping_' . $key;
2013-08-06 10:41:20 +00:00
2016-08-08 13:49:53 +00:00
if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
$field_value = $order->{"get_$field_name"}( 'edit' );
} else {
$field_value = $order->get_meta( '_' . $field_name );
}
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $field_value ) ) . '</p>';
}
2013-08-06 10:41:20 +00:00
}
if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) && $post->post_excerpt ) {
echo '<p><strong>' . __( 'Customer provided note:', 'woocommerce' ) . '</strong> ' . nl2br( esc_html( $post->post_excerpt ) ) . '</p>';
}
2013-08-06 10:41:20 +00:00
echo '</div>';
// Display form
echo '<div class="edit_address">';
2013-08-06 10:41:20 +00:00
2015-04-01 13:33:56 +00:00
if ( ! empty( self::$shipping_fields ) ) {
foreach ( self::$shipping_fields as $key => $field ) {
if ( ! isset( $field['type'] ) ) {
$field['type'] = 'text';
}
if ( ! isset( $field['id'] ) ) {
$field['id'] = '_shipping_' . $key;
}
switch ( $field['type'] ) {
case 'select' :
woocommerce_wp_select( $field );
break;
default :
woocommerce_wp_text_input( $field );
break;
}
2013-08-06 10:41:20 +00:00
}
}
if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) ) {
?>
<p class="form-field form-field-wide"><label for="excerpt"><?php _e( 'Customer provided note', 'woocommerce' ) ?>:</label>
<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></p>
<?php
}
2013-08-06 10:41:20 +00:00
echo '</div>';
do_action( 'woocommerce_admin_order_data_after_shipping_address', $order );
?>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<?php
}
/**
* Save meta box data.
*
* @param int $order_id Order ID.
2013-08-06 10:41:20 +00:00
*/
public static function save( $order_id ) {
2013-08-06 10:41:20 +00:00
self::init_address_fields();
// Ensure gateways are loaded in case they need to insert data into the emails.
2016-03-03 17:05:40 +00:00
WC()->payment_gateways();
WC()->shipping();
// Get order object.
$order = wc_get_order( $order_id );
$props = array( 'status' => wc_clean( $_POST['order_status'] ) );
// Create order key.
if ( ! $order->get_order_key() ) {
$props['order_key'] = 'wc_' . apply_filters( 'woocommerce_generate_order_key', uniqid( 'order_' ) );
}
2013-08-06 10:41:20 +00:00
// Update customer.
$customer_id = isset( $_POST['customer_user'] ) ? absint( $_POST['customer_user'] ) : 0;
if ( $customer_id !== $order->get_customer_id() ) {
$props['customer_id'] = $customer_id;
}
2013-08-06 10:41:20 +00:00
// Update billing fields.
2015-04-01 13:33:56 +00:00
if ( ! empty( self::$billing_fields ) ) {
foreach ( self::$billing_fields as $key => $field ) {
if ( ! isset( $field['id'] ) ) {
$field['id'] = '_billing_' . $key;
}
if ( ! isset( $_POST[ $field['id'] ] ) ) {
continue;
}
if ( is_callable( array( $order, 'set_billing_' . $key ) ) ) {
$props[ 'billing_' . $key ] = wc_clean( $_POST[ $field['id'] ] );
} else {
$order->update_meta_data( $field['id'], wc_clean( $_POST[ $field['id'] ] ) );
}
}
}
2013-08-06 10:41:20 +00:00
// Update shipping fields.
2015-04-01 13:35:53 +00:00
if ( ! empty( self::$shipping_fields ) ) {
foreach ( self::$shipping_fields as $key => $field ) {
if ( ! isset( $field['id'] ) ) {
$field['id'] = '_shipping_' . $key;
}
if ( ! isset( $_POST[ $field['id'] ] ) ) {
continue;
}
if ( is_callable( array( $order, 'set_shipping_' . $key ) ) ) {
$props[ 'shipping_' . $key ] = wc_clean( $_POST[ $field['id'] ] );
} else {
$order->update_meta_data( $field['id'], wc_clean( $_POST[ $field['id'] ] ) );
}
}
}
2013-08-06 10:41:20 +00:00
if ( isset( $_POST['_transaction_id'] ) ) {
$props['transaction_id'] = wc_clean( $_POST['_transaction_id'] );
}
// Payment method handling.
if ( $order->get_payment_method() !== wp_unslash( $_POST['_payment_method'] ) ) {
$methods = WC()->payment_gateways->payment_gateways();
$payment_method = wc_clean( $_POST['_payment_method'] );
$payment_method_title = $payment_method;
if ( isset( $methods ) && isset( $methods[ $payment_method ] ) ) {
$payment_method_title = $methods[ $payment_method ]->get_title();
}
$props['payment_method'] = $payment_method;
$props['payment_method_title'] = $payment_method_title;
}
// Update date.
2013-08-06 10:41:20 +00:00
if ( empty( $_POST['order_date'] ) ) {
2017-03-10 18:54:59 +00:00
$date = current_time( 'timestamp', true );
2013-08-06 10:41:20 +00:00
} else {
2017-03-10 18:54:59 +00:00
$date = gmdate( 'Y-m-d H:i:s', strtotime( $_POST['order_date'] . ' ' . (int) $_POST['order_date_hour'] . ':' . (int) $_POST['order_date_minute'] . ':00' ) );
2013-08-06 10:41:20 +00:00
}
$props['date_created'] = $date;
2013-08-06 10:41:20 +00:00
// Save order data.
$order->set_props( $props );
$order->save();
2013-08-06 10:41:20 +00:00
}
2014-06-11 20:12:23 +00:00
}