· 9 years ago · Oct 25, 2016, 06:56 PM
1<?php
2/**
3 * Product Data
4 *
5 * Displays the product data box, tabbed, with several panels covering price, stock etc.
6 *
7 * @author WooThemes
8 * @category Admin
9 * @package WooCommerce/Admin/Meta Boxes
10 * @version 2.4.0
11 */
12
13if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15}
16
17/**
18 * WC_Meta_Box_Product_Data Class.
19 */
20class WC_Meta_Box_Product_Data {
21
22 /**
23 * Output the metabox.
24 *
25 * @param WP_Post $post
26 */
27 public static function output( $post ) {
28 global $post, $thepostid;
29
30 wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
31
32 $thepostid = $post->ID;
33
34 if ( $terms = wp_get_object_terms( $post->ID, 'product_type' ) ) {
35 $product_type = sanitize_title( current( $terms )->name );
36 } else {
37 $product_type = apply_filters( 'default_product_type', 'simple' );
38 }
39
40 $type_box = '<label for="product-type"><select id="product-type" name="product-type"><optgroup label="' . esc_attr__( 'Product Type', 'woocommerce' ) . '">';
41
42 foreach ( wc_get_product_types() as $value => $label ) {
43 $type_box .= '<option value="' . esc_attr( $value ) . '" ' . selected( $product_type, $value, false ) .'>' . esc_html( $label ) . '</option>';
44 }
45
46 $type_box .= '</optgroup></select></label>';
47
48 $product_type_options = apply_filters( 'product_type_options', array(
49 'virtual' => array(
50 'id' => '_virtual',
51 'wrapper_class' => 'show_if_simple',
52 'label' => __( 'Virtual', 'woocommerce' ),
53 'description' => __( 'Virtual products are intangible and aren\'t shipped.', 'woocommerce' ),
54 'default' => 'no'
55 ),
56 'downloadable' => array(
57 'id' => '_downloadable',
58 'wrapper_class' => 'show_if_simple',
59 'label' => __( 'Downloadable', 'woocommerce' ),
60 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ),
61 'default' => 'no'
62 )
63 ) );
64
65 foreach ( $product_type_options as $key => $option ) {
66 $selected_value = get_post_meta( $post->ID, '_' . $key, true );
67
68 if ( '' == $selected_value && isset( $option['default'] ) ) {
69 $selected_value = $option['default'];
70 }
71
72 $type_box .= '<label for="' . esc_attr( $option['id'] ) . '" class="'. esc_attr( $option['wrapper_class'] ) . ' tips" data-tip="' . esc_attr( $option['description'] ) . '">' . esc_html( $option['label'] ) . ': <input type="checkbox" name="' . esc_attr( $option['id'] ) . '" id="' . esc_attr( $option['id'] ) . '" ' . checked( $selected_value, 'yes', false ) .' /></label>';
73 }
74
75 ?>
76 <div class="panel-wrap product_data">
77
78 <span class="type_box hidden"> — <?php echo $type_box; ?></span>
79
80 <ul class="product_data_tabs wc-tabs">
81 <?php
82 $product_data_tabs = apply_filters( 'woocommerce_product_data_tabs', array(
83 'general' => array(
84 'label' => __( 'General', 'woocommerce' ),
85 'target' => 'general_product_data',
86 'class' => array( 'hide_if_grouped' ),
87 ),
88 'inventory' => array(
89 'label' => __( 'Inventory', 'woocommerce' ),
90 'target' => 'inventory_product_data',
91 'class' => array( 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ),
92 ),
93 'shipping' => array(
94 'label' => __( 'Shipping', 'woocommerce' ),
95 'target' => 'shipping_product_data',
96 'class' => array( 'hide_if_virtual', 'hide_if_grouped', 'hide_if_external' ),
97 ),
98 'linked_product' => array(
99 'label' => __( 'Linked Products', 'woocommerce' ),
100 'target' => 'linked_product_data',
101 'class' => array(),
102 ),
103 'attribute' => array(
104 'label' => __( 'Attributes', 'woocommerce' ),
105 'target' => 'product_attributes',
106 'class' => array(),
107 ),
108 'variations' => array(
109 'label' => __( 'Variations', 'woocommerce' ),
110 'target' => 'variable_product_options',
111 'class' => array( 'variations_tab', 'show_if_variable' ),
112 ),
113 'advanced' => array(
114 'label' => __( 'Advanced', 'woocommerce' ),
115 'target' => 'advanced_product_data',
116 'class' => array(),
117 )
118 ) );
119
120 foreach ( $product_data_tabs as $key => $tab ) {
121 ?><li class="<?php echo $key; ?>_options <?php echo $key; ?>_tab <?php echo implode( ' ' , (array) $tab['class'] ); ?>">
122 <a href="#<?php echo $tab['target']; ?>"><?php echo esc_html( $tab['label'] ); ?></a>
123 </li><?php
124 }
125
126 do_action( 'woocommerce_product_write_panel_tabs' );
127 ?>
128 </ul>
129 <div id="general_product_data" class="panel woocommerce_options_panel"><?php
130
131 echo '<div class="options_group show_if_external">';
132
133 // External URL
134 woocommerce_wp_text_input( array( 'id' => '_product_url', 'label' => __( 'Product URL', 'woocommerce' ), 'placeholder' => 'http://', 'description' => __( 'Enter the external URL to the product.', 'woocommerce' ) ) );
135
136 // Button text
137 woocommerce_wp_text_input( array( 'id' => '_button_text', 'label' => __( 'Button text', 'woocommerce' ), 'placeholder' => _x('Buy product', 'placeholder', 'woocommerce'), 'description' => __( 'This text will be shown on the button linking to the external product.', 'woocommerce' ) ) );
138
139 echo '</div>';
140
141 echo '<div class="options_group pricing show_if_simple show_if_external hidden">';
142
143 // Price
144 woocommerce_wp_text_input( array( 'id' => '_regular_price', 'label' => __( 'Regular price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) );
145
146 // Special Price
147 woocommerce_wp_text_input( array( 'id' => '_sale_price', 'data_type' => 'price', 'label' => __( 'Sale price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')', 'description' => '<a href="#" class="sale_schedule">' . __( 'Schedule', 'woocommerce' ) . '</a>' ) );
148
149 // Special Price date range
150 $sale_price_dates_from = ( $date = get_post_meta( $thepostid, '_sale_price_dates_from', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';
151 $sale_price_dates_to = ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';
152
153 echo '<p class="form-field sale_price_dates_fields">
154 <label for="_sale_price_dates_from">' . __( 'Sale price dates', 'woocommerce' ) . '</label>
155 <input type="text" class="short" name="_sale_price_dates_from" id="_sale_price_dates_from" value="' . esc_attr( $sale_price_dates_from ) . '" placeholder="' . _x( 'From…', 'placeholder', 'woocommerce' ) . ' YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
156 <input type="text" class="short" name="_sale_price_dates_to" id="_sale_price_dates_to" value="' . esc_attr( $sale_price_dates_to ) . '" placeholder="' . _x( 'To…', 'placeholder', 'woocommerce' ) . ' YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
157 <a href="#" class="cancel_sale_schedule">'. __( 'Cancel', 'woocommerce' ) . '</a>' . wc_help_tip( __( 'The sale will end at the beginning of the set date.', 'woocommerce' ) ) . '
158 </p>';
159
160 do_action( 'woocommerce_product_options_pricing' );
161
162 echo '</div>';
163
164 echo '<div class="options_group show_if_downloadable hidden">';
165
166 ?>
167 <div class="form-field downloadable_files">
168 <label><?php _e( 'Downloadable files', 'woocommerce' ); ?></label>
169 <table class="widefat">
170 <thead>
171 <tr>
172 <th class="sort"> </th>
173 <th><?php _e( 'Name', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the name of the download shown to the customer.', 'woocommerce' ) ); ?></th>
174 <th colspan="2"><?php _e( 'File URL', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the URL or absolute path to the file which customers will get access to. URLs entered here should already be encoded.', 'woocommerce' ) ); ?></th>
175 <th> </th>
176 </tr>
177 </thead>
178 <tbody>
179 <?php
180 $downloadable_files = get_post_meta( $post->ID, '_downloadable_files', true );
181
182 if ( $downloadable_files ) {
183 foreach ( $downloadable_files as $key => $file ) {
184 include( 'views/html-product-download.php' );
185 }
186 }
187 ?>
188 </tbody>
189 <tfoot>
190 <tr>
191 <th colspan="5">
192 <a href="#" class="button insert" data-row="<?php
193 $file = array(
194 'file' => '',
195 'name' => ''
196 );
197 ob_start();
198 include( 'views/html-product-download.php' );
199 echo esc_attr( ob_get_clean() );
200 ?>"><?php _e( 'Add File', 'woocommerce' ); ?></a>
201 </th>
202 </tr>
203 </tfoot>
204 </table>
205 </div>
206 <?php
207
208 // Download Limit
209 woocommerce_wp_text_input( array( 'id' => '_download_limit', 'label' => __( 'Download limit', 'woocommerce' ), 'placeholder' => __( 'Unlimited', 'woocommerce' ), 'description' => __( 'Leave blank for unlimited re-downloads.', 'woocommerce' ), 'type' => 'number', 'custom_attributes' => array(
210 'step' => '1',
211 'min' => '0'
212 ) ) );
213
214 // Expirey
215 woocommerce_wp_text_input( array( 'id' => '_download_expiry', 'label' => __( 'Download expiry', 'woocommerce' ), 'placeholder' => __( 'Never', 'woocommerce' ), 'description' => __( 'Enter the number of days before a download link expires, or leave blank.', 'woocommerce' ), 'type' => 'number', 'custom_attributes' => array(
216 'step' => '1',
217 'min' => '0'
218 ) ) );
219
220 // Download Type
221 woocommerce_wp_select( array( 'id' => '_download_type', 'label' => __( 'Download type', 'woocommerce' ), 'description' => sprintf( __( 'Choose a download type - this controls the <a href="%s">schema</a>.', 'woocommerce' ), 'http://schema.org/' ), 'options' => array(
222 '' => __( 'Standard Product', 'woocommerce' ),
223 'application' => __( 'Application/Software', 'woocommerce' ),
224 'music' => __( 'Music', 'woocommerce' ),
225 ) ) );
226
227 do_action( 'woocommerce_product_options_downloads' );
228
229 echo '</div>';
230
231 if ( wc_tax_enabled() ) {
232
233 echo '<div class="options_group show_if_simple show_if_external show_if_variable">';
234
235 // Tax
236 woocommerce_wp_select( array(
237 'id' => '_tax_status',
238 'label' => __( 'Tax status', 'woocommerce' ),
239 'options' => array(
240 'taxable' => __( 'Taxable', 'woocommerce' ),
241 'shipping' => __( 'Shipping only', 'woocommerce' ),
242 'none' => _x( 'None', 'Tax status', 'woocommerce' )
243 ),
244 'desc_tip' => 'true',
245 'description' => __( 'Define whether or not the entire product is taxable, or just the cost of shipping it.', 'woocommerce' )
246 ) );
247
248 $tax_classes = WC_Tax::get_tax_classes();
249 $classes_options = array();
250 $classes_options[''] = __( 'Standard', 'woocommerce' );
251
252 if ( ! empty( $tax_classes ) ) {
253 foreach ( $tax_classes as $class ) {
254 $classes_options[ sanitize_title( $class ) ] = esc_html( $class );
255 }
256 }
257
258 woocommerce_wp_select( array(
259 'id' => '_tax_class',
260 'label' => __( 'Tax class', 'woocommerce' ),
261 'options' => $classes_options,
262 'desc_tip' => 'true',
263 'description' => __( 'Choose a tax class for this product. Tax classes are used to apply different tax rates specific to certain types of product.', 'woocommerce' )
264 ) );
265
266 do_action( 'woocommerce_product_options_tax' );
267
268 echo '</div>';
269
270 }
271
272 do_action( 'woocommerce_product_options_general_product_data' );
273 ?>
274 </div>
275
276 <div id="inventory_product_data" class="panel woocommerce_options_panel hidden">
277
278 <?php
279
280 echo '<div class="options_group">';
281
282 // SKU
283 if ( wc_product_sku_enabled() ) {
284 woocommerce_wp_text_input( array( 'id' => '_sku', 'label' => '<abbr title="'. __( 'Stock Keeping Unit', 'woocommerce' ) .'">' . __( 'SKU', 'woocommerce' ) . '</abbr>', 'desc_tip' => 'true', 'description' => __( 'SKU refers to a Stock-keeping unit, a unique identifier for each distinct product and service that can be purchased.', 'woocommerce' ) ) );
285 } else {
286 echo '<input type="hidden" name="_sku" value="' . esc_attr( get_post_meta( $thepostid, '_sku', true ) ) . '" />';
287 }
288
289 do_action( 'woocommerce_product_options_sku' );
290
291 if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
292
293 // manage stock
294 woocommerce_wp_checkbox( array( 'id' => '_manage_stock', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Manage stock?', 'woocommerce' ), 'description' => __( 'Enable stock management at product level', 'woocommerce' ) ) );
295
296 do_action( 'woocommerce_product_options_stock' );
297
298 echo '<div class="stock_fields show_if_simple show_if_variable">';
299
300 // Stock
301 woocommerce_wp_text_input( array(
302 'id' => '_stock',
303 'label' => __( 'Stock quantity', 'woocommerce' ),
304 'desc_tip' => true,
305 'description' => __( 'Stock quantity. If this is a variable product this value will be used to control stock for all variations, unless you define stock at variation level.', 'woocommerce' ),
306 'type' => 'number',
307 'custom_attributes' => array(
308 'step' => 'any'
309 ),
310 'data_type' => 'stock'
311 ) );
312
313 // Backorders?
314 woocommerce_wp_select( array( 'id' => '_backorders', 'label' => __( 'Allow backorders?', 'woocommerce' ), 'options' => array(
315 'no' => __( 'Do not allow', 'woocommerce' ),
316 'notify' => __( 'Allow, but notify customer', 'woocommerce' ),
317 'yes' => __( 'Allow', 'woocommerce' )
318 ), 'desc_tip' => true, 'description' => __( 'If managing stock, this controls whether or not backorders are allowed. If enabled, stock quantity can go below 0.', 'woocommerce' ) ) );
319
320 do_action( 'woocommerce_product_options_stock_fields' );
321
322 echo '</div>';
323
324 }
325
326 // Stock status
327 woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable hide_if_external', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
328 'instock' => __( 'In stock', 'woocommerce' ),
329 'outofstock' => __( 'Out of stock', 'woocommerce' )
330 ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
331
332 do_action( 'woocommerce_product_options_stock_status' );
333
334 echo '</div>';
335
336 echo '<div class="options_group show_if_simple show_if_variable">';
337
338 // Individual product
339 woocommerce_wp_checkbox( array( 'id' => '_sold_individually', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Sold individually', 'woocommerce' ), 'description' => __( 'Enable this to only allow one of this item to be bought in a single order', 'woocommerce' ) ) );
340
341 do_action( 'woocommerce_product_options_sold_individually' );
342
343 echo '</div>';
344
345 do_action( 'woocommerce_product_options_inventory_product_data' );
346 ?>
347
348 </div>
349
350 <div id="shipping_product_data" class="panel woocommerce_options_panel hidden">
351
352 <?php
353
354 echo '<div class="options_group">';
355
356 // Weight
357 if ( wc_product_weight_enabled() ) {
358 woocommerce_wp_text_input( array( 'id' => '_weight', 'label' => __( 'Weight', 'woocommerce' ) . ' (' . get_option( 'woocommerce_weight_unit' ) . ')', 'placeholder' => wc_format_localized_decimal( 0 ), 'desc_tip' => 'true', 'description' => __( 'Weight in decimal form', 'woocommerce' ), 'type' => 'text', 'data_type' => 'decimal' ) );
359 }
360
361 // Size fields
362 if ( wc_product_dimensions_enabled() ) {
363 ?><p class="form-field dimensions_field">
364 <label for="product_length"><?php echo __( 'Dimensions', 'woocommerce' ) . ' (' . get_option( 'woocommerce_dimension_unit' ) . ')'; ?></label>
365 <span class="wrap">
366 <input id="product_length" placeholder="<?php esc_attr_e( 'Length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="_length" value="<?php echo esc_attr( wc_format_localized_decimal( get_post_meta( $thepostid, '_length', true ) ) ); ?>" />
367 <input placeholder="<?php esc_attr_e( 'Width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="_width" value="<?php echo esc_attr( wc_format_localized_decimal( get_post_meta( $thepostid, '_width', true ) ) ); ?>" />
368 <input placeholder="<?php esc_attr_e( 'Height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="_height" value="<?php echo esc_attr( wc_format_localized_decimal( get_post_meta( $thepostid, '_height', true ) ) ); ?>" />
369 </span>
370 <?php echo wc_help_tip( __( 'LxWxH in decimal form', 'woocommerce' ) ); ?>
371 </p><?php
372 }
373
374 do_action( 'woocommerce_product_options_dimensions' );
375
376 echo '</div>';
377
378 echo '<div class="options_group">';
379
380 // Shipping Class
381 $classes = get_the_terms( $thepostid, 'product_shipping_class' );
382 if ( $classes && ! is_wp_error( $classes ) ) {
383 $current_shipping_class = current( $classes )->term_id;
384 } else {
385 $current_shipping_class = '';
386 }
387
388 $args = array(
389 'taxonomy' => 'product_shipping_class',
390 'hide_empty' => 0,
391 'show_option_none' => __( 'No shipping class', 'woocommerce' ),
392 'name' => 'product_shipping_class',
393 'id' => 'product_shipping_class',
394 'selected' => $current_shipping_class,
395 'class' => 'select short'
396 );
397 ?><p class="form-field dimensions_field"><label for="product_shipping_class"><?php _e( 'Shipping class', 'woocommerce' ); ?></label> <?php wp_dropdown_categories( $args ); ?> <?php echo wc_help_tip( __( 'Shipping classes are used by certain shipping methods to group similar products.', 'woocommerce' ) ); ?></p><?php
398
399 do_action( 'woocommerce_product_options_shipping' );
400
401 echo '</div>';
402 ?>
403
404 </div>
405
406 <div id="product_attributes" class="panel wc-metaboxes-wrapper hidden">
407 <div class="toolbar toolbar-top">
408 <span class="expand-close">
409 <a href="#" class="expand_all"><?php _e( 'Expand', 'woocommerce' ); ?></a> / <a href="#" class="close_all"><?php _e( 'Close', 'woocommerce' ); ?></a>
410 </span>
411 <select name="attribute_taxonomy" class="attribute_taxonomy">
412 <option value=""><?php _e( 'Custom product attribute', 'woocommerce' ); ?></option>
413 <?php
414 global $wc_product_attributes;
415
416 // Array of defined attribute taxonomies
417 $attribute_taxonomies = wc_get_attribute_taxonomies();
418
419 if ( ! empty( $attribute_taxonomies ) ) {
420 foreach ( $attribute_taxonomies as $tax ) {
421 $attribute_taxonomy_name = wc_attribute_taxonomy_name( $tax->attribute_name );
422 $label = $tax->attribute_label ? $tax->attribute_label : $tax->attribute_name;
423 echo '<option value="' . esc_attr( $attribute_taxonomy_name ) . '">' . esc_html( $label ) . '</option>';
424 }
425 }
426 ?>
427 </select>
428 <button type="button" class="button add_attribute"><?php _e( 'Add', 'woocommerce' ); ?></button>
429 </div>
430 <div class="product_attributes wc-metaboxes">
431 <?php
432 // Product attributes - taxonomies and custom, ordered, with visibility and variation attributes set
433 $attributes = maybe_unserialize( get_post_meta( $thepostid, '_product_attributes', true ) );
434
435 // Output All Set Attributes
436 if ( ! empty( $attributes ) ) {
437 $attribute_keys = array_keys( $attributes );
438 $attribute_total = sizeof( $attribute_keys );
439
440 for ( $i = 0; $i < $attribute_total; $i ++ ) {
441 $attribute = $attributes[ $attribute_keys[ $i ] ];
442 $position = empty( $attribute['position'] ) ? 0 : absint( $attribute['position'] );
443 $taxonomy = '';
444 $metabox_class = array();
445
446 if ( $attribute['is_taxonomy'] ) {
447 $taxonomy = $attribute['name'];
448
449 if ( ! taxonomy_exists( $taxonomy ) ) {
450 continue;
451 }
452
453 $attribute_taxonomy = $wc_product_attributes[ $taxonomy ];
454 $metabox_class[] = 'taxonomy';
455 $metabox_class[] = $taxonomy;
456 $attribute_label = wc_attribute_label( $taxonomy );
457 } else {
458 $attribute_label = apply_filters( 'woocommerce_attribute_label', $attribute['name'], $attribute['name'], false );
459 }
460
461 include( 'views/html-product-attribute.php' );
462 }
463 }
464 ?>
465 </div>
466 <div class="toolbar">
467 <span class="expand-close">
468 <a href="#" class="expand_all"><?php _e( 'Expand', 'woocommerce' ); ?></a> / <a href="#" class="close_all"><?php _e( 'Close', 'woocommerce' ); ?></a>
469 </span>
470 <button type="button" class="button save_attributes button-primary"><?php _e( 'Save attributes', 'woocommerce' ); ?></button>
471 </div>
472 <?php do_action( 'woocommerce_product_options_attributes' ); ?>
473 </div>
474 <div id="linked_product_data" class="panel woocommerce_options_panel hidden">
475
476 <div class="options_group">
477
478 <p class="form-field">
479 <label for="upsell_ids"><?php _e( 'Up-sells', 'woocommerce' ); ?></label>
480 <input type="hidden" class="wc-product-search" style="width: 50%;" id="upsell_ids" name="upsell_ids" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products" data-multiple="true" data-exclude="<?php echo intval( $post->ID ); ?>" data-selected="<?php
481 $product_ids = array_filter( array_map( 'absint', (array) get_post_meta( $post->ID, '_upsell_ids', true ) ) );
482 $json_ids = array();
483
484 foreach ( $product_ids as $product_id ) {
485 $product = wc_get_product( $product_id );
486 if ( is_object( $product ) ) {
487 $json_ids[ $product_id ] = wp_kses_post( html_entity_decode( $product->get_formatted_name(), ENT_QUOTES, get_bloginfo( 'charset' ) ) );
488 }
489 }
490
491 echo esc_attr( json_encode( $json_ids ) );
492 ?>" value="<?php echo implode( ',', array_keys( $json_ids ) ); ?>" /> <?php echo wc_help_tip( __( 'Up-sells are products which you recommend instead of the currently viewed product, for example, products that are more profitable or better quality or more expensive.', 'woocommerce' ) ); ?>
493 </p>
494
495 <p class="form-field">
496 <label for="crosssell_ids"><?php _e( 'Cross-sells', 'woocommerce' ); ?></label>
497 <input type="hidden" class="wc-product-search" style="width: 50%;" id="crosssell_ids" name="crosssell_ids" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products" data-multiple="true" data-exclude="<?php echo intval( $post->ID ); ?>" data-selected="<?php
498 $product_ids = array_filter( array_map( 'absint', (array) get_post_meta( $post->ID, '_crosssell_ids', true ) ) );
499 $json_ids = array();
500
501 foreach ( $product_ids as $product_id ) {
502 $product = wc_get_product( $product_id );
503 if ( is_object( $product ) ) {
504 $json_ids[ $product_id ] = wp_kses_post( html_entity_decode( $product->get_formatted_name(), ENT_QUOTES, get_bloginfo( 'charset' ) ) );
505 }
506 }
507
508 echo esc_attr( json_encode( $json_ids ) );
509 ?>" value="<?php echo implode( ',', array_keys( $json_ids ) ); ?>" /> <?php echo wc_help_tip( __( 'Cross-sells are products which you promote in the cart, based on the current product.', 'woocommerce' ) ); ?>
510 </p>
511 </div>
512
513 <div class="options_group grouping show_if_simple show_if_external">
514
515 <p class="form-field">
516 <label for="parent_id"><?php _e( 'Grouping', 'woocommerce' ); ?></label>
517 <input type="hidden" class="wc-product-search" style="width: 50%;" id="parent_id" name="parent_id" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_grouped_products" data-allow_clear="true" data-multiple="false" data-exclude="<?php echo intval( $post->ID ); ?>" data-selected="<?php
518 $parent_id = absint( $post->post_parent );
519
520 if ( $parent_id ) {
521 $parent = wc_get_product( $parent_id );
522 if ( is_object( $parent ) ) {
523 $parent_title = wp_kses_post( html_entity_decode( $parent->get_formatted_name(), ENT_QUOTES, get_bloginfo( 'charset' ) ) );
524 }
525
526 echo esc_attr( $parent_title );
527 }
528 ?>" value="<?php echo $parent_id ? $parent_id : ''; ?>" /> <?php echo wc_help_tip( __( 'Set this option to make this product part of a grouped product.', 'woocommerce' ) ); ?>
529 </p>
530
531 <?php
532 woocommerce_wp_hidden_input( array( 'id' => 'previous_parent_id', 'value' => absint( $post->post_parent ) ) );
533
534 do_action( 'woocommerce_product_options_grouping' );
535 ?>
536 </div>
537
538 <?php do_action( 'woocommerce_product_options_related' ); ?>
539 </div>
540
541 <div id="advanced_product_data" class="panel woocommerce_options_panel hidden">
542
543 <div class="options_group hide_if_external">
544 <?php
545 // Purchase note
546 woocommerce_wp_textarea_input( array( 'id' => '_purchase_note', 'label' => __( 'Purchase note', 'woocommerce' ), 'desc_tip' => 'true', 'description' => __( 'Enter an optional note to send the customer after purchase.', 'woocommerce' ) ) );
547 ?>
548 </div>
549
550 <div class="options_group">
551 <?php
552 // menu_order
553 woocommerce_wp_text_input( array( 'id' => 'menu_order', 'label' => __( 'Menu order', 'woocommerce' ), 'desc_tip' => 'true', 'description' => __( 'Custom ordering position.', 'woocommerce' ), 'value' => intval( $post->menu_order ), 'type' => 'number', 'custom_attributes' => array(
554 'step' => '1'
555 ) ) );
556 ?>
557 </div>
558
559 <div class="options_group reviews">
560 <?php
561 woocommerce_wp_checkbox( array( 'id' => 'comment_status', 'label' => __( 'Enable reviews', 'woocommerce' ), 'cbvalue' => 'open', 'value' => esc_attr( $post->comment_status ) ) );
562
563 do_action( 'woocommerce_product_options_reviews' );
564 ?>
565 </div>
566
567 <?php do_action( 'woocommerce_product_options_advanced' ); ?>
568
569 </div>
570
571 <?php
572 self::output_variations();
573
574 do_action( 'woocommerce_product_data_panels' );
575 do_action( 'woocommerce_product_write_panels' ); // _deprecated
576 ?>
577
578 <div class="clear"></div>
579
580 </div>
581 <?php
582 }
583
584 /**
585 * Show options for the variable product type.
586 */
587 public static function output_variations() {
588 global $post, $wpdb;
589
590 // Get attributes
591 $attributes = maybe_unserialize( get_post_meta( $post->ID, '_product_attributes', true ) );
592
593 // See if any are set
594 $variation_attribute_found = false;
595
596 if ( $attributes ) {
597 foreach ( $attributes as $attribute ) {
598 if ( ! empty( $attribute['is_variation'] ) ) {
599 $variation_attribute_found = true;
600 break;
601 }
602 }
603 }
604
605 $variations_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' AND post_status IN ('publish', 'private')", $post->ID ) ) );
606 $variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 15 ) );
607 $variations_total_pages = ceil( $variations_count / $variations_per_page );
608 ?>
609 <div id="variable_product_options" class="panel wc-metaboxes-wrapper hidden"><div id="variable_product_options_inner">
610
611 <?php if ( ! $variation_attribute_found ) : ?>
612
613 <div id="message" class="inline notice woocommerce-message">
614 <p><?php _e( 'Before you can add a variation you need to add some variation attributes on the <strong>Attributes</strong> tab.', 'woocommerce' ); ?></p>
615 <p>
616 <a class="button-primary" href="<?php echo esc_url( apply_filters( 'woocommerce_docs_url', 'https://docs.woocommerce.com/document/variable-product/', 'product-variations' ) ); ?>" target="_blank"><?php _e( 'Learn more', 'woocommerce' ); ?></a>
617 </p>
618 </div>
619
620 <?php else : ?>
621
622 <div class="toolbar toolbar-variations-defaults">
623 <div class="variations-defaults">
624 <strong><?php _e( 'Default Form Values', 'woocommerce' ); ?>: <?php echo wc_help_tip( __( 'These are the attributes that will be pre-selected on the frontend.', 'woocommerce' ) ); ?></strong>
625 <?php
626 $default_attributes = maybe_unserialize( get_post_meta( $post->ID, '_default_attributes', true ) );
627
628 foreach ( $attributes as $attribute ) {
629
630 // Only deal with attributes that are variations
631 if ( ! $attribute['is_variation'] ) {
632 continue;
633 }
634
635 // Get current value for variation (if set)
636 $variation_selected_value = isset( $default_attributes[ sanitize_title( $attribute['name'] ) ] ) ? $default_attributes[ sanitize_title( $attribute['name'] ) ] : '';
637
638 // Name will be something like attribute_pa_color
639 echo '<select name="default_attribute_' . sanitize_title( $attribute['name'] ) . '" data-current="' . esc_attr( $variation_selected_value ) . '"><option value="">' . __( 'No default', 'woocommerce' ) . ' ' . esc_html( wc_attribute_label( $attribute['name'] ) ) . '…</option>';
640
641 // Get terms for attribute taxonomy or value if its a custom attribute
642 if ( $attribute['is_taxonomy'] ) {
643 $post_terms = wp_get_post_terms( $post->ID, $attribute['name'] );
644
645 foreach ( $post_terms as $term ) {
646 echo '<option ' . selected( $variation_selected_value, $term->slug, false ) . ' value="' . esc_attr( $term->slug ) . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>';
647 }
648
649 } else {
650 $options = wc_get_text_attributes( $attribute['value'] );
651
652 foreach ( $options as $option ) {
653 $selected = sanitize_title( $variation_selected_value ) === $variation_selected_value ? selected( $variation_selected_value, sanitize_title( $option ), false ) : selected( $variation_selected_value, $option, false );
654 echo '<option ' . $selected . ' value="' . esc_attr( $option ) . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
655 }
656
657 }
658
659 echo '</select>';
660 }
661 ?>
662 </div>
663 <div class="clear"></div>
664 </div>
665
666 <div class="toolbar toolbar-top">
667 <select id="field_to_edit" class="variation_actions">
668 <option data-global="true" value="add_variation"><?php _e( 'Add variation', 'woocommerce' ); ?></option>
669 <option data-global="true" value="link_all_variations"><?php _e( 'Create variations from all attributes', 'woocommerce' ); ?></option>
670 <option value="delete_all"><?php _e( 'Delete all variations', 'woocommerce' ); ?></option>
671 <optgroup label="<?php esc_attr_e( 'Status', 'woocommerce' ); ?>">
672 <option value="toggle_enabled"><?php _e( 'Toggle "Enabled"', 'woocommerce' ); ?></option>
673 <option value="toggle_downloadable"><?php _e( 'Toggle "Downloadable"', 'woocommerce' ); ?></option>
674 <option value="toggle_virtual"><?php _e( 'Toggle "Virtual"', 'woocommerce' ); ?></option>
675 </optgroup>
676 <optgroup label="<?php esc_attr_e( 'Pricing', 'woocommerce' ); ?>">
677 <option value="variable_regular_price"><?php _e( 'Set regular prices', 'woocommerce' ); ?></option>
678 <option value="variable_regular_price_increase"><?php _e( 'Increase regular prices (fixed amount or percentage)', 'woocommerce' ); ?></option>
679 <option value="variable_regular_price_decrease"><?php _e( 'Decrease regular prices (fixed amount or percentage)', 'woocommerce' ); ?></option>
680 <option value="variable_sale_price"><?php _e( 'Set sale prices', 'woocommerce' ); ?></option>
681 <option value="variable_sale_price_increase"><?php _e( 'Increase sale prices (fixed amount or percentage)', 'woocommerce' ); ?></option>
682 <option value="variable_sale_price_decrease"><?php _e( 'Decrease sale prices (fixed amount or percentage)', 'woocommerce' ); ?></option>
683 <option value="variable_sale_schedule"><?php _e( 'Set scheduled sale dates', 'woocommerce' ); ?></option>
684 </optgroup>
685 <optgroup label="<?php esc_attr_e( 'Inventory', 'woocommerce' ); ?>">
686 <option value="toggle_manage_stock"><?php _e( 'Toggle "Manage stock"', 'woocommerce' ); ?></option>
687 <option value="variable_stock"><?php _e( 'Stock', 'woocommerce' ); ?></option>
688 </optgroup>
689 <optgroup label="<?php esc_attr_e( 'Shipping', 'woocommerce' ); ?>">
690 <option value="variable_length"><?php _e( 'Length', 'woocommerce' ); ?></option>
691 <option value="variable_width"><?php _e( 'Width', 'woocommerce' ); ?></option>
692 <option value="variable_height"><?php _e( 'Height', 'woocommerce' ); ?></option>
693 <option value="variable_weight"><?php _e( 'Weight', 'woocommerce' ); ?></option>
694 </optgroup>
695 <optgroup label="<?php esc_attr_e( 'Downloadable products', 'woocommerce' ); ?>">
696 <option value="variable_download_limit"><?php _e( 'Download limit', 'woocommerce' ); ?></option>
697 <option value="variable_download_expiry"><?php _e( 'Download expiry', 'woocommerce' ); ?></option>
698 </optgroup>
699 <?php do_action( 'woocommerce_variable_product_bulk_edit_actions' ); ?>
700 </select>
701 <a class="button bulk_edit do_variation_action"><?php _e( 'Go', 'woocommerce' ); ?></a>
702
703 <div class="variations-pagenav">
704 <span class="displaying-num"><?php printf( _n( '%s item', '%s items', $variations_count, 'woocommerce' ), $variations_count ); ?></span>
705 <span class="expand-close">
706 (<a href="#" class="expand_all"><?php _e( 'Expand', 'woocommerce' ); ?></a> / <a href="#" class="close_all"><?php _e( 'Close', 'woocommerce' ); ?></a>)
707 </span>
708 <span class="pagination-links">
709 <a class="first-page disabled" title="<?php esc_attr_e( 'Go to the first page', 'woocommerce' ); ?>" href="#">«</a>
710 <a class="prev-page disabled" title="<?php esc_attr_e( 'Go to the previous page', 'woocommerce' ); ?>" href="#">‹</a>
711 <span class="paging-select">
712 <label for="current-page-selector-1" class="screen-reader-text"><?php _e( 'Select Page', 'woocommerce' ); ?></label>
713 <select class="page-selector" id="current-page-selector-1" title="<?php esc_attr_e( 'Current page', 'woocommerce' ); ?>">
714 <?php for ( $i = 1; $i <= $variations_total_pages; $i++ ) : ?>
715 <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
716 <?php endfor; ?>
717 </select>
718 <?php _ex( 'of', 'number of pages', 'woocommerce' ); ?> <span class="total-pages"><?php echo $variations_total_pages; ?></span>
719 </span>
720 <a class="next-page" title="<?php esc_attr_e( 'Go to the next page', 'woocommerce' ); ?>" href="#">›</a>
721 <a class="last-page" title="<?php esc_attr_e( 'Go to the last page', 'woocommerce' ); ?>" href="#">»</a>
722 </span>
723 </div>
724 <div class="clear"></div>
725 </div>
726
727 <div class="woocommerce_variations wc-metaboxes" data-attributes="<?php
728 // esc_attr does not double encode - htmlspecialchars does
729 echo htmlspecialchars( json_encode( $attributes ) );
730 ?>" data-total="<?php echo $variations_count; ?>" data-total_pages="<?php echo $variations_total_pages; ?>" data-page="1" data-edited="false">
731 </div>
732
733 <div class="toolbar">
734 <button type="button" class="button-primary save-variation-changes" disabled="disabled"><?php _e( 'Save changes', 'woocommerce' ); ?></button>
735 <button type="button" class="button cancel-variation-changes" disabled="disabled"><?php _e( 'Cancel', 'woocommerce' ); ?></button>
736
737 <div class="variations-pagenav">
738 <span class="displaying-num"><?php printf( _n( '%s item', '%s items', $variations_count, 'woocommerce' ), $variations_count ); ?></span>
739 <span class="expand-close">
740 (<a href="#" class="expand_all"><?php _e( 'Expand', 'woocommerce' ); ?></a> / <a href="#" class="close_all"><?php _e( 'Close', 'woocommerce' ); ?></a>)
741 </span>
742 <span class="pagination-links">
743 <a class="first-page disabled" title="<?php esc_attr_e( 'Go to the first page', 'woocommerce' ); ?>" href="#">«</a>
744 <a class="prev-page disabled" title="<?php esc_attr_e( 'Go to the previous page', 'woocommerce' ); ?>" href="#">‹</a>
745 <span class="paging-select">
746 <label for="current-page-selector-1" class="screen-reader-text"><?php _e( 'Select Page', 'woocommerce' ); ?></label>
747 <select class="page-selector" id="current-page-selector-1" title="<?php esc_attr_e( 'Current page', 'woocommerce' ); ?>">
748 <?php for ( $i = 1; $i <= $variations_total_pages; $i++ ) : ?>
749 <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
750 <?php endfor; ?>
751 </select>
752 <?php _ex( 'of', 'number of pages', 'woocommerce' ); ?> <span class="total-pages"><?php echo $variations_total_pages; ?></span>
753 </span>
754 <a class="next-page" title="<?php esc_attr_e( 'Go to the next page', 'woocommerce' ); ?>" href="#">›</a>
755 <a class="last-page" title="<?php esc_attr_e( 'Go to the last page', 'woocommerce' ); ?>" href="#">»</a>
756 </span>
757 </div>
758
759 <div class="clear"></div>
760 </div>
761
762 <?php endif; ?>
763 </div></div>
764 <?php
765 }
766
767 /**
768 * Save meta box data.
769 */
770 public static function save( $post_id, $post ) {
771 global $wpdb;
772
773 // Add any default post meta
774 add_post_meta( $post_id, 'total_sales', '0', true );
775
776 // Get types
777 $product_type = empty( $_POST['product-type'] ) ? 'simple' : sanitize_title( stripslashes( $_POST['product-type'] ) );
778 $is_downloadable = isset( $_POST['_downloadable'] ) ? 'yes' : 'no';
779 $is_virtual = isset( $_POST['_virtual'] ) ? 'yes' : 'no';
780
781 // Product type + Downloadable/Virtual
782 wp_set_object_terms( $post_id, $product_type, 'product_type' );
783 update_post_meta( $post_id, '_downloadable', $is_downloadable );
784 update_post_meta( $post_id, '_virtual', $is_virtual );
785
786 // Update post meta
787 if ( isset( $_POST['_tax_status'] ) ) {
788 update_post_meta( $post_id, '_tax_status', wc_clean( $_POST['_tax_status'] ) );
789 }
790
791 if ( isset( $_POST['_tax_class'] ) ) {
792 update_post_meta( $post_id, '_tax_class', wc_clean( $_POST['_tax_class'] ) );
793 }
794
795 if ( isset( $_POST['_purchase_note'] ) ) {
796 update_post_meta( $post_id, '_purchase_note', wp_kses_post( stripslashes( $_POST['_purchase_note'] ) ) );
797 }
798
799 // Featured
800 if ( update_post_meta( $post_id, '_featured', isset( $_POST['_featured'] ) ? 'yes' : 'no' ) ) {
801 delete_transient( 'wc_featured_products' );
802 }
803
804 // Dimensions
805 if ( 'no' == $is_virtual ) {
806
807 if ( isset( $_POST['_weight'] ) ) {
808 update_post_meta( $post_id, '_weight', ( '' === $_POST['_weight'] ) ? '' : wc_format_decimal( $_POST['_weight'] ) );
809 }
810
811 if ( isset( $_POST['_length'] ) ) {
812 update_post_meta( $post_id, '_length', ( '' === $_POST['_length'] ) ? '' : wc_format_decimal( $_POST['_length'] ) );
813 }
814
815 if ( isset( $_POST['_width'] ) ) {
816 update_post_meta( $post_id, '_width', ( '' === $_POST['_width'] ) ? '' : wc_format_decimal( $_POST['_width'] ) );
817 }
818
819 if ( isset( $_POST['_height'] ) ) {
820 update_post_meta( $post_id, '_height', ( '' === $_POST['_height'] ) ? '' : wc_format_decimal( $_POST['_height'] ) );
821 }
822
823 } else {
824 update_post_meta( $post_id, '_weight', '' );
825 update_post_meta( $post_id, '_length', '' );
826 update_post_meta( $post_id, '_width', '' );
827 update_post_meta( $post_id, '_height', '' );
828 }
829
830 // Save shipping class
831 $product_shipping_class = $_POST['product_shipping_class'] > 0 && $product_type != 'external' ? absint( $_POST['product_shipping_class'] ) : '';
832 wp_set_object_terms( $post_id, $product_shipping_class, 'product_shipping_class');
833
834 // Unique SKU
835 $sku = get_post_meta( $post_id, '_sku', true );
836 $new_sku = (string) wc_clean( $_POST['_sku'] );
837
838 if ( '' == $new_sku ) {
839 update_post_meta( $post_id, '_sku', '' );
840 } elseif ( $new_sku !== $sku ) {
841 if ( ! empty( $new_sku ) ) {
842 $unique_sku = wc_product_has_unique_sku( $post_id, $new_sku );
843
844 if ( ! $unique_sku ) {
845 WC_Admin_Meta_Boxes::add_error( __( 'Product SKU must be unique.', 'woocommerce' ) );
846 } else {
847 update_post_meta( $post_id, '_sku', $new_sku );
848 }
849 } else {
850 update_post_meta( $post_id, '_sku', '' );
851 }
852 }
853
854 // Save Attributes
855 $attributes = array();
856
857 if ( isset( $_POST['attribute_names'] ) && isset( $_POST['attribute_values'] ) ) {
858
859 $attribute_names = $_POST['attribute_names'];
860 $attribute_values = $_POST['attribute_values'];
861
862 if ( isset( $_POST['attribute_visibility'] ) ) {
863 $attribute_visibility = $_POST['attribute_visibility'];
864 }
865
866 if ( isset( $_POST['attribute_variation'] ) ) {
867 $attribute_variation = $_POST['attribute_variation'];
868 }
869
870 $attribute_is_taxonomy = $_POST['attribute_is_taxonomy'];
871 $attribute_position = $_POST['attribute_position'];
872 $attribute_names_max_key = max( array_keys( $attribute_names ) );
873
874 for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) {
875 if ( empty( $attribute_names[ $i ] ) ) {
876 continue;
877 }
878
879 $is_visible = isset( $attribute_visibility[ $i ] ) ? 1 : 0;
880 $is_variation = isset( $attribute_variation[ $i ] ) ? 1 : 0;
881 $is_taxonomy = $attribute_is_taxonomy[ $i ] ? 1 : 0;
882
883 if ( $is_taxonomy ) {
884
885 $values_are_slugs = false;
886
887 if ( isset( $attribute_values[ $i ] ) ) {
888
889 // Select based attributes - Format values (posted values are slugs)
890 if ( is_array( $attribute_values[ $i ] ) ) {
891 $values = array_map( 'sanitize_title', $attribute_values[ $i ] );
892 $values_are_slugs = true;
893
894 // Text based attributes - Posted values are term names - don't change to slugs
895 } else {
896 $values = array_map( 'stripslashes', array_map( 'strip_tags', explode( WC_DELIMITER, $attribute_values[ $i ] ) ) );
897 }
898
899 // Remove empty items in the array
900 $values = array_filter( $values, 'strlen' );
901
902 } else {
903 $values = array();
904 }
905
906 // Update post terms
907 if ( taxonomy_exists( $attribute_names[ $i ] ) ) {
908
909 foreach( $values as $key => $value ) {
910 $term = get_term_by( $values_are_slugs ? 'slug' : 'name', trim( $value ), $attribute_names[ $i ] );
911
912 if ( $term ) {
913 $values[ $key ] = intval( $term->term_id );
914 } else {
915 $term = wp_insert_term( trim( $value ), $attribute_names[ $i ] );
916 if ( isset( $term->term_id ) ) {
917 $values[ $key ] = intval($term->term_id);
918 }
919 }
920 }
921
922 wp_set_object_terms( $post_id, $values, $attribute_names[ $i ] );
923 }
924
925 if ( ! empty( $values ) ) {
926 // Add attribute to array, but don't set values
927 $attributes[ sanitize_title( $attribute_names[ $i ] ) ] = array(
928 'name' => wc_clean( $attribute_names[ $i ] ),
929 'value' => '',
930 'position' => $attribute_position[ $i ],
931 'is_visible' => $is_visible,
932 'is_variation' => $is_variation,
933 'is_taxonomy' => $is_taxonomy
934 );
935 }
936
937 } elseif ( isset( $attribute_values[ $i ] ) ) {
938
939 // Text based, possibly separated by pipes (WC_DELIMITER). Preserve line breaks in non-variation attributes.
940 $values = $is_variation ? wc_clean( $attribute_values[ $i ] ) : implode( "\n", array_map( 'wc_clean', explode( "\n", $attribute_values[ $i ] ) ) );
941 $values = implode( ' ' . WC_DELIMITER . ' ', wc_get_text_attributes( $values ) );
942
943 // Custom attribute - Add attribute to array and set the values
944 $attributes[ sanitize_title( $attribute_names[ $i ] ) ] = array(
945 'name' => wc_clean( $attribute_names[ $i ] ),
946 'value' => $values,
947 'position' => $attribute_position[ $i ],
948 'is_visible' => $is_visible,
949 'is_variation' => $is_variation,
950 'is_taxonomy' => $is_taxonomy
951 );
952 }
953 }
954 }
955
956 uasort( $attributes, 'wc_product_attribute_uasort_comparison' );
957
958 /**
959 * Unset removed attributes by looping over previous values and
960 * unsetting the terms.
961 */
962 $old_attributes = array_filter( (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) ) );
963
964 if ( ! empty( $old_attributes ) ) {
965 foreach ( $old_attributes as $key => $value ) {
966 if ( empty( $attributes[ $key ] ) && ! empty( $value['is_taxonomy'] ) && taxonomy_exists( $key ) ) {
967 wp_set_object_terms( $post_id, array(), $key );
968 }
969 }
970 }
971
972 /**
973 * After removed attributes are unset, we can set the new attribute data.
974 */
975 update_post_meta( $post_id, '_product_attributes', $attributes );
976
977 // Sales and prices
978 if ( in_array( $product_type, array( 'variable', 'grouped' ) ) ) {
979
980 // Variable and grouped products have no prices
981 update_post_meta( $post_id, '_regular_price', '' );
982 update_post_meta( $post_id, '_sale_price', '' );
983 update_post_meta( $post_id, '_sale_price_dates_from', '' );
984 update_post_meta( $post_id, '_sale_price_dates_to', '' );
985
986 } else {
987 $date_from = (string) isset( $_POST['_sale_price_dates_from'] ) ? wc_clean( $_POST['_sale_price_dates_from'] ) : '';
988 $date_to = (string) isset( $_POST['_sale_price_dates_to'] ) ? wc_clean( $_POST['_sale_price_dates_to'] ) : '';
989 $regular_price = (string) isset( $_POST['_regular_price'] ) ? wc_clean( $_POST['_regular_price'] ) : '';
990 $sale_price = (string) isset( $_POST['_sale_price'] ) ? wc_clean( $_POST['_sale_price'] ) : '';
991
992 update_post_meta( $post_id, '_regular_price', '' === $regular_price ? '' : wc_format_decimal( $regular_price ) );
993 update_post_meta( $post_id, '_sale_price', '' === $sale_price ? '' : wc_format_decimal( $sale_price ) );
994
995 // Dates
996 update_post_meta( $post_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' );
997 update_post_meta( $post_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' );
998
999 if ( $date_to && ! $date_from ) {
1000 $date_from = date( 'Y-m-d' );
1001 update_post_meta( $post_id, '_sale_price_dates_from', strtotime( $date_from ) );
1002 }
1003
1004 // Update price if on sale
1005 if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) {
1006 update_post_meta( $post_id, '_price', wc_format_decimal( $sale_price ) );
1007 } elseif ( '' !== $sale_price && $date_from && strtotime( $date_from ) <= strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
1008 update_post_meta( $post_id, '_price', wc_format_decimal( $sale_price ) );
1009 } else {
1010 update_post_meta( $post_id, '_price', '' === $regular_price ? '' : wc_format_decimal( $regular_price ) );
1011 }
1012
1013 if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
1014 update_post_meta( $post_id, '_price', '' === $regular_price ? '' : wc_format_decimal( $regular_price ) );
1015 update_post_meta( $post_id, '_sale_price', '' );
1016 update_post_meta( $post_id, '_sale_price_dates_from', '' );
1017 update_post_meta( $post_id, '_sale_price_dates_to', '' );
1018 }
1019 }
1020
1021 // Update parent if grouped so price sorting works and stays in sync with the cheapest child
1022 if ( $post->post_parent > 0 || 'grouped' === $product_type || $_POST['previous_parent_id'] > 0 ) {
1023
1024 $clear_parent_ids = array();
1025
1026 if ( $post->post_parent > 0 ) {
1027 $clear_parent_ids[] = $post->post_parent;
1028 }
1029
1030 if ( 'grouped' === $product_type ) {
1031 $clear_parent_ids[] = $post_id;
1032 }
1033
1034 if ( $_POST['previous_parent_id'] > 0 ) {
1035 $clear_parent_ids[] = absint( $_POST['previous_parent_id'] );
1036 }
1037
1038 if ( ! empty( $clear_parent_ids ) ) {
1039 foreach ( $clear_parent_ids as $clear_id ) {
1040 $children_by_price = get_posts( array(
1041 'post_parent' => $clear_id,
1042 'orderby' => 'meta_value_num',
1043 'order' => 'asc',
1044 'meta_key' => '_price',
1045 'posts_per_page' => 1,
1046 'post_type' => 'product',
1047 'fields' => 'ids'
1048 ) );
1049
1050 if ( $children_by_price ) {
1051 foreach ( $children_by_price as $child ) {
1052 $child_price = get_post_meta( $child, '_price', true );
1053 update_post_meta( $clear_id, '_price', $child_price );
1054 }
1055 }
1056
1057 wc_delete_product_transients( $clear_id );
1058 }
1059 }
1060 }
1061
1062 // Sold Individually
1063 if ( ! empty( $_POST['_sold_individually'] ) ) {
1064 update_post_meta( $post_id, '_sold_individually', 'yes' );
1065 } else {
1066 update_post_meta( $post_id, '_sold_individually', '' );
1067 }
1068
1069 // Stock Data
1070 if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
1071
1072 $manage_stock = 'no';
1073 $backorders = 'no';
1074 $stock_status = wc_clean( $_POST['_stock_status'] );
1075
1076 if ( 'external' === $product_type ) {
1077
1078 $stock_status = 'instock';
1079
1080 } elseif ( 'variable' === $product_type ) {
1081
1082 // Stock status is always determined by children so sync later
1083 $stock_status = '';
1084
1085 if ( ! empty( $_POST['_manage_stock'] ) ) {
1086 $manage_stock = 'yes';
1087 $backorders = wc_clean( $_POST['_backorders'] );
1088 }
1089
1090 } elseif ( 'grouped' !== $product_type && ! empty( $_POST['_manage_stock'] ) ) {
1091 $manage_stock = 'yes';
1092 $backorders = wc_clean( $_POST['_backorders'] );
1093 }
1094
1095 update_post_meta( $post_id, '_manage_stock', $manage_stock );
1096 update_post_meta( $post_id, '_backorders', $backorders );
1097
1098 if ( $stock_status ) {
1099 wc_update_product_stock_status( $post_id, $stock_status );
1100 }
1101
1102 if ( ! empty( $_POST['_manage_stock'] ) ) {
1103 wc_update_product_stock( $post_id, wc_stock_amount( $_POST['_stock'] ) );
1104 } else {
1105 update_post_meta( $post_id, '_stock', '' );
1106 }
1107
1108 } elseif ( 'variable' !== $product_type ) {
1109 wc_update_product_stock_status( $post_id, wc_clean( $_POST['_stock_status'] ) );
1110 }
1111
1112 // Cross sells and upsells
1113 $upsells = isset( $_POST['upsell_ids'] ) ? array_filter( array_map( 'intval', explode( ',', $_POST['upsell_ids'] ) ) ) : array();
1114 $crosssells = isset( $_POST['crosssell_ids'] ) ? array_filter( array_map( 'intval', explode( ',', $_POST['crosssell_ids'] ) ) ) : array();
1115
1116 update_post_meta( $post_id, '_upsell_ids', $upsells );
1117 update_post_meta( $post_id, '_crosssell_ids', $crosssells );
1118
1119 // Downloadable options
1120 if ( 'yes' == $is_downloadable ) {
1121
1122 $_download_limit = absint( $_POST['_download_limit'] );
1123 if ( ! $_download_limit ) {
1124 $_download_limit = ''; // 0 or blank = unlimited
1125 }
1126
1127 $_download_expiry = absint( $_POST['_download_expiry'] );
1128 if ( ! $_download_expiry ) {
1129 $_download_expiry = ''; // 0 or blank = unlimited
1130 }
1131
1132 // file paths will be stored in an array keyed off md5(file path)
1133 $files = array();
1134
1135 if ( isset( $_POST['_wc_file_urls'] ) ) {
1136 $file_names = isset( $_POST['_wc_file_names'] ) ? $_POST['_wc_file_names'] : array();
1137 $file_urls = isset( $_POST['_wc_file_urls'] ) ? wp_unslash( array_map( 'trim', $_POST['_wc_file_urls'] ) ) : array();
1138 $file_url_size = sizeof( $file_urls );
1139 $allowed_file_types = apply_filters( 'woocommerce_downloadable_file_allowed_mime_types', get_allowed_mime_types() );
1140
1141 for ( $i = 0; $i < $file_url_size; $i ++ ) {
1142 if ( ! empty( $file_urls[ $i ] ) ) {
1143 // Find type and file URL
1144 if ( 0 === strpos( $file_urls[ $i ], 'http' ) ) {
1145 $file_is = 'absolute';
1146 $file_url = esc_url_raw( $file_urls[ $i ] );
1147 } elseif ( '[' === substr( $file_urls[ $i ], 0, 1 ) && ']' === substr( $file_urls[ $i ], -1 ) ) {
1148 $file_is = 'shortcode';
1149 $file_url = wc_clean( $file_urls[ $i ] );
1150 } else {
1151 $file_is = 'relative';
1152 $file_url = wc_clean( $file_urls[ $i ] );
1153 }
1154
1155 $file_name = wc_clean( $file_names[ $i ] );
1156 $file_hash = md5( $file_url );
1157
1158 // Validate the file extension
1159 if ( in_array( $file_is, array( 'absolute', 'relative' ) ) ) {
1160 $file_type = wp_check_filetype( strtok( $file_url, '?' ), $allowed_file_types );
1161 $parsed_url = parse_url( $file_url, PHP_URL_PATH );
1162 $extension = pathinfo( $parsed_url, PATHINFO_EXTENSION );
1163
1164 if ( ! empty( $extension ) && ! in_array( $file_type['type'], $allowed_file_types ) ) {
1165 WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The downloadable file %s cannot be used as it does not have an allowed file type. Allowed types include: %s', 'woocommerce' ), '<code>' . basename( $file_url ) . '</code>', '<code>' . implode( ', ', array_keys( $allowed_file_types ) ) . '</code>' ) );
1166 continue;
1167 }
1168 }
1169
1170 // Validate the file exists
1171 if ( 'relative' === $file_is ) {
1172 $_file_url = $file_url;
1173 if ( '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 ) ) {
1174 $_file_url = realpath( ABSPATH . $file_url );
1175 }
1176
1177 if ( ! apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $_file_url ), $file_url ) ) {
1178 WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce' ), '<code>' . $file_url . '</code>' ) );
1179 continue;
1180 }
1181 }
1182
1183 $files[ $file_hash ] = array(
1184 'name' => $file_name,
1185 'file' => $file_url
1186 );
1187 }
1188 }
1189 }
1190
1191 // grant permission to any newly added files on any existing orders for this product prior to saving
1192 do_action( 'woocommerce_process_product_file_download_paths', $post_id, 0, $files );
1193
1194 update_post_meta( $post_id, '_downloadable_files', $files );
1195 update_post_meta( $post_id, '_download_limit', $_download_limit );
1196 update_post_meta( $post_id, '_download_expiry', $_download_expiry );
1197
1198 if ( isset( $_POST['_download_type'] ) ) {
1199 update_post_meta( $post_id, '_download_type', wc_clean( $_POST['_download_type'] ) );
1200 }
1201 }
1202
1203 // Product url
1204 if ( 'external' == $product_type ) {
1205
1206 if ( isset( $_POST['_product_url'] ) ) {
1207 update_post_meta( $post_id, '_product_url', esc_url_raw( $_POST['_product_url'] ) );
1208 }
1209
1210 if ( isset( $_POST['_button_text'] ) ) {
1211 update_post_meta( $post_id, '_button_text', wc_clean( $_POST['_button_text'] ) );
1212 }
1213 }
1214
1215 // Save variations
1216 if ( 'variable' == $product_type ) {
1217 // Update parent if variable so price sorting works and stays in sync with the cheapest child
1218 WC_Product_Variable::sync( $post_id );
1219 WC_Product_Variable::sync_stock_status( $post_id );
1220 }
1221
1222 // Update version after saving
1223 update_post_meta( $post_id, '_product_version', WC_VERSION );
1224
1225 // Do action for product type
1226 do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id );
1227
1228 // Clear cache/transients
1229 wc_delete_product_transients( $post_id );
1230 }
1231
1232 /**
1233 * Save meta box data.
1234 *
1235 * @param int $post_id
1236 * @param WP_Post $post
1237 */
1238 public static function save_variations( $post_id, $post ) {
1239 global $wpdb;
1240
1241 $attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) );
1242
1243 if ( isset( $_POST['variable_sku'] ) ) {
1244 $variable_post_id = $_POST['variable_post_id'];
1245 $variable_sku = $_POST['variable_sku'];
1246 $variable_regular_price = $_POST['variable_regular_price'];
1247 $variable_sale_price = $_POST['variable_sale_price'];
1248 $upload_image_id = $_POST['upload_image_id'];
1249 $variable_download_limit = $_POST['variable_download_limit'];
1250 $variable_download_expiry = $_POST['variable_download_expiry'];
1251 $variable_shipping_class = $_POST['variable_shipping_class'];
1252 $variable_tax_class = isset( $_POST['variable_tax_class'] ) ? $_POST['variable_tax_class'] : array();
1253 $variable_menu_order = $_POST['variation_menu_order'];
1254 $variable_sale_price_dates_from = $_POST['variable_sale_price_dates_from'];
1255 $variable_sale_price_dates_to = $_POST['variable_sale_price_dates_to'];
1256
1257 $variable_weight = isset( $_POST['variable_weight'] ) ? $_POST['variable_weight'] : array();
1258 $variable_length = isset( $_POST['variable_length'] ) ? $_POST['variable_length'] : array();
1259 $variable_width = isset( $_POST['variable_width'] ) ? $_POST['variable_width'] : array();
1260 $variable_height = isset( $_POST['variable_height'] ) ? $_POST['variable_height'] : array();
1261 $variable_enabled = isset( $_POST['variable_enabled'] ) ? $_POST['variable_enabled'] : array();
1262 $variable_is_virtual = isset( $_POST['variable_is_virtual'] ) ? $_POST['variable_is_virtual'] : array();
1263 $variable_is_downloadable = isset( $_POST['variable_is_downloadable'] ) ? $_POST['variable_is_downloadable'] : array();
1264
1265 $variable_manage_stock = isset( $_POST['variable_manage_stock'] ) ? $_POST['variable_manage_stock'] : array();
1266 $variable_stock = isset( $_POST['variable_stock'] ) ? $_POST['variable_stock'] : array();
1267 $variable_backorders = isset( $_POST['variable_backorders'] ) ? $_POST['variable_backorders'] : array();
1268 $variable_stock_status = isset( $_POST['variable_stock_status'] ) ? $_POST['variable_stock_status'] : array();
1269
1270 $variable_description = isset( $_POST['variable_description'] ) ? $_POST['variable_description'] : array();
1271
1272 $max_loop = max( array_keys( $_POST['variable_post_id'] ) );
1273
1274 for ( $i = 0; $i <= $max_loop; $i ++ ) {
1275
1276 if ( ! isset( $variable_post_id[ $i ] ) ) {
1277 continue;
1278 }
1279
1280 $variation_id = absint( $variable_post_id[ $i ] );
1281
1282 // Checkboxes
1283 $is_virtual = isset( $variable_is_virtual[ $i ] ) ? 'yes' : 'no';
1284 $is_downloadable = isset( $variable_is_downloadable[ $i ] ) ? 'yes' : 'no';
1285 $post_status = isset( $variable_enabled[ $i ] ) ? 'publish' : 'private';
1286 $manage_stock = isset( $variable_manage_stock[ $i ] ) ? 'yes' : 'no';
1287
1288 // Generate a useful post title
1289 $variation_post_title = sprintf( __( 'Variation #%s of %s', 'woocommerce' ), absint( $variation_id ), esc_html( get_the_title( $post_id ) ) );
1290
1291 // Update or Add post
1292 if ( ! $variation_id ) {
1293
1294 $variation = array(
1295 'post_title' => $variation_post_title,
1296 'post_content' => '',
1297 'post_status' => $post_status,
1298 'post_author' => get_current_user_id(),
1299 'post_parent' => $post_id,
1300 'post_type' => 'product_variation',
1301 'menu_order' => $variable_menu_order[ $i ]
1302 );
1303
1304 $variation_id = wp_insert_post( $variation );
1305
1306 do_action( 'woocommerce_create_product_variation', $variation_id );
1307
1308 } else {
1309
1310 $modified_date = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) );
1311
1312 $wpdb->update( $wpdb->posts, array(
1313 'post_status' => $post_status,
1314 'post_title' => $variation_post_title,
1315 'menu_order' => $variable_menu_order[ $i ],
1316 'post_modified' => $modified_date,
1317 'post_modified_gmt' => get_gmt_from_date( $modified_date )
1318 ), array( 'ID' => $variation_id ) );
1319
1320 clean_post_cache( $variation_id );
1321
1322 do_action( 'woocommerce_update_product_variation', $variation_id );
1323
1324 }
1325
1326 // Only continue if we have a variation ID
1327 if ( ! $variation_id ) {
1328 continue;
1329 }
1330
1331 // Unique SKU
1332 $sku = get_post_meta( $variation_id, '_sku', true );
1333 $new_sku = wc_clean( $variable_sku[ $i ] );
1334
1335 if ( '' == $new_sku ) {
1336 update_post_meta( $variation_id, '_sku', '' );
1337 } elseif ( $new_sku !== $sku ) {
1338 if ( ! empty( $new_sku ) ) {
1339 $unique_sku = wc_product_has_unique_sku( $variation_id, $new_sku );
1340
1341 if ( ! $unique_sku ) {
1342 WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%s – Variation SKU must be unique.', 'woocommerce' ), $variation_id ) );
1343 } else {
1344 update_post_meta( $variation_id, '_sku', $new_sku );
1345 }
1346 } else {
1347 update_post_meta( $variation_id, '_sku', '' );
1348 }
1349 }
1350
1351 // Update post meta
1352 update_post_meta( $variation_id, '_thumbnail_id', absint( $upload_image_id[ $i ] ) );
1353 update_post_meta( $variation_id, '_virtual', wc_clean( $is_virtual ) );
1354 update_post_meta( $variation_id, '_downloadable', wc_clean( $is_downloadable ) );
1355
1356 if ( isset( $variable_weight[ $i ] ) ) {
1357 update_post_meta( $variation_id, '_weight', ( '' === $variable_weight[ $i ] ) ? '' : wc_format_decimal( $variable_weight[ $i ] ) );
1358 }
1359
1360 if ( isset( $variable_length[ $i ] ) ) {
1361 update_post_meta( $variation_id, '_length', ( '' === $variable_length[ $i ] ) ? '' : wc_format_decimal( $variable_length[ $i ] ) );
1362 }
1363
1364 if ( isset( $variable_width[ $i ] ) ) {
1365 update_post_meta( $variation_id, '_width', ( '' === $variable_width[ $i ] ) ? '' : wc_format_decimal( $variable_width[ $i ] ) );
1366 }
1367
1368 if ( isset( $variable_height[ $i ] ) ) {
1369 update_post_meta( $variation_id, '_height', ( '' === $variable_height[ $i ] ) ? '' : wc_format_decimal( $variable_height[ $i ] ) );
1370 }
1371
1372 // Stock handling
1373 update_post_meta( $variation_id, '_manage_stock', $manage_stock );
1374
1375 if ( 'yes' === $manage_stock ) {
1376 update_post_meta( $variation_id, '_backorders', wc_clean( $variable_backorders[ $i ] ) );
1377 wc_update_product_stock( $variation_id, wc_stock_amount( $variable_stock[ $i ] ) );
1378 } else {
1379 delete_post_meta( $variation_id, '_backorders' );
1380 delete_post_meta( $variation_id, '_stock' );
1381 }
1382
1383 // Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level
1384 if ( ! empty( $variable_stock_status[ $i ] ) ) {
1385 wc_update_product_stock_status( $variation_id, $variable_stock_status[ $i ] );
1386 }
1387
1388 // Price handling
1389 _wc_save_product_price( $variation_id, $variable_regular_price[ $i ], $variable_sale_price[ $i ], $variable_sale_price_dates_from[ $i ], $variable_sale_price_dates_to[ $i ] );
1390
1391 if ( isset( $variable_tax_class[ $i ] ) && $variable_tax_class[ $i ] !== 'parent' ) {
1392 update_post_meta( $variation_id, '_tax_class', wc_clean( $variable_tax_class[ $i ] ) );
1393 } else {
1394 delete_post_meta( $variation_id, '_tax_class' );
1395 }
1396
1397 if ( 'yes' == $is_downloadable ) {
1398 update_post_meta( $variation_id, '_download_limit', wc_clean( $variable_download_limit[ $i ] ) );
1399 update_post_meta( $variation_id, '_download_expiry', wc_clean( $variable_download_expiry[ $i ] ) );
1400
1401 $files = array();
1402 $file_names = isset( $_POST['_wc_variation_file_names'][ $variation_id ] ) ? array_map( 'wc_clean', $_POST['_wc_variation_file_names'][ $variation_id ] ) : array();
1403 $file_urls = isset( $_POST['_wc_variation_file_urls'][ $variation_id ] ) ? array_map( 'wc_clean', $_POST['_wc_variation_file_urls'][ $variation_id ] ) : array();
1404 $file_url_size = sizeof( $file_urls );
1405 $allowed_file_types = get_allowed_mime_types();
1406
1407 for ( $ii = 0; $ii < $file_url_size; $ii ++ ) {
1408 if ( ! empty( $file_urls[ $ii ] ) ) {
1409 // Find type and file URL
1410 if ( 0 === strpos( $file_urls[ $ii ], 'http' ) ) {
1411 $file_is = 'absolute';
1412 $file_url = esc_url_raw( $file_urls[ $ii ] );
1413 } elseif ( '[' === substr( $file_urls[ $ii ], 0, 1 ) && ']' === substr( $file_urls[ $ii ], -1 ) ) {
1414 $file_is = 'shortcode';
1415 $file_url = wc_clean( $file_urls[ $ii ] );
1416 } else {
1417 $file_is = 'relative';
1418 $file_url = wc_clean( $file_urls[ $ii ] );
1419 }
1420
1421 $file_name = wc_clean( $file_names[ $ii ] );
1422 $file_hash = md5( $file_url );
1423
1424 // Validate the file extension
1425 if ( in_array( $file_is, array( 'absolute', 'relative' ) ) ) {
1426 $file_type = wp_check_filetype( strtok( $file_url, '?' ), $allowed_file_types );
1427 $parsed_url = parse_url( $file_url, PHP_URL_PATH );
1428 $extension = pathinfo( $parsed_url, PATHINFO_EXTENSION );
1429
1430 if ( ! empty( $extension ) && ! in_array( $file_type['type'], $allowed_file_types ) ) {
1431 WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%s – The downloadable file %s cannot be used as it does not have an allowed file type. Allowed types include: %s', 'woocommerce' ), $variation_id, '<code>' . basename( $file_url ) . '</code>', '<code>' . implode( ', ', array_keys( $allowed_file_types ) ) . '</code>' ) );
1432 continue;
1433 }
1434 }
1435
1436 // Validate the file exists
1437 if ( 'relative' === $file_is && ! apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $file_url ) ) {
1438 WC_Admin_Meta_Boxes::add_error( sprintf( __( '#%s – The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce' ), $variation_id, '<code>' . $file_url . '</code>' ) );
1439 continue;
1440 }
1441
1442 $files[ $file_hash ] = array(
1443 'name' => $file_name,
1444 'file' => $file_url
1445 );
1446 }
1447 }
1448
1449 // grant permission to any newly added files on any existing orders for this product prior to saving
1450 do_action( 'woocommerce_process_product_file_download_paths', $post_id, $variation_id, $files );
1451
1452 update_post_meta( $variation_id, '_downloadable_files', $files );
1453 } else {
1454 update_post_meta( $variation_id, '_download_limit', '' );
1455 update_post_meta( $variation_id, '_download_expiry', '' );
1456 update_post_meta( $variation_id, '_downloadable_files', '' );
1457 }
1458
1459 update_post_meta( $variation_id, '_variation_description', wp_kses_post( $variable_description[ $i ] ) );
1460
1461 // Save shipping class
1462 $variable_shipping_class[ $i ] = ! empty( $variable_shipping_class[ $i ] ) ? (int) $variable_shipping_class[ $i ] : '';
1463 wp_set_object_terms( $variation_id, $variable_shipping_class[ $i ], 'product_shipping_class');
1464
1465 // Update Attributes
1466 $updated_attribute_keys = array();
1467 foreach ( $attributes as $attribute ) {
1468 if ( $attribute['is_variation'] ) {
1469 $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] );
1470 $updated_attribute_keys[] = $attribute_key;
1471
1472 if ( $attribute['is_taxonomy'] ) {
1473 // Don't use wc_clean as it destroys sanitized characters
1474 $value = isset( $_POST[ $attribute_key ][ $i ] ) ? sanitize_title( stripslashes( $_POST[ $attribute_key ][ $i ] ) ) : '';
1475 } else {
1476 $value = isset( $_POST[ $attribute_key ][ $i ] ) ? wc_clean( stripslashes( $_POST[ $attribute_key ][ $i ] ) ) : '';
1477 }
1478
1479 update_post_meta( $variation_id, $attribute_key, $value );
1480 }
1481 }
1482
1483 // Remove old taxonomies attributes so data is kept up to date - first get attribute key names
1484 $delete_attribute_keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode( "','", $updated_attribute_keys ) . "' ) AND post_id = %d;", $variation_id ) );
1485
1486 foreach ( $delete_attribute_keys as $key ) {
1487 delete_post_meta( $variation_id, $key );
1488 }
1489
1490 do_action( 'woocommerce_save_product_variation', $variation_id, $i );
1491 }
1492 }
1493
1494 // Update parent if variable so price sorting works and stays in sync with the cheapest child
1495 WC_Product_Variable::sync( $post_id );
1496
1497 // Update default attribute options setting
1498 $default_attributes = array();
1499
1500 foreach ( $attributes as $attribute ) {
1501
1502 if ( $attribute['is_variation'] ) {
1503 $value = '';
1504
1505 if ( isset( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) {
1506 if ( $attribute['is_taxonomy'] ) {
1507 // Don't use wc_clean as it destroys sanitized characters
1508 $value = sanitize_title( trim( stripslashes( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) );
1509 } else {
1510 $value = wc_clean( trim( stripslashes( $_POST[ 'default_attribute_' . sanitize_title( $attribute['name'] ) ] ) ) );
1511 }
1512 }
1513
1514 if ( $value ) {
1515 $default_attributes[ sanitize_title( $attribute['name'] ) ] = $value;
1516 }
1517 }
1518 }
1519
1520 update_post_meta( $post_id, '_default_attributes', $default_attributes );
1521 }
1522}