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