api-keys.js
5 years ago
api-keys.min.js
5 years ago
backbone-modal.js
5 years ago
backbone-modal.min.js
5 years ago
marketplace-suggestions.js
4 years ago
marketplace-suggestions.min.js
3 years ago
meta-boxes-coupon.js
5 years ago
meta-boxes-coupon.min.js
3 years ago
meta-boxes-order.js
3 years ago
meta-boxes-order.min.js
3 years ago
meta-boxes-product-variation.js
3 years ago
meta-boxes-product-variation.min.js
3 years ago
meta-boxes-product.js
3 years ago
meta-boxes-product.min.js
3 years ago
meta-boxes.js
3 years ago
meta-boxes.min.js
3 years ago
network-orders.js
8 years ago
network-orders.min.js
3 years ago
product-editor.js
3 years ago
product-editor.min.js
3 years ago
product-ordering.js
3 years ago
product-ordering.min.js
3 years ago
quick-edit.js
4 years ago
quick-edit.min.js
3 years ago
reports.js
5 years ago
reports.min.js
3 years ago
settings-views-html-settings-tax.js
5 years ago
settings-views-html-settings-tax.min.js
3 years ago
settings.js
4 years ago
settings.min.js
3 years ago
system-status.js
3 years ago
system-status.min.js
3 years ago
term-ordering.js
4 years ago
term-ordering.min.js
4 years ago
users.js
5 years ago
users.min.js
3 years ago
wc-clipboard.js
5 years ago
wc-clipboard.min.js
5 years ago
wc-enhanced-select.js
3 years ago
wc-enhanced-select.min.js
3 years ago
wc-orders.js
3 years ago
wc-orders.min.js
3 years ago
wc-product-export.js
5 years ago
wc-product-export.min.js
5 years ago
wc-product-import.js
3 years ago
wc-product-import.min.js
3 years ago
wc-setup.js
5 years ago
wc-setup.min.js
3 years ago
wc-shipping-classes.js
5 years ago
wc-shipping-classes.min.js
3 years ago
wc-shipping-zone-methods.js
4 years ago
wc-shipping-zone-methods.min.js
3 years ago
wc-shipping-zones.js
5 years ago
wc-shipping-zones.min.js
3 years ago
wc-status-widget.js
4 years ago
wc-status-widget.min.js
4 years ago
woocommerce_admin.js
3 years ago
woocommerce_admin.min.js
3 years ago
meta-boxes-product-variation.js
1637 lines
| 1 | /* global wp, woocommerce_admin_meta_boxes_variations, woocommerce_admin, accounting */ |
| 2 | jQuery( function ( $ ) { |
| 3 | 'use strict'; |
| 4 | |
| 5 | /** |
| 6 | * Variations actions |
| 7 | */ |
| 8 | var wc_meta_boxes_product_variations_actions = { |
| 9 | /** |
| 10 | * Initialize variations actions |
| 11 | */ |
| 12 | init: function () { |
| 13 | $( '#variable_product_options' ) |
| 14 | .on( |
| 15 | 'change', |
| 16 | 'input.variable_is_downloadable', |
| 17 | this.variable_is_downloadable |
| 18 | ) |
| 19 | .on( |
| 20 | 'change', |
| 21 | 'input.variable_is_virtual', |
| 22 | this.variable_is_virtual |
| 23 | ) |
| 24 | .on( |
| 25 | 'change', |
| 26 | 'input.variable_manage_stock', |
| 27 | this.variable_manage_stock |
| 28 | ) |
| 29 | .on( 'click', 'button.notice-dismiss', this.notice_dismiss ) |
| 30 | .on( 'click', 'h3 .sort', this.set_menu_order ) |
| 31 | .on( |
| 32 | 'click', |
| 33 | 'button.add_price_for_variations', |
| 34 | this.open_modal_to_set_variations_price |
| 35 | ) |
| 36 | .on( 'reload', this.reload ) |
| 37 | .on( |
| 38 | 'click', |
| 39 | 'button.create-variations', |
| 40 | this.create_variations |
| 41 | ); |
| 42 | |
| 43 | $( |
| 44 | 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock' |
| 45 | ).trigger( 'change' ); |
| 46 | $( '#woocommerce-product-data' ).on( |
| 47 | 'woocommerce_variations_loaded', |
| 48 | this.variations_loaded |
| 49 | ); |
| 50 | $( document.body ) |
| 51 | .on( 'woocommerce_variations_added', this.variation_added ) |
| 52 | .on( |
| 53 | 'keyup', |
| 54 | '.wc_input_variations_price', |
| 55 | this.maybe_enable_button_to_add_price_to_variations |
| 56 | ); |
| 57 | }, |
| 58 | |
| 59 | create_variations: function () { |
| 60 | var new_attribute_data = $( |
| 61 | '.woocommerce_variation_new_attribute_data' |
| 62 | ); |
| 63 | |
| 64 | $( '#variable_product_options' ).block( { |
| 65 | message: null, |
| 66 | overlayCSS: { |
| 67 | background: '#fff', |
| 68 | opacity: 0.6, |
| 69 | }, |
| 70 | } ); |
| 71 | |
| 72 | var original_data = new_attribute_data.find( |
| 73 | 'input, select, textarea' |
| 74 | ); |
| 75 | |
| 76 | var data = { |
| 77 | post_id: woocommerce_admin_meta_boxes.post_id, |
| 78 | product_type: $( '#product-type' ).val(), |
| 79 | data: original_data.serialize(), |
| 80 | action: 'woocommerce_add_attributes_and_variations', |
| 81 | security: |
| 82 | woocommerce_admin_meta_boxes.add_attributes_and_variations, |
| 83 | }; |
| 84 | |
| 85 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function ( |
| 86 | response |
| 87 | ) { |
| 88 | if ( response.error ) { |
| 89 | // Error. |
| 90 | window.alert( response.error ); |
| 91 | $( '#variable_product_options' ).unblock(); |
| 92 | } else if ( response ) { |
| 93 | // Reload variations and attributes panel. |
| 94 | var this_page_url = window.location.toString(); |
| 95 | this_page_url = this_page_url.replace( |
| 96 | 'post-new.php?', |
| 97 | 'post.php?post=' + |
| 98 | woocommerce_admin_meta_boxes.post_id + |
| 99 | '&action=edit&' |
| 100 | ); |
| 101 | |
| 102 | $.get( this_page_url, function ( response ) { |
| 103 | $( '#variable_product_options' ).unblock(); |
| 104 | $( '#variable_product_options_inner' ).replaceWith( |
| 105 | $( response ).find( |
| 106 | '#variable_product_options_inner' |
| 107 | ) |
| 108 | ); |
| 109 | $( '#variable_product_options' ).trigger( 'reload' ); |
| 110 | $( |
| 111 | '#product_attributes > .product_attributes' |
| 112 | ).replaceWith( |
| 113 | $( response ).find( |
| 114 | '#product_attributes > .product_attributes' |
| 115 | ) |
| 116 | ); |
| 117 | } ); |
| 118 | } |
| 119 | } ); |
| 120 | }, |
| 121 | |
| 122 | /** |
| 123 | * Reload UI |
| 124 | * |
| 125 | * @param {Object} event |
| 126 | * @param {Int} qty |
| 127 | */ |
| 128 | reload: function () { |
| 129 | wc_meta_boxes_product_variations_ajax.load_variations( 1 ); |
| 130 | wc_meta_boxes_product_variations_pagenav.set_paginav( 0 ); |
| 131 | }, |
| 132 | |
| 133 | /** |
| 134 | * Check if variation is downloadable and show/hide elements |
| 135 | */ |
| 136 | variable_is_downloadable: function () { |
| 137 | $( this ) |
| 138 | .closest( '.woocommerce_variation' ) |
| 139 | .find( '.show_if_variation_downloadable' ) |
| 140 | .hide(); |
| 141 | |
| 142 | if ( $( this ).is( ':checked' ) ) { |
| 143 | $( this ) |
| 144 | .closest( '.woocommerce_variation' ) |
| 145 | .find( '.show_if_variation_downloadable' ) |
| 146 | .show(); |
| 147 | } |
| 148 | }, |
| 149 | |
| 150 | /** |
| 151 | * Check if variation is virtual and show/hide elements |
| 152 | */ |
| 153 | variable_is_virtual: function () { |
| 154 | $( this ) |
| 155 | .closest( '.woocommerce_variation' ) |
| 156 | .find( '.hide_if_variation_virtual' ) |
| 157 | .show(); |
| 158 | |
| 159 | if ( $( this ).is( ':checked' ) ) { |
| 160 | $( this ) |
| 161 | .closest( '.woocommerce_variation' ) |
| 162 | .find( '.hide_if_variation_virtual' ) |
| 163 | .hide(); |
| 164 | } |
| 165 | }, |
| 166 | |
| 167 | /** |
| 168 | * Maybe enable the button to add a price for every variation |
| 169 | */ |
| 170 | maybe_enable_button_to_add_price_to_variations: function () { |
| 171 | var variation_price = parseInt( |
| 172 | $( '.wc_input_variations_price' ).val(), |
| 173 | 10 |
| 174 | ); |
| 175 | if ( isNaN( variation_price ) ) { |
| 176 | $( '.add_variations_price_button' ).prop( 'disabled', true ); |
| 177 | } else { |
| 178 | $( '.add_variations_price_button' ).prop( 'disabled', false ); |
| 179 | } |
| 180 | }, |
| 181 | |
| 182 | /** |
| 183 | * Check if variation manage stock and show/hide elements |
| 184 | */ |
| 185 | variable_manage_stock: function () { |
| 186 | $( this ) |
| 187 | .closest( '.woocommerce_variation' ) |
| 188 | .find( '.show_if_variation_manage_stock' ) |
| 189 | .hide(); |
| 190 | $( this ) |
| 191 | .closest( '.woocommerce_variation' ) |
| 192 | .find( '.variable_stock_status' ) |
| 193 | .show(); |
| 194 | |
| 195 | if ( $( this ).is( ':checked' ) ) { |
| 196 | $( this ) |
| 197 | .closest( '.woocommerce_variation' ) |
| 198 | .find( '.show_if_variation_manage_stock' ) |
| 199 | .show(); |
| 200 | $( this ) |
| 201 | .closest( '.woocommerce_variation' ) |
| 202 | .find( '.variable_stock_status' ) |
| 203 | .hide(); |
| 204 | } |
| 205 | |
| 206 | // Parent level. |
| 207 | if ( $( 'input#_manage_stock:checked' ).length ) { |
| 208 | $( this ) |
| 209 | .closest( '.woocommerce_variation' ) |
| 210 | .find( '.variable_stock_status' ) |
| 211 | .hide(); |
| 212 | } |
| 213 | }, |
| 214 | |
| 215 | /** |
| 216 | * Notice dismiss |
| 217 | */ |
| 218 | notice_dismiss: function () { |
| 219 | $( this ).closest( 'div.notice' ).remove(); |
| 220 | }, |
| 221 | |
| 222 | /** |
| 223 | * Run actions when variations is loaded |
| 224 | * |
| 225 | * @param {Object} event |
| 226 | * @param {Int} needsUpdate |
| 227 | */ |
| 228 | variations_loaded: function ( event, needsUpdate ) { |
| 229 | needsUpdate = needsUpdate || false; |
| 230 | |
| 231 | var wrapper = $( '#woocommerce-product-data' ); |
| 232 | |
| 233 | if ( ! needsUpdate ) { |
| 234 | // Show/hide downloadable, virtual and stock fields |
| 235 | $( |
| 236 | 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock', |
| 237 | wrapper |
| 238 | ).trigger( 'change' ); |
| 239 | |
| 240 | // Open sale schedule fields when have some sale price date |
| 241 | $( '.woocommerce_variation', wrapper ).each( function ( |
| 242 | index, |
| 243 | el |
| 244 | ) { |
| 245 | var $el = $( el ), |
| 246 | date_from = $( '.sale_price_dates_from', $el ).val(), |
| 247 | date_to = $( '.sale_price_dates_to', $el ).val(); |
| 248 | |
| 249 | if ( '' !== date_from || '' !== date_to ) { |
| 250 | $( 'a.sale_schedule', $el ).trigger( 'click' ); |
| 251 | } |
| 252 | } ); |
| 253 | |
| 254 | // Remove variation-needs-update classes |
| 255 | $( |
| 256 | '.woocommerce_variations .variation-needs-update', |
| 257 | wrapper |
| 258 | ).removeClass( 'variation-needs-update' ); |
| 259 | |
| 260 | // Disable cancel and save buttons |
| 261 | $( |
| 262 | 'button.cancel-variation-changes, button.save-variation-changes', |
| 263 | wrapper |
| 264 | ).attr( 'disabled', 'disabled' ); |
| 265 | } |
| 266 | |
| 267 | // Init TipTip |
| 268 | $( '#tiptip_holder' ).removeAttr( 'style' ); |
| 269 | $( '#tiptip_arrow' ).removeAttr( 'style' ); |
| 270 | $( |
| 271 | '.woocommerce_variations .tips, ' + |
| 272 | '.woocommerce_variations .help_tip, ' + |
| 273 | '.woocommerce_variations .woocommerce-help-tip, ' + |
| 274 | '.toolbar-variations-defaults .woocommerce-help-tip', |
| 275 | wrapper |
| 276 | ).tipTip( { |
| 277 | attribute: 'data-tip', |
| 278 | fadeIn: 50, |
| 279 | fadeOut: 50, |
| 280 | delay: 200, |
| 281 | } ); |
| 282 | |
| 283 | // Datepicker fields |
| 284 | $( '.sale_price_dates_fields', wrapper ) |
| 285 | .find( 'input' ) |
| 286 | .datepicker( { |
| 287 | defaultDate: '', |
| 288 | dateFormat: 'yy-mm-dd', |
| 289 | numberOfMonths: 1, |
| 290 | showButtonPanel: true, |
| 291 | onSelect: function () { |
| 292 | var option = $( this ).is( '.sale_price_dates_from' ) |
| 293 | ? 'minDate' |
| 294 | : 'maxDate', |
| 295 | dates = $( this ) |
| 296 | .closest( '.sale_price_dates_fields' ) |
| 297 | .find( 'input' ), |
| 298 | date = $( this ).datepicker( 'getDate' ); |
| 299 | |
| 300 | dates.not( this ).datepicker( 'option', option, date ); |
| 301 | $( this ).trigger( 'change' ); |
| 302 | }, |
| 303 | } ); |
| 304 | |
| 305 | // Allow sorting |
| 306 | $( '.woocommerce_variations', wrapper ).sortable( { |
| 307 | items: '.woocommerce_variation', |
| 308 | cursor: 'move', |
| 309 | axis: 'y', |
| 310 | handle: '.sort', |
| 311 | scrollSensitivity: 40, |
| 312 | forcePlaceholderSize: true, |
| 313 | helper: 'clone', |
| 314 | opacity: 0.65, |
| 315 | stop: function () { |
| 316 | wc_meta_boxes_product_variations_actions.variation_row_indexes(); |
| 317 | }, |
| 318 | } ); |
| 319 | |
| 320 | $( document.body ).trigger( 'wc-enhanced-select-init' ); |
| 321 | }, |
| 322 | |
| 323 | /** |
| 324 | * Run actions when added a variation |
| 325 | * |
| 326 | * @param {Object} event |
| 327 | * @param {Int} qty |
| 328 | */ |
| 329 | variation_added: function ( event, qty ) { |
| 330 | if ( 1 === qty ) { |
| 331 | wc_meta_boxes_product_variations_actions.variations_loaded( |
| 332 | null, |
| 333 | true |
| 334 | ); |
| 335 | } |
| 336 | }, |
| 337 | |
| 338 | /** |
| 339 | * Sets a price for every variation |
| 340 | */ |
| 341 | set_variations_price: function () { |
| 342 | var variation_price = $( '.wc_input_variations_price' ).val(); |
| 343 | var product_type = $( 'select#product-type' ).val(); |
| 344 | var input_type = |
| 345 | 'variable-subscription' === product_type |
| 346 | ? 'variable_subscription_sign_up_fee' |
| 347 | : 'variable_regular_price'; |
| 348 | var input = $( `.wc_input_price[name^=${ input_type }]` ); |
| 349 | |
| 350 | // We don't want to override prices already set |
| 351 | input.each( function ( index, el ) { |
| 352 | if ( '0' === $( el ).val() || '' === $( el ).val() ) { |
| 353 | $( el ).val( variation_price ).trigger( 'change' ); |
| 354 | } |
| 355 | } ); |
| 356 | wc_meta_boxes_product_variations_ajax.save_variations(); |
| 357 | }, |
| 358 | |
| 359 | /** |
| 360 | * Opens the modal to set a price for every variation |
| 361 | */ |
| 362 | open_modal_to_set_variations_price: function () { |
| 363 | $( this ).WCBackboneModal( { |
| 364 | template: 'wc-modal-set-price-variations', |
| 365 | } ); |
| 366 | $( '.add_variations_price_button' ).on( |
| 367 | 'click', |
| 368 | wc_meta_boxes_product_variations_actions.set_variations_price |
| 369 | ); |
| 370 | }, |
| 371 | |
| 372 | /** |
| 373 | * Lets the user manually input menu order to move items around pages |
| 374 | */ |
| 375 | set_menu_order: function ( event ) { |
| 376 | event.preventDefault(); |
| 377 | var $menu_order = $( this ) |
| 378 | .closest( '.woocommerce_variation' ) |
| 379 | .find( '.variation_menu_order' ); |
| 380 | var variation_id = $( this ) |
| 381 | .closest( '.woocommerce_variation' ) |
| 382 | .find( '.variable_post_id' ) |
| 383 | .val(); |
| 384 | var value = window.prompt( |
| 385 | woocommerce_admin_meta_boxes_variations.i18n_enter_menu_order, |
| 386 | $menu_order.val() |
| 387 | ); |
| 388 | |
| 389 | if ( value != null ) { |
| 390 | // Set value, save changes and reload view |
| 391 | $menu_order.val( parseInt( value, 10 ) ).trigger( 'change' ); |
| 392 | |
| 393 | $( this ) |
| 394 | .closest( '.woocommerce_variation' ) |
| 395 | .append( |
| 396 | '<input type="hidden" name="new_variation_menu_order_id" value="' + |
| 397 | encodeURIComponent( variation_id ) + |
| 398 | '" />' |
| 399 | ); |
| 400 | |
| 401 | $( this ) |
| 402 | .closest( '.woocommerce_variation' ) |
| 403 | .append( |
| 404 | '<input type="hidden" name="new_variation_menu_order_value" value="' + |
| 405 | encodeURIComponent( parseInt( value, 10 ) ) + |
| 406 | '" />' |
| 407 | ); |
| 408 | |
| 409 | wc_meta_boxes_product_variations_ajax.save_variations(); |
| 410 | } |
| 411 | }, |
| 412 | |
| 413 | /** |
| 414 | * Set menu order |
| 415 | */ |
| 416 | variation_row_indexes: function () { |
| 417 | var wrapper = $( '#variable_product_options' ).find( |
| 418 | '.woocommerce_variations' |
| 419 | ), |
| 420 | current_page = parseInt( wrapper.attr( 'data-page' ), 10 ), |
| 421 | offset = parseInt( |
| 422 | ( current_page - 1 ) * |
| 423 | woocommerce_admin_meta_boxes_variations.variations_per_page, |
| 424 | 10 |
| 425 | ); |
| 426 | |
| 427 | $( '.woocommerce_variations .woocommerce_variation' ).each( |
| 428 | function ( index, el ) { |
| 429 | $( '.variation_menu_order', el ) |
| 430 | .val( |
| 431 | parseInt( |
| 432 | $( el ).index( |
| 433 | '.woocommerce_variations .woocommerce_variation' |
| 434 | ), |
| 435 | 10 |
| 436 | ) + |
| 437 | 1 + |
| 438 | offset |
| 439 | ) |
| 440 | .trigger( 'change' ); |
| 441 | } |
| 442 | ); |
| 443 | }, |
| 444 | }; |
| 445 | |
| 446 | /** |
| 447 | * Variations media actions |
| 448 | */ |
| 449 | var wc_meta_boxes_product_variations_media = { |
| 450 | /** |
| 451 | * wp.media frame object |
| 452 | * |
| 453 | * @type {Object} |
| 454 | */ |
| 455 | variable_image_frame: null, |
| 456 | |
| 457 | /** |
| 458 | * Variation image ID |
| 459 | * |
| 460 | * @type {Int} |
| 461 | */ |
| 462 | setting_variation_image_id: null, |
| 463 | |
| 464 | /** |
| 465 | * Variation image object |
| 466 | * |
| 467 | * @type {Object} |
| 468 | */ |
| 469 | setting_variation_image: null, |
| 470 | |
| 471 | /** |
| 472 | * wp.media post ID |
| 473 | * |
| 474 | * @type {Int} |
| 475 | */ |
| 476 | wp_media_post_id: wp.media.model.settings.post.id, |
| 477 | |
| 478 | /** |
| 479 | * Initialize media actions |
| 480 | */ |
| 481 | init: function () { |
| 482 | $( '#variable_product_options' ).on( |
| 483 | 'click', |
| 484 | '.upload_image_button', |
| 485 | this.add_image |
| 486 | ); |
| 487 | $( 'a.add_media' ).on( 'click', this.restore_wp_media_post_id ); |
| 488 | }, |
| 489 | |
| 490 | /** |
| 491 | * Added new image |
| 492 | * |
| 493 | * @param {Object} event |
| 494 | */ |
| 495 | add_image: function ( event ) { |
| 496 | var $button = $( this ), |
| 497 | post_id = $button.attr( 'rel' ), |
| 498 | $parent = $button.closest( '.upload_image' ); |
| 499 | |
| 500 | wc_meta_boxes_product_variations_media.setting_variation_image = $parent; |
| 501 | wc_meta_boxes_product_variations_media.setting_variation_image_id = post_id; |
| 502 | |
| 503 | event.preventDefault(); |
| 504 | |
| 505 | if ( $button.is( '.remove' ) ) { |
| 506 | $( |
| 507 | '.upload_image_id', |
| 508 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 509 | ) |
| 510 | .val( '' ) |
| 511 | .trigger( 'change' ); |
| 512 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 513 | .find( 'img' ) |
| 514 | .eq( 0 ) |
| 515 | .attr( |
| 516 | 'src', |
| 517 | woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src |
| 518 | ); |
| 519 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 520 | .find( '.upload_image_button' ) |
| 521 | .removeClass( 'remove' ); |
| 522 | } else { |
| 523 | // If the media frame already exists, reopen it. |
| 524 | if ( |
| 525 | wc_meta_boxes_product_variations_media.variable_image_frame |
| 526 | ) { |
| 527 | wc_meta_boxes_product_variations_media.variable_image_frame.uploader.uploader.param( |
| 528 | 'post_id', |
| 529 | wc_meta_boxes_product_variations_media.setting_variation_image_id |
| 530 | ); |
| 531 | wc_meta_boxes_product_variations_media.variable_image_frame.open(); |
| 532 | return; |
| 533 | } else { |
| 534 | wp.media.model.settings.post.id = |
| 535 | wc_meta_boxes_product_variations_media.setting_variation_image_id; |
| 536 | } |
| 537 | |
| 538 | // Create the media frame. |
| 539 | wc_meta_boxes_product_variations_media.variable_image_frame = wp.media.frames.variable_image = wp.media( |
| 540 | { |
| 541 | // Set the title of the modal. |
| 542 | title: |
| 543 | woocommerce_admin_meta_boxes_variations.i18n_choose_image, |
| 544 | button: { |
| 545 | text: |
| 546 | woocommerce_admin_meta_boxes_variations.i18n_set_image, |
| 547 | }, |
| 548 | states: [ |
| 549 | new wp.media.controller.Library( { |
| 550 | title: |
| 551 | woocommerce_admin_meta_boxes_variations.i18n_choose_image, |
| 552 | filterable: 'all', |
| 553 | } ), |
| 554 | ], |
| 555 | } |
| 556 | ); |
| 557 | |
| 558 | // When an image is selected, run a callback. |
| 559 | wc_meta_boxes_product_variations_media.variable_image_frame.on( |
| 560 | 'select', |
| 561 | function () { |
| 562 | var attachment = wc_meta_boxes_product_variations_media.variable_image_frame |
| 563 | .state() |
| 564 | .get( 'selection' ) |
| 565 | .first() |
| 566 | .toJSON(), |
| 567 | url = |
| 568 | attachment.sizes && attachment.sizes.thumbnail |
| 569 | ? attachment.sizes.thumbnail.url |
| 570 | : attachment.url; |
| 571 | |
| 572 | $( |
| 573 | '.upload_image_id', |
| 574 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 575 | ) |
| 576 | .val( attachment.id ) |
| 577 | .trigger( 'change' ); |
| 578 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 579 | .find( '.upload_image_button' ) |
| 580 | .addClass( 'remove' ); |
| 581 | wc_meta_boxes_product_variations_media.setting_variation_image |
| 582 | .find( 'img' ) |
| 583 | .eq( 0 ) |
| 584 | .attr( 'src', url ); |
| 585 | |
| 586 | wp.media.model.settings.post.id = |
| 587 | wc_meta_boxes_product_variations_media.wp_media_post_id; |
| 588 | } |
| 589 | ); |
| 590 | |
| 591 | // Finally, open the modal. |
| 592 | wc_meta_boxes_product_variations_media.variable_image_frame.open(); |
| 593 | } |
| 594 | }, |
| 595 | |
| 596 | /** |
| 597 | * Restore wp.media post ID. |
| 598 | */ |
| 599 | restore_wp_media_post_id: function () { |
| 600 | wp.media.model.settings.post.id = |
| 601 | wc_meta_boxes_product_variations_media.wp_media_post_id; |
| 602 | }, |
| 603 | }; |
| 604 | |
| 605 | /** |
| 606 | * Product variations metabox ajax methods |
| 607 | */ |
| 608 | var wc_meta_boxes_product_variations_ajax = { |
| 609 | /** |
| 610 | * Initialize variations ajax methods |
| 611 | */ |
| 612 | init: function () { |
| 613 | $( 'li.variations_tab a' ).on( 'click', this.initial_load ); |
| 614 | |
| 615 | $( '#variable_product_options' ) |
| 616 | .on( |
| 617 | 'click', |
| 618 | 'button.save-variation-changes', |
| 619 | this.save_variations |
| 620 | ) |
| 621 | .on( |
| 622 | 'click', |
| 623 | 'button.cancel-variation-changes', |
| 624 | this.cancel_variations |
| 625 | ) |
| 626 | .on( 'click', '.remove_variation', this.remove_variation ) |
| 627 | .on( |
| 628 | 'click', |
| 629 | '.downloadable_files a.delete', |
| 630 | this.input_changed |
| 631 | ); |
| 632 | |
| 633 | $( document.body ) |
| 634 | .on( |
| 635 | 'change input', |
| 636 | '#variable_product_options .woocommerce_variations :input', |
| 637 | this.input_changed |
| 638 | ) |
| 639 | .on( |
| 640 | 'change', |
| 641 | '.variations-defaults select', |
| 642 | this.defaults_changed |
| 643 | ); |
| 644 | |
| 645 | var postForm = $( 'form#post' ); |
| 646 | |
| 647 | postForm.on( 'submit', this.save_on_submit ); |
| 648 | |
| 649 | $( 'input:submit', postForm ).on( 'click keypress', function () { |
| 650 | postForm.data( 'callerid', this.id ); |
| 651 | } ); |
| 652 | |
| 653 | $( '.wc-metaboxes-wrapper' ).on( |
| 654 | 'click', |
| 655 | 'a.do_variation_action', |
| 656 | this.do_variation_action |
| 657 | ); |
| 658 | }, |
| 659 | |
| 660 | /** |
| 661 | * Check if have some changes before leave the page |
| 662 | * |
| 663 | * @return {Bool} |
| 664 | */ |
| 665 | check_for_changes: function () { |
| 666 | var need_update = $( '#variable_product_options' ).find( |
| 667 | '.woocommerce_variations .variation-needs-update' |
| 668 | ); |
| 669 | |
| 670 | if ( 0 < need_update.length ) { |
| 671 | if ( |
| 672 | window.confirm( |
| 673 | woocommerce_admin_meta_boxes_variations.i18n_edited_variations |
| 674 | ) |
| 675 | ) { |
| 676 | wc_meta_boxes_product_variations_ajax.save_changes(); |
| 677 | } else { |
| 678 | need_update.removeClass( 'variation-needs-update' ); |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return true; |
| 684 | }, |
| 685 | |
| 686 | /** |
| 687 | * Block edit screen |
| 688 | */ |
| 689 | block: function () { |
| 690 | $( '#woocommerce-product-data' ).block( { |
| 691 | message: null, |
| 692 | overlayCSS: { |
| 693 | background: '#fff', |
| 694 | opacity: 0.6, |
| 695 | }, |
| 696 | } ); |
| 697 | }, |
| 698 | |
| 699 | /** |
| 700 | * Unblock edit screen |
| 701 | */ |
| 702 | unblock: function () { |
| 703 | $( '#woocommerce-product-data' ).unblock(); |
| 704 | }, |
| 705 | |
| 706 | /** |
| 707 | * Initial load variations |
| 708 | * |
| 709 | * @return {Bool} |
| 710 | */ |
| 711 | initial_load: function () { |
| 712 | if ( |
| 713 | 0 === |
| 714 | $( '#variable_product_options' ).find( |
| 715 | '.woocommerce_variations .woocommerce_variation' |
| 716 | ).length |
| 717 | ) { |
| 718 | wc_meta_boxes_product_variations_pagenav.go_to_page(); |
| 719 | } |
| 720 | }, |
| 721 | |
| 722 | /** |
| 723 | * Load variations via Ajax |
| 724 | * |
| 725 | * @param {Int} page (default: 1) |
| 726 | * @param {Int} per_page (default: 10) |
| 727 | */ |
| 728 | load_variations: function ( page, per_page ) { |
| 729 | page = page || 1; |
| 730 | per_page = |
| 731 | per_page || |
| 732 | woocommerce_admin_meta_boxes_variations.variations_per_page; |
| 733 | |
| 734 | var wrapper = $( '#variable_product_options' ).find( |
| 735 | '.woocommerce_variations' |
| 736 | ); |
| 737 | |
| 738 | wc_meta_boxes_product_variations_ajax.block(); |
| 739 | |
| 740 | $.ajax( { |
| 741 | url: woocommerce_admin_meta_boxes_variations.ajax_url, |
| 742 | data: { |
| 743 | action: 'woocommerce_load_variations', |
| 744 | security: |
| 745 | woocommerce_admin_meta_boxes_variations.load_variations_nonce, |
| 746 | product_id: woocommerce_admin_meta_boxes_variations.post_id, |
| 747 | attributes: wrapper.data( 'attributes' ), |
| 748 | page: page, |
| 749 | per_page: per_page, |
| 750 | }, |
| 751 | type: 'POST', |
| 752 | success: function ( response ) { |
| 753 | wrapper |
| 754 | .empty() |
| 755 | .append( response ) |
| 756 | .attr( 'data-page', page ); |
| 757 | |
| 758 | $( '#woocommerce-product-data' ).trigger( |
| 759 | 'woocommerce_variations_loaded' |
| 760 | ); |
| 761 | |
| 762 | wc_meta_boxes_product_variations_ajax.unblock(); |
| 763 | }, |
| 764 | } ); |
| 765 | }, |
| 766 | |
| 767 | /** |
| 768 | * Ger variations fields and convert to object |
| 769 | * |
| 770 | * @param {Object} fields |
| 771 | * |
| 772 | * @return {Object} |
| 773 | */ |
| 774 | get_variations_fields: function ( fields ) { |
| 775 | var data = $( ':input', fields ).serializeJSON(); |
| 776 | |
| 777 | $( '.variations-defaults select' ).each( function ( |
| 778 | index, |
| 779 | element |
| 780 | ) { |
| 781 | var select = $( element ); |
| 782 | data[ select.attr( 'name' ) ] = select.val(); |
| 783 | } ); |
| 784 | |
| 785 | return data; |
| 786 | }, |
| 787 | |
| 788 | /** |
| 789 | * Save variations changes |
| 790 | * |
| 791 | * @param {Function} callback Called once saving is complete |
| 792 | */ |
| 793 | save_changes: function ( callback ) { |
| 794 | var wrapper = $( '#variable_product_options' ).find( |
| 795 | '.woocommerce_variations' |
| 796 | ), |
| 797 | need_update = $( '.variation-needs-update', wrapper ), |
| 798 | data = {}; |
| 799 | |
| 800 | // Save only with products need update. |
| 801 | if ( 0 < need_update.length ) { |
| 802 | wc_meta_boxes_product_variations_ajax.block(); |
| 803 | |
| 804 | data = wc_meta_boxes_product_variations_ajax.get_variations_fields( |
| 805 | need_update |
| 806 | ); |
| 807 | data.action = 'woocommerce_save_variations'; |
| 808 | data.security = |
| 809 | woocommerce_admin_meta_boxes_variations.save_variations_nonce; |
| 810 | data.product_id = |
| 811 | woocommerce_admin_meta_boxes_variations.post_id; |
| 812 | data[ 'product-type' ] = $( '#product-type' ).val(); |
| 813 | |
| 814 | $.ajax( { |
| 815 | url: woocommerce_admin_meta_boxes_variations.ajax_url, |
| 816 | data: data, |
| 817 | type: 'POST', |
| 818 | success: function ( response ) { |
| 819 | // Allow change page, delete and add new variations |
| 820 | need_update.removeClass( 'variation-needs-update' ); |
| 821 | $( |
| 822 | 'button.cancel-variation-changes, button.save-variation-changes' |
| 823 | ).attr( 'disabled', 'disabled' ); |
| 824 | |
| 825 | $( '#woocommerce-product-data' ).trigger( |
| 826 | 'woocommerce_variations_saved' |
| 827 | ); |
| 828 | |
| 829 | if ( typeof callback === 'function' ) { |
| 830 | callback( response ); |
| 831 | } |
| 832 | |
| 833 | wc_meta_boxes_product_variations_ajax.unblock(); |
| 834 | }, |
| 835 | } ); |
| 836 | } |
| 837 | }, |
| 838 | |
| 839 | /** |
| 840 | * Save variations |
| 841 | * |
| 842 | * @return {Bool} |
| 843 | */ |
| 844 | save_variations: function () { |
| 845 | $( '#variable_product_options' ).trigger( |
| 846 | 'woocommerce_variations_save_variations_button' |
| 847 | ); |
| 848 | |
| 849 | wc_meta_boxes_product_variations_ajax.save_changes( function ( |
| 850 | error |
| 851 | ) { |
| 852 | var wrapper = $( '#variable_product_options' ).find( |
| 853 | '.woocommerce_variations' |
| 854 | ), |
| 855 | current = wrapper.attr( 'data-page' ); |
| 856 | |
| 857 | $( '#variable_product_options' ) |
| 858 | .find( '#woocommerce_errors' ) |
| 859 | .remove(); |
| 860 | |
| 861 | if ( error ) { |
| 862 | wrapper.before( error ); |
| 863 | } |
| 864 | |
| 865 | $( '.variations-defaults select' ).each( function () { |
| 866 | $( this ).attr( 'data-current', $( this ).val() ); |
| 867 | } ); |
| 868 | |
| 869 | wc_meta_boxes_product_variations_pagenav.go_to_page( current ); |
| 870 | } ); |
| 871 | |
| 872 | return false; |
| 873 | }, |
| 874 | |
| 875 | /** |
| 876 | * Save on post form submit |
| 877 | */ |
| 878 | save_on_submit: function ( e ) { |
| 879 | var need_update = $( '#variable_product_options' ).find( |
| 880 | '.woocommerce_variations .variation-needs-update' |
| 881 | ); |
| 882 | |
| 883 | if ( 0 < need_update.length ) { |
| 884 | e.preventDefault(); |
| 885 | $( '#variable_product_options' ).trigger( |
| 886 | 'woocommerce_variations_save_variations_on_submit' |
| 887 | ); |
| 888 | wc_meta_boxes_product_variations_ajax.save_changes( |
| 889 | wc_meta_boxes_product_variations_ajax.save_on_submit_done |
| 890 | ); |
| 891 | } |
| 892 | }, |
| 893 | |
| 894 | /** |
| 895 | * After saved, continue with form submission |
| 896 | */ |
| 897 | save_on_submit_done: function () { |
| 898 | var postForm = $( 'form#post' ), |
| 899 | callerid = postForm.data( 'callerid' ); |
| 900 | |
| 901 | if ( 'publish' === callerid ) { |
| 902 | postForm |
| 903 | .append( |
| 904 | '<input type="hidden" name="publish" value="1" />' |
| 905 | ) |
| 906 | .trigger( 'submit' ); |
| 907 | } else { |
| 908 | postForm |
| 909 | .append( |
| 910 | '<input type="hidden" name="save-post" value="1" />' |
| 911 | ) |
| 912 | .trigger( 'submit' ); |
| 913 | } |
| 914 | }, |
| 915 | |
| 916 | /** |
| 917 | * Discart changes. |
| 918 | * |
| 919 | * @return {Bool} |
| 920 | */ |
| 921 | cancel_variations: function () { |
| 922 | var current = parseInt( |
| 923 | $( '#variable_product_options' ) |
| 924 | .find( '.woocommerce_variations' ) |
| 925 | .attr( 'data-page' ), |
| 926 | 10 |
| 927 | ); |
| 928 | |
| 929 | $( '#variable_product_options' ) |
| 930 | .find( '.woocommerce_variations .variation-needs-update' ) |
| 931 | .removeClass( 'variation-needs-update' ); |
| 932 | $( '.variations-defaults select' ).each( function () { |
| 933 | $( this ).val( $( this ).attr( 'data-current' ) ); |
| 934 | } ); |
| 935 | |
| 936 | wc_meta_boxes_product_variations_pagenav.go_to_page( current ); |
| 937 | |
| 938 | return false; |
| 939 | }, |
| 940 | |
| 941 | /** |
| 942 | * Add variation |
| 943 | * |
| 944 | * @return {Bool} |
| 945 | */ |
| 946 | add_variation: function () { |
| 947 | wc_meta_boxes_product_variations_ajax.block(); |
| 948 | |
| 949 | var data = { |
| 950 | action: 'woocommerce_add_variation', |
| 951 | post_id: woocommerce_admin_meta_boxes_variations.post_id, |
| 952 | loop: $( '.woocommerce_variation' ).length, |
| 953 | security: |
| 954 | woocommerce_admin_meta_boxes_variations.add_variation_nonce, |
| 955 | }; |
| 956 | |
| 957 | $.post( |
| 958 | woocommerce_admin_meta_boxes_variations.ajax_url, |
| 959 | data, |
| 960 | function ( response ) { |
| 961 | var variation = $( response ); |
| 962 | variation.addClass( 'variation-needs-update' ); |
| 963 | |
| 964 | $( '.woocommerce-notice-invalid-variation' ).remove(); |
| 965 | $( '#variable_product_options' ) |
| 966 | .find( '.woocommerce_variations' ) |
| 967 | .prepend( variation ); |
| 968 | $( |
| 969 | 'button.cancel-variation-changes, button.save-variation-changes' |
| 970 | ).prop( 'disabled', false ); |
| 971 | $( '#variable_product_options' ).trigger( |
| 972 | 'woocommerce_variations_added', |
| 973 | 1 |
| 974 | ); |
| 975 | wc_meta_boxes_product_variations_ajax.unblock(); |
| 976 | } |
| 977 | ); |
| 978 | |
| 979 | return false; |
| 980 | }, |
| 981 | |
| 982 | /** |
| 983 | * Remove variation |
| 984 | * |
| 985 | * @return {Bool} |
| 986 | */ |
| 987 | remove_variation: function () { |
| 988 | wc_meta_boxes_product_variations_ajax.check_for_changes(); |
| 989 | |
| 990 | if ( |
| 991 | window.confirm( |
| 992 | woocommerce_admin_meta_boxes_variations.i18n_remove_variation |
| 993 | ) |
| 994 | ) { |
| 995 | var variation = $( this ).attr( 'rel' ), |
| 996 | variation_ids = [], |
| 997 | data = { |
| 998 | action: 'woocommerce_remove_variations', |
| 999 | }; |
| 1000 | |
| 1001 | wc_meta_boxes_product_variations_ajax.block(); |
| 1002 | |
| 1003 | if ( 0 < variation ) { |
| 1004 | variation_ids.push( variation ); |
| 1005 | |
| 1006 | data.variation_ids = variation_ids; |
| 1007 | data.security = |
| 1008 | woocommerce_admin_meta_boxes_variations.delete_variations_nonce; |
| 1009 | |
| 1010 | $.post( |
| 1011 | woocommerce_admin_meta_boxes_variations.ajax_url, |
| 1012 | data, |
| 1013 | function () { |
| 1014 | var wrapper = $( '#variable_product_options' ).find( |
| 1015 | '.woocommerce_variations' |
| 1016 | ), |
| 1017 | current_page = parseInt( |
| 1018 | wrapper.attr( 'data-page' ), |
| 1019 | 10 |
| 1020 | ), |
| 1021 | total_pages = Math.ceil( |
| 1022 | ( parseInt( |
| 1023 | wrapper.attr( 'data-total' ), |
| 1024 | 10 |
| 1025 | ) - |
| 1026 | 1 ) / |
| 1027 | woocommerce_admin_meta_boxes_variations.variations_per_page |
| 1028 | ), |
| 1029 | page = 1; |
| 1030 | |
| 1031 | $( '#woocommerce-product-data' ).trigger( |
| 1032 | 'woocommerce_variations_removed' |
| 1033 | ); |
| 1034 | |
| 1035 | if ( |
| 1036 | current_page === total_pages || |
| 1037 | current_page <= total_pages |
| 1038 | ) { |
| 1039 | page = current_page; |
| 1040 | } else if ( |
| 1041 | current_page > total_pages && |
| 1042 | 0 !== total_pages |
| 1043 | ) { |
| 1044 | page = total_pages; |
| 1045 | } |
| 1046 | |
| 1047 | wc_meta_boxes_product_variations_pagenav.go_to_page( |
| 1048 | page, |
| 1049 | -1 |
| 1050 | ); |
| 1051 | } |
| 1052 | ); |
| 1053 | } else { |
| 1054 | wc_meta_boxes_product_variations_ajax.unblock(); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return false; |
| 1059 | }, |
| 1060 | |
| 1061 | /** |
| 1062 | * Link all variations (or at least try :p) |
| 1063 | * |
| 1064 | * @return {Bool} |
| 1065 | */ |
| 1066 | link_all_variations: function () { |
| 1067 | wc_meta_boxes_product_variations_ajax.check_for_changes(); |
| 1068 | |
| 1069 | if ( |
| 1070 | window.confirm( |
| 1071 | woocommerce_admin_meta_boxes_variations.i18n_link_all_variations |
| 1072 | ) |
| 1073 | ) { |
| 1074 | wc_meta_boxes_product_variations_ajax.block(); |
| 1075 | |
| 1076 | var data = { |
| 1077 | action: 'woocommerce_link_all_variations', |
| 1078 | post_id: woocommerce_admin_meta_boxes_variations.post_id, |
| 1079 | security: |
| 1080 | woocommerce_admin_meta_boxes_variations.link_variation_nonce, |
| 1081 | }; |
| 1082 | |
| 1083 | $.post( |
| 1084 | woocommerce_admin_meta_boxes_variations.ajax_url, |
| 1085 | data, |
| 1086 | function ( response ) { |
| 1087 | var count = parseInt( response, 10 ); |
| 1088 | |
| 1089 | if ( 1 === count ) { |
| 1090 | window.alert( |
| 1091 | count + |
| 1092 | ' ' + |
| 1093 | woocommerce_admin_meta_boxes_variations.i18n_variation_added |
| 1094 | ); |
| 1095 | } else if ( 0 === count || count > 1 ) { |
| 1096 | window.alert( |
| 1097 | count + |
| 1098 | ' ' + |
| 1099 | woocommerce_admin_meta_boxes_variations.i18n_variations_added |
| 1100 | ); |
| 1101 | } else { |
| 1102 | window.alert( |
| 1103 | woocommerce_admin_meta_boxes_variations.i18n_no_variations_added |
| 1104 | ); |
| 1105 | } |
| 1106 | |
| 1107 | if ( count > 0 ) { |
| 1108 | wc_meta_boxes_product_variations_pagenav.go_to_page( |
| 1109 | 1, |
| 1110 | count |
| 1111 | ); |
| 1112 | $( '#variable_product_options' ).trigger( |
| 1113 | 'woocommerce_variations_added', |
| 1114 | count |
| 1115 | ); |
| 1116 | } else { |
| 1117 | wc_meta_boxes_product_variations_ajax.unblock(); |
| 1118 | } |
| 1119 | } |
| 1120 | ); |
| 1121 | } |
| 1122 | |
| 1123 | return false; |
| 1124 | }, |
| 1125 | |
| 1126 | /** |
| 1127 | * Add new class when have changes in some input |
| 1128 | */ |
| 1129 | input_changed: function ( event ) { |
| 1130 | $( this ) |
| 1131 | .closest( '.woocommerce_variation' ) |
| 1132 | .addClass( 'variation-needs-update' ); |
| 1133 | |
| 1134 | $( |
| 1135 | 'button.cancel-variation-changes, button.save-variation-changes' |
| 1136 | ).prop( 'disabled', false ); |
| 1137 | |
| 1138 | // Do not trigger 'woocommerce_variations_input_changed' for 'input' events for backwards compat. |
| 1139 | if ( 'input' === event.type && $( this ).is( ':text' ) ) { |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | $( '#variable_product_options' ).trigger( |
| 1144 | 'woocommerce_variations_input_changed' |
| 1145 | ); |
| 1146 | }, |
| 1147 | |
| 1148 | /** |
| 1149 | * Added new .variation-needs-update class when defaults is changed |
| 1150 | */ |
| 1151 | defaults_changed: function () { |
| 1152 | $( this ) |
| 1153 | .closest( '#variable_product_options' ) |
| 1154 | .find( '.woocommerce_variation:first' ) |
| 1155 | .addClass( 'variation-needs-update' ); |
| 1156 | |
| 1157 | $( |
| 1158 | 'button.cancel-variation-changes, button.save-variation-changes' |
| 1159 | ).prop( 'disabled', false ); |
| 1160 | |
| 1161 | $( '#variable_product_options' ).trigger( |
| 1162 | 'woocommerce_variations_defaults_changed' |
| 1163 | ); |
| 1164 | }, |
| 1165 | |
| 1166 | /** |
| 1167 | * Actions |
| 1168 | */ |
| 1169 | do_variation_action: function () { |
| 1170 | var do_variation_action = $( 'select.variation_actions' ).val(), |
| 1171 | data = {}, |
| 1172 | changes = 0, |
| 1173 | value; |
| 1174 | |
| 1175 | switch ( do_variation_action ) { |
| 1176 | case 'add_variation': |
| 1177 | wc_meta_boxes_product_variations_ajax.add_variation(); |
| 1178 | return; |
| 1179 | case 'link_all_variations': |
| 1180 | wc_meta_boxes_product_variations_ajax.link_all_variations(); |
| 1181 | return; |
| 1182 | case 'delete_all': |
| 1183 | if ( |
| 1184 | window.confirm( |
| 1185 | woocommerce_admin_meta_boxes_variations.i18n_delete_all_variations |
| 1186 | ) |
| 1187 | ) { |
| 1188 | if ( |
| 1189 | window.confirm( |
| 1190 | woocommerce_admin_meta_boxes_variations.i18n_last_warning |
| 1191 | ) |
| 1192 | ) { |
| 1193 | data.allowed = true; |
| 1194 | changes = |
| 1195 | parseInt( |
| 1196 | $( '#variable_product_options' ) |
| 1197 | .find( '.woocommerce_variations' ) |
| 1198 | .attr( 'data-total' ), |
| 1199 | 10 |
| 1200 | ) * -1; |
| 1201 | } |
| 1202 | } |
| 1203 | break; |
| 1204 | case 'variable_regular_price_increase': |
| 1205 | case 'variable_regular_price_decrease': |
| 1206 | case 'variable_sale_price_increase': |
| 1207 | case 'variable_sale_price_decrease': |
| 1208 | value = window.prompt( |
| 1209 | woocommerce_admin_meta_boxes_variations.i18n_enter_a_value_fixed_or_percent |
| 1210 | ); |
| 1211 | |
| 1212 | if ( value != null ) { |
| 1213 | if ( value.indexOf( '%' ) >= 0 ) { |
| 1214 | data.value = |
| 1215 | accounting.unformat( |
| 1216 | value.replace( /\%/, '' ), |
| 1217 | woocommerce_admin.mon_decimal_point |
| 1218 | ) + '%'; |
| 1219 | } else { |
| 1220 | data.value = accounting.unformat( |
| 1221 | value, |
| 1222 | woocommerce_admin.mon_decimal_point |
| 1223 | ); |
| 1224 | } |
| 1225 | } else { |
| 1226 | return; |
| 1227 | } |
| 1228 | break; |
| 1229 | case 'variable_regular_price': |
| 1230 | case 'variable_sale_price': |
| 1231 | case 'variable_stock': |
| 1232 | case 'variable_low_stock_amount': |
| 1233 | case 'variable_weight': |
| 1234 | case 'variable_length': |
| 1235 | case 'variable_width': |
| 1236 | case 'variable_height': |
| 1237 | case 'variable_download_limit': |
| 1238 | case 'variable_download_expiry': |
| 1239 | value = window.prompt( |
| 1240 | woocommerce_admin_meta_boxes_variations.i18n_enter_a_value |
| 1241 | ); |
| 1242 | |
| 1243 | if ( value != null ) { |
| 1244 | data.value = value; |
| 1245 | } else { |
| 1246 | return; |
| 1247 | } |
| 1248 | break; |
| 1249 | case 'variable_sale_schedule': |
| 1250 | data.date_from = window.prompt( |
| 1251 | woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_start |
| 1252 | ); |
| 1253 | data.date_to = window.prompt( |
| 1254 | woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_end |
| 1255 | ); |
| 1256 | |
| 1257 | if ( null === data.date_from ) { |
| 1258 | data.date_from = false; |
| 1259 | } |
| 1260 | |
| 1261 | if ( null === data.date_to ) { |
| 1262 | data.date_to = false; |
| 1263 | } |
| 1264 | |
| 1265 | if ( false === data.date_to && false === data.date_from ) { |
| 1266 | return; |
| 1267 | } |
| 1268 | break; |
| 1269 | default: |
| 1270 | $( 'select.variation_actions' ).trigger( |
| 1271 | do_variation_action |
| 1272 | ); |
| 1273 | data = $( 'select.variation_actions' ).triggerHandler( |
| 1274 | do_variation_action + '_ajax_data', |
| 1275 | data |
| 1276 | ); |
| 1277 | |
| 1278 | if ( null === data ) { |
| 1279 | return; |
| 1280 | } |
| 1281 | break; |
| 1282 | } |
| 1283 | |
| 1284 | if ( 'delete_all' === do_variation_action && data.allowed ) { |
| 1285 | $( '#variable_product_options' ) |
| 1286 | .find( '.variation-needs-update' ) |
| 1287 | .removeClass( 'variation-needs-update' ); |
| 1288 | } else { |
| 1289 | wc_meta_boxes_product_variations_ajax.check_for_changes(); |
| 1290 | } |
| 1291 | |
| 1292 | wc_meta_boxes_product_variations_ajax.block(); |
| 1293 | |
| 1294 | $.ajax( { |
| 1295 | url: woocommerce_admin_meta_boxes_variations.ajax_url, |
| 1296 | data: { |
| 1297 | action: 'woocommerce_bulk_edit_variations', |
| 1298 | security: |
| 1299 | woocommerce_admin_meta_boxes_variations.bulk_edit_variations_nonce, |
| 1300 | product_id: woocommerce_admin_meta_boxes_variations.post_id, |
| 1301 | product_type: $( '#product-type' ).val(), |
| 1302 | bulk_action: do_variation_action, |
| 1303 | data: data, |
| 1304 | }, |
| 1305 | type: 'POST', |
| 1306 | success: function () { |
| 1307 | wc_meta_boxes_product_variations_pagenav.go_to_page( |
| 1308 | 1, |
| 1309 | changes |
| 1310 | ); |
| 1311 | }, |
| 1312 | } ); |
| 1313 | }, |
| 1314 | }; |
| 1315 | |
| 1316 | /** |
| 1317 | * Product variations pagenav |
| 1318 | */ |
| 1319 | var wc_meta_boxes_product_variations_pagenav = { |
| 1320 | /** |
| 1321 | * Initialize products variations meta box |
| 1322 | */ |
| 1323 | init: function () { |
| 1324 | $( document.body ) |
| 1325 | .on( |
| 1326 | 'woocommerce_variations_added', |
| 1327 | this.update_single_quantity |
| 1328 | ) |
| 1329 | .on( |
| 1330 | 'change', |
| 1331 | '.variations-pagenav .page-selector', |
| 1332 | this.page_selector |
| 1333 | ) |
| 1334 | .on( |
| 1335 | 'click', |
| 1336 | '.variations-pagenav .first-page', |
| 1337 | this.first_page |
| 1338 | ) |
| 1339 | .on( 'click', '.variations-pagenav .prev-page', this.prev_page ) |
| 1340 | .on( 'click', '.variations-pagenav .next-page', this.next_page ) |
| 1341 | .on( |
| 1342 | 'click', |
| 1343 | '.variations-pagenav .last-page', |
| 1344 | this.last_page |
| 1345 | ); |
| 1346 | }, |
| 1347 | |
| 1348 | /** |
| 1349 | * Set variations count |
| 1350 | * |
| 1351 | * @param {Int} qty |
| 1352 | * |
| 1353 | * @return {Int} |
| 1354 | */ |
| 1355 | update_variations_count: function ( qty ) { |
| 1356 | var wrapper = $( '#variable_product_options' ).find( |
| 1357 | '.woocommerce_variations' |
| 1358 | ), |
| 1359 | total = parseInt( wrapper.attr( 'data-total' ), 10 ) + qty, |
| 1360 | displaying_num = $( '.variations-pagenav .displaying-num' ); |
| 1361 | |
| 1362 | // Set the new total of variations |
| 1363 | wrapper.attr( 'data-total', total ); |
| 1364 | |
| 1365 | if ( 1 === total ) { |
| 1366 | displaying_num.text( |
| 1367 | woocommerce_admin_meta_boxes_variations.i18n_variation_count_single.replace( |
| 1368 | '%qty%', |
| 1369 | total |
| 1370 | ) |
| 1371 | ); |
| 1372 | } else { |
| 1373 | displaying_num.text( |
| 1374 | woocommerce_admin_meta_boxes_variations.i18n_variation_count_plural.replace( |
| 1375 | '%qty%', |
| 1376 | total |
| 1377 | ) |
| 1378 | ); |
| 1379 | } |
| 1380 | |
| 1381 | return total; |
| 1382 | }, |
| 1383 | |
| 1384 | /** |
| 1385 | * Update variations quantity when add a new variation |
| 1386 | * |
| 1387 | * @param {Object} event |
| 1388 | * @param {Int} qty |
| 1389 | */ |
| 1390 | update_single_quantity: function ( event, qty ) { |
| 1391 | if ( 1 === qty ) { |
| 1392 | var page_nav = $( '.variations-pagenav' ); |
| 1393 | |
| 1394 | wc_meta_boxes_product_variations_pagenav.update_variations_count( |
| 1395 | qty |
| 1396 | ); |
| 1397 | |
| 1398 | if ( page_nav.is( ':hidden' ) ) { |
| 1399 | $( 'option, optgroup', '.variation_actions' ).show(); |
| 1400 | $( '.variation_actions' ).val( 'add_variation' ); |
| 1401 | $( '#variable_product_options' ).find( '.toolbar' ).show(); |
| 1402 | page_nav.show(); |
| 1403 | $( '.pagination-links', page_nav ).hide(); |
| 1404 | } |
| 1405 | } |
| 1406 | }, |
| 1407 | |
| 1408 | /** |
| 1409 | * Set the pagenav fields |
| 1410 | * |
| 1411 | * @param {Int} qty |
| 1412 | */ |
| 1413 | set_paginav: function ( qty ) { |
| 1414 | var wrapper = $( '#variable_product_options' ).find( |
| 1415 | '.woocommerce_variations' |
| 1416 | ), |
| 1417 | new_qty = wc_meta_boxes_product_variations_pagenav.update_variations_count( |
| 1418 | qty |
| 1419 | ), |
| 1420 | toolbar = $( '#variable_product_options' ).find( '.toolbar' ), |
| 1421 | variation_action = $( '.variation_actions' ), |
| 1422 | page_nav = $( '.variations-pagenav' ), |
| 1423 | displaying_links = $( '.pagination-links', page_nav ), |
| 1424 | total_pages = Math.ceil( |
| 1425 | new_qty / |
| 1426 | woocommerce_admin_meta_boxes_variations.variations_per_page |
| 1427 | ), |
| 1428 | options = ''; |
| 1429 | |
| 1430 | // Set the new total of pages |
| 1431 | wrapper.attr( 'data-total_pages', total_pages ); |
| 1432 | |
| 1433 | $( '.total-pages', page_nav ).text( total_pages ); |
| 1434 | |
| 1435 | // Set the new pagenav options |
| 1436 | for ( var i = 1; i <= total_pages; i++ ) { |
| 1437 | options += '<option value="' + i + '">' + i + '</option>'; |
| 1438 | } |
| 1439 | |
| 1440 | $( '.page-selector', page_nav ).empty().html( options ); |
| 1441 | |
| 1442 | // Show/hide pagenav |
| 1443 | if ( 0 === new_qty ) { |
| 1444 | toolbar.not( '.toolbar-top, .toolbar-buttons' ).hide(); |
| 1445 | page_nav.hide(); |
| 1446 | $( 'option, optgroup', variation_action ).hide(); |
| 1447 | $( '.variation_actions' ).val( 'add_variation' ); |
| 1448 | $( 'option[data-global="true"]', variation_action ).show(); |
| 1449 | } else { |
| 1450 | toolbar.show(); |
| 1451 | page_nav.show(); |
| 1452 | $( 'option, optgroup', variation_action ).show(); |
| 1453 | $( '.variation_actions' ).val( 'add_variation' ); |
| 1454 | |
| 1455 | // Show/hide links |
| 1456 | if ( 1 === total_pages ) { |
| 1457 | displaying_links.hide(); |
| 1458 | } else { |
| 1459 | displaying_links.show(); |
| 1460 | } |
| 1461 | } |
| 1462 | }, |
| 1463 | |
| 1464 | /** |
| 1465 | * Check button if enabled and if don't have changes |
| 1466 | * |
| 1467 | * @return {Bool} |
| 1468 | */ |
| 1469 | check_is_enabled: function ( current ) { |
| 1470 | return ! $( current ).hasClass( 'disabled' ); |
| 1471 | }, |
| 1472 | |
| 1473 | /** |
| 1474 | * Change "disabled" class on pagenav |
| 1475 | */ |
| 1476 | change_classes: function ( selected, total ) { |
| 1477 | var first_page = $( '.variations-pagenav .first-page' ), |
| 1478 | prev_page = $( '.variations-pagenav .prev-page' ), |
| 1479 | next_page = $( '.variations-pagenav .next-page' ), |
| 1480 | last_page = $( '.variations-pagenav .last-page' ); |
| 1481 | |
| 1482 | if ( 1 === selected ) { |
| 1483 | first_page.addClass( 'disabled' ); |
| 1484 | prev_page.addClass( 'disabled' ); |
| 1485 | } else { |
| 1486 | first_page.removeClass( 'disabled' ); |
| 1487 | prev_page.removeClass( 'disabled' ); |
| 1488 | } |
| 1489 | |
| 1490 | if ( total === selected ) { |
| 1491 | next_page.addClass( 'disabled' ); |
| 1492 | last_page.addClass( 'disabled' ); |
| 1493 | } else { |
| 1494 | next_page.removeClass( 'disabled' ); |
| 1495 | last_page.removeClass( 'disabled' ); |
| 1496 | } |
| 1497 | }, |
| 1498 | |
| 1499 | /** |
| 1500 | * Set page |
| 1501 | */ |
| 1502 | set_page: function ( page ) { |
| 1503 | $( '.variations-pagenav .page-selector' ) |
| 1504 | .val( page ) |
| 1505 | .first() |
| 1506 | .trigger( 'change' ); |
| 1507 | }, |
| 1508 | |
| 1509 | /** |
| 1510 | * Navigate on variations pages |
| 1511 | * |
| 1512 | * @param {Int} page |
| 1513 | * @param {Int} qty |
| 1514 | */ |
| 1515 | go_to_page: function ( page, qty ) { |
| 1516 | page = page || 1; |
| 1517 | qty = qty || 0; |
| 1518 | |
| 1519 | wc_meta_boxes_product_variations_pagenav.set_paginav( qty ); |
| 1520 | wc_meta_boxes_product_variations_pagenav.set_page( page ); |
| 1521 | }, |
| 1522 | |
| 1523 | /** |
| 1524 | * Paginav pagination selector |
| 1525 | */ |
| 1526 | page_selector: function () { |
| 1527 | var selected = parseInt( $( this ).val(), 10 ), |
| 1528 | wrapper = $( '#variable_product_options' ).find( |
| 1529 | '.woocommerce_variations' |
| 1530 | ); |
| 1531 | |
| 1532 | $( '.variations-pagenav .page-selector' ).val( selected ); |
| 1533 | |
| 1534 | wc_meta_boxes_product_variations_ajax.check_for_changes(); |
| 1535 | wc_meta_boxes_product_variations_pagenav.change_classes( |
| 1536 | selected, |
| 1537 | parseInt( wrapper.attr( 'data-total_pages' ), 10 ) |
| 1538 | ); |
| 1539 | wc_meta_boxes_product_variations_ajax.load_variations( selected ); |
| 1540 | }, |
| 1541 | |
| 1542 | /** |
| 1543 | * Go to first page |
| 1544 | * |
| 1545 | * @return {Bool} |
| 1546 | */ |
| 1547 | first_page: function () { |
| 1548 | if ( |
| 1549 | wc_meta_boxes_product_variations_pagenav.check_is_enabled( |
| 1550 | this |
| 1551 | ) |
| 1552 | ) { |
| 1553 | wc_meta_boxes_product_variations_pagenav.set_page( 1 ); |
| 1554 | } |
| 1555 | |
| 1556 | return false; |
| 1557 | }, |
| 1558 | |
| 1559 | /** |
| 1560 | * Go to previous page |
| 1561 | * |
| 1562 | * @return {Bool} |
| 1563 | */ |
| 1564 | prev_page: function () { |
| 1565 | if ( |
| 1566 | wc_meta_boxes_product_variations_pagenav.check_is_enabled( |
| 1567 | this |
| 1568 | ) |
| 1569 | ) { |
| 1570 | var wrapper = $( '#variable_product_options' ).find( |
| 1571 | '.woocommerce_variations' |
| 1572 | ), |
| 1573 | prev_page = parseInt( wrapper.attr( 'data-page' ), 10 ) - 1, |
| 1574 | new_page = 0 < prev_page ? prev_page : 1; |
| 1575 | |
| 1576 | wc_meta_boxes_product_variations_pagenav.set_page( new_page ); |
| 1577 | } |
| 1578 | |
| 1579 | return false; |
| 1580 | }, |
| 1581 | |
| 1582 | /** |
| 1583 | * Go to next page |
| 1584 | * |
| 1585 | * @return {Bool} |
| 1586 | */ |
| 1587 | next_page: function () { |
| 1588 | if ( |
| 1589 | wc_meta_boxes_product_variations_pagenav.check_is_enabled( |
| 1590 | this |
| 1591 | ) |
| 1592 | ) { |
| 1593 | var wrapper = $( '#variable_product_options' ).find( |
| 1594 | '.woocommerce_variations' |
| 1595 | ), |
| 1596 | total_pages = parseInt( |
| 1597 | wrapper.attr( 'data-total_pages' ), |
| 1598 | 10 |
| 1599 | ), |
| 1600 | next_page = parseInt( wrapper.attr( 'data-page' ), 10 ) + 1, |
| 1601 | new_page = |
| 1602 | total_pages >= next_page ? next_page : total_pages; |
| 1603 | |
| 1604 | wc_meta_boxes_product_variations_pagenav.set_page( new_page ); |
| 1605 | } |
| 1606 | |
| 1607 | return false; |
| 1608 | }, |
| 1609 | |
| 1610 | /** |
| 1611 | * Go to last page |
| 1612 | * |
| 1613 | * @return {Bool} |
| 1614 | */ |
| 1615 | last_page: function () { |
| 1616 | if ( |
| 1617 | wc_meta_boxes_product_variations_pagenav.check_is_enabled( |
| 1618 | this |
| 1619 | ) |
| 1620 | ) { |
| 1621 | var last_page = $( '#variable_product_options' ) |
| 1622 | .find( '.woocommerce_variations' ) |
| 1623 | .attr( 'data-total_pages' ); |
| 1624 | |
| 1625 | wc_meta_boxes_product_variations_pagenav.set_page( last_page ); |
| 1626 | } |
| 1627 | |
| 1628 | return false; |
| 1629 | }, |
| 1630 | }; |
| 1631 | |
| 1632 | wc_meta_boxes_product_variations_actions.init(); |
| 1633 | wc_meta_boxes_product_variations_media.init(); |
| 1634 | wc_meta_boxes_product_variations_ajax.init(); |
| 1635 | wc_meta_boxes_product_variations_pagenav.init(); |
| 1636 | } ); |
| 1637 |