class-wcs-meta-box-subscription-data.php
11.4 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
<?php
/**
* Order Data
*
* Functions for displaying the order data meta box.
*
* @author Prospress
* @category Admin
* @package WooCommerce Subscriptions/Admin/Meta Boxes
* @version 2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WCS_Meta_Box_Subscription_Data Class
*/
class WCS_Meta_Box_Subscription_Data extends WC_Meta_Box_Order_Data {
/**
* Output the metabox
*/
public static function output( $post ) {
global $the_subscription;
if ( ! is_object( $the_subscription ) || $the_subscription->id !== $post->ID ) {
$the_subscription = wc_get_order( $post->ID );
}
$subscription = $the_subscription;
self::init_address_fields();
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
?>
<style type="text/css">
#post-body-content, #titlediv, #major-publishing-actions, #minor-publishing-actions, #visibility, #submitdiv { display:none }
</style>
<div class="panel-wrap woocommerce">
<input name="post_title" type="hidden" value="<?php echo empty( $post->post_title ) ? esc_attr( get_post_type_object( $subscription->post->post_type )->labels->singular_name ) : esc_attr( $post->post_title ); ?>" />
<input name="post_status" type="hidden" value="<?php echo esc_attr( 'wc-' . $subscription->get_status() ); ?>" />
<div id="order_data" class="panel">
<h2><?php
// translators: placeholder is the ID of the subscription
printf( esc_html_x( 'Subscription #%s details', 'edit subscription header', 'woocommerce-subscriptions' ), esc_html( $subscription->get_order_number() ) ); ?></h2>
<div class="order_data_column_container">
<div class="order_data_column">
<p class="form-field form-field-wide wc-customer-user">
<label for="customer_user"><?php esc_html_e( 'Customer:', 'woocommerce-subscriptions' ) ?> <?php
if ( ! empty( $subscription->customer_user ) ) {
$args = array(
'post_status' => 'all',
'post_type' => 'shop_subscription',
'_customer_user' => absint( $subscription->customer_user ),
);
printf( '<a href="%s">%s →</a>',
esc_url( add_query_arg( $args, admin_url( 'edit.php' ) ) ),
esc_html__( 'View other subscriptions', 'woocommerce-subscriptions' )
);
}
?></label>
<?php
$user_string = '';
$user_id = '';
if ( ! empty( $subscription->customer_user ) && ( false !== get_userdata( $subscription->customer_user ) ) ) {
$user_id = absint( $subscription->customer_user );
$user = get_user_by( 'id', $user_id );
$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' – ' . esc_html( $user->user_email );
}
?>
<input type="hidden" class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Search for a customer…', 'woocommerce-subscriptions' ); ?>" data-selected="<?php echo esc_attr( $user_string ); ?>" value="<?php echo esc_attr( $user_id ); ?>" />
</p>
<p class="form-field form-field-wide">
<label for="order_status"><?php esc_html_e( 'Subscription Status:', 'woocommerce-subscriptions' ); ?></label>
<select id="order_status" name="order_status">
<?php
$statuses = wcs_get_subscription_statuses();
foreach ( $statuses as $status => $status_name ) {
if ( ! $subscription->can_be_updated_to( $status ) && ! $subscription->has_status( str_replace( 'wc-', '', $status ) ) ) {
continue;
}
echo '<option value="' . esc_attr( $status ) . '" ' . selected( $status, 'wc-' . $subscription->get_status(), false ) . '>' . esc_html( $status_name ) . '</option>';
}
?>
</select>
</p>
<?php do_action( 'woocommerce_admin_order_data_after_order_details', $subscription ); ?>
</div>
<div class="order_data_column">
<h4><?php esc_html_e( 'Billing Details', 'woocommerce-subscriptions' ); ?> <a class="edit_address" href="#"><a href="#" class="tips load_customer_billing" data-tip="Load billing address" style="display:none;">Load billing address</a></a></h4>
<?php
// Display values
echo '<div class="address">';
if ( $subscription->get_formatted_billing_address() ) {
echo '<p><strong>' . esc_html__( 'Address', 'woocommerce-subscriptions' ) . ':</strong>' . wp_kses( $subscription->get_formatted_billing_address(), array( 'br' => array() ) ) . '</p>';
} else {
echo '<p class="none_set"><strong>' . esc_html__( 'Address', 'woocommerce-subscriptions' ) . ':</strong> ' . esc_html__( 'No billing address set.', 'woocommerce-subscriptions' ) . '</p>';
}
foreach ( self::$billing_fields as $key => $field ) {
if ( isset( $field['show'] ) && false === $field['show'] ) {
continue;
}
$field_name = 'billing_' . $key;
if ( $subscription->$field_name ) {
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . wp_kses_post( make_clickable( esc_html( $subscription->$field_name ) ) ) . '</p>';
}
}
echo '<p' . ( ! empty( $subscription->payment_method ) ? ' class="' . esc_attr( $subscription->payment_method ) . '"' : '' ) . '><strong>' . esc_html__( 'Payment Method', 'woocommerce-subscriptions' ) . ':</strong>' . wp_kses_post( nl2br( $subscription->get_payment_method_to_display() ) );
// Display help tip
if ( ! empty( $subscription->payment_method ) && ! $subscription->is_manual() ) {
echo '<img class="help_tip" data-tip="Gateway ID: [' . esc_attr( $subscription->payment_gateway->id ) . ']" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16" />';
}
echo '</p>';
echo '</div>';
// Display form
echo '<div class="edit_address">';
foreach ( self::$billing_fields as $key => $field ) {
if ( ! isset( $field['type'] ) ) {
$field['type'] = 'text';
}
switch ( $field['type'] ) {
case 'select' :
// allow for setting a default value programaticaly, and draw the selectbox
woocommerce_wp_select( array( 'id' => '_billing_' . $key, 'label' => $field['label'], 'options' => $field['options'], 'value' => isset( $field['value'] ) ? $field['value'] : null ) );
break;
default :
// allow for setting a default value programaticaly, and draw the textbox
woocommerce_wp_text_input( array( 'id' => '_billing_' . $key, 'label' => $field['label'], 'value' => isset( $field['value'] ) ? $field['value'] : null ) );
break;
}
}
WCS_Change_Payment_Method_Admin::display_fields( $subscription );
echo '</div>';
do_action( 'woocommerce_admin_order_data_after_billing_address', $subscription );
?>
</div>
<div class="order_data_column">
<h4><?php esc_html_e( 'Shipping Details', 'woocommerce-subscriptions' ); ?>
<a class="edit_address" href="#">
<a href="#" class="tips billing-same-as-shipping" data-tip="Copy from billing" style="display:none;">Copy from billing</a>
<a href="#" class="tips load_customer_shipping" data-tip="Load shipping address" style="display:none;">Load shipping address</a>
</a>
</h4>
<?php
// Display values
echo '<div class="address">';
if ( $subscription->get_formatted_shipping_address() ) {
echo '<p><strong>' . esc_html__( 'Address', 'woocommerce-subscriptions' ) . ':</strong>' . wp_kses( $subscription->get_formatted_shipping_address(), array( 'br' => array() ) ) . '</p>';
} else {
echo '<p class="none_set"><strong>' . esc_html__( 'Address', 'woocommerce-subscriptions' ) . ':</strong> ' . esc_html__( 'No shipping address set.', 'woocommerce-subscriptions' ) . '</p>';
}
if ( self::$shipping_fields ) {
foreach ( self::$shipping_fields as $key => $field ) {
if ( isset( $field['show'] ) && false === $field['show'] ) {
continue;
}
$field_name = 'shipping_' . $key;
if ( ! empty( $subscription->$field_name ) ) {
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . wp_kses_post( make_clickable( esc_html( $subscription->$field_name ) ) ) . '</p>';
}
}
}
if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) && $post->post_excerpt ) {
echo '<p><strong>' . esc_html__( 'Customer Note:', 'woocommerce-subscriptions' ) . '</strong> ' . wp_kses_post( nl2br( $post->post_excerpt ) ) . '</p>';
}
echo '</div>';
// Display form
echo '<div class="edit_address">';
if ( self::$shipping_fields ) {
foreach ( self::$shipping_fields as $key => $field ) {
if ( ! isset( $field['type'] ) ) {
$field['type'] = 'text';
}
switch ( $field['type'] ) {
case 'select' :
woocommerce_wp_select( array( 'id' => '_shipping_' . $key, 'label' => $field['label'], 'options' => $field['options'] ) );
break;
default :
woocommerce_wp_text_input( array( 'id' => '_shipping_' . $key, 'label' => $field['label'] ) );
break;
}
}
}
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 esc_html_e( 'Customer Note:', 'woocommerce-subscriptions' ) ?></label>
<textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt" placeholder="<?php esc_attr_e( 'Customer\'s notes about the order', 'woocommerce-subscriptions' ); ?>"><?php echo wp_kses_post( $post->post_excerpt ); ?></textarea></p>
<?php
}
echo '</div>';
do_action( 'woocommerce_admin_order_data_after_shipping_address', $subscription );
?>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<?php
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
global $wpdb;
if ( 'shop_subscription' != $post->post_type || empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) {
return;
}
self::init_address_fields();
// Update meta
update_post_meta( $post_id, '_customer_user', absint( $_POST['customer_user'] ) );
if ( self::$billing_fields ) {
foreach ( self::$billing_fields as $key => $field ) {
update_post_meta( $post_id, '_billing_' . $key, wc_clean( $_POST[ '_billing_' . $key ] ) );
}
}
if ( self::$shipping_fields ) {
foreach ( self::$shipping_fields as $key => $field ) {
update_post_meta( $post_id, '_shipping_' . $key, wc_clean( $_POST[ '_shipping_' . $key ] ) );
}
}
$subscription = wcs_get_subscription( $post_id );
try {
WCS_Change_Payment_Method_Admin::save_meta( $subscription );
if ( 'cancelled' == $_POST['order_status'] ) {
$subscription->cancel_order();
} else {
$subscription->update_status( $_POST['order_status'], '', true );
}
} catch ( Exception $e ) {
// translators: placeholder is error message from the payment gateway or subscriptions when updating the status
wcs_add_admin_notice( sprintf( __( 'Error updating subscription: %s', 'woocommerce-subscriptions' ), $e->getMessage() ), 'error' );
}
do_action( 'woocommerce_process_shop_subscription_meta', $post_id, $post );
}
}