2015-07-08 05:19:56 +00:00
/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order */
2014-07-16 18:11:48 +00:00
jQuery ( function ( $ ) {
2013-11-12 17:01:13 +00:00
2014-07-16 18:41:18 +00:00
/ * *
2015-01-12 15:43:13 +00:00
* Order Data Panel
2014-07-16 18:41:18 +00:00
* /
2015-01-12 15:43:13 +00:00
var wc _meta _boxes _order = {
states : null ,
init : function ( ) {
if ( ! ( typeof woocommerce _admin _meta _boxes _order === 'undefined' || typeof woocommerce _admin _meta _boxes _order . countries === 'undefined' ) ) {
/* State/Country select boxes */
this . states = $ . parseJSON ( woocommerce _admin _meta _boxes _order . countries . replace ( /"/g , '"' ) ) ;
2014-07-16 18:41:18 +00:00
}
2012-11-27 16:22:47 +00:00
2017-06-26 19:44:33 +00:00
$ ( '.js_field-country' ) . selectWoo ( ) . change ( this . change _country ) ;
2015-01-12 15:43:13 +00:00
$ ( '.js_field-country' ) . trigger ( 'change' , [ true ] ) ;
2015-04-13 15:37:22 +00:00
$ ( document . body ) . on ( 'change' , 'select.js_field-state' , this . change _state ) ;
2015-01-12 15:43:13 +00:00
$ ( '#woocommerce-order-actions input, #woocommerce-order-actions a' ) . click ( function ( ) {
window . onbeforeunload = '' ;
} ) ;
$ ( 'a.edit_address' ) . click ( this . edit _address ) ;
2015-05-15 11:52:23 +00:00
$ ( 'a.billing-same-as-shipping' ) . on ( 'click' , this . copy _billing _to _shipping ) ;
$ ( 'a.load_customer_billing' ) . on ( 'click' , this . load _billing ) ;
$ ( 'a.load_customer_shipping' ) . on ( 'click' , this . load _shipping ) ;
$ ( '#customer_user' ) . on ( 'change' , this . change _customer _user ) ;
2015-01-12 15:43:13 +00:00
} ,
2014-08-01 04:27:22 +00:00
2015-01-12 15:43:13 +00:00
change _country : function ( e , stickValue ) {
2014-08-01 04:47:03 +00:00
// Check for stickValue before using it
if ( typeof stickValue === 'undefined' ) {
stickValue = false ;
}
// Prevent if we don't have the metabox data
2015-01-12 15:43:13 +00:00
if ( wc _meta _boxes _order . states === null ) {
2014-08-01 04:47:03 +00:00
return ;
}
var $this = $ ( this ) ,
country = $this . val ( ) ,
2015-05-15 11:52:23 +00:00
$state = $this . parents ( 'div.edit_address' ) . find ( ':input.js_field-state' ) ,
2014-08-01 04:47:03 +00:00
$parent = $state . parent ( ) ,
input _name = $state . attr ( 'name' ) ,
input _id = $state . attr ( 'id' ) ,
2014-08-02 21:34:30 +00:00
value = $this . data ( 'woocommerce.stickState-' + country ) ? $this . data ( 'woocommerce.stickState-' + country ) : $state . val ( ) ,
2014-08-01 04:47:03 +00:00
placeholder = $state . attr ( 'placeholder' ) ;
if ( stickValue ) {
$this . data ( 'woocommerce.stickState-' + country , value ) ;
}
2015-01-12 15:43:13 +00:00
// Remove the previous DOM element
$parent . show ( ) . find ( '.select2-container' ) . remove ( ) ;
2014-08-02 21:34:30 +00:00
2015-01-12 15:43:13 +00:00
if ( ! $ . isEmptyObject ( wc _meta _boxes _order . states [ country ] ) ) {
2014-08-01 04:47:03 +00:00
var $states _select = $ ( '<select name="' + input _name + '" id="' + input _id + '" class="js_field-state select short" placeholder="' + placeholder + '"></select>' ) ,
2015-01-12 15:43:13 +00:00
state = wc _meta _boxes _order . states [ country ] ;
2014-08-01 04:47:03 +00:00
$states _select . append ( $ ( '<option value="">' + woocommerce _admin _meta _boxes _order . i18n _select _state _text + '</option>' ) ) ;
2015-07-08 05:19:56 +00:00
$ . each ( state , function ( index ) {
2014-08-01 04:47:03 +00:00
$states _select . append ( $ ( '<option value="' + index + '">' + state [ index ] + '</option>' ) ) ;
} ) ;
$states _select . val ( value ) ;
$state . replaceWith ( $states _select ) ;
2017-06-26 19:44:33 +00:00
$states _select . show ( ) . selectWoo ( ) . hide ( ) . change ( ) ;
2014-08-01 04:47:03 +00:00
} else {
2014-11-11 11:24:02 +00:00
$state . replaceWith ( '<input type="text" class="js_field-state" name="' + input _name + '" id="' + input _id + '" value="' + value + '" placeholder="' + placeholder + '" />' ) ;
2014-08-01 04:47:03 +00:00
}
2015-10-01 09:04:23 +00:00
// This event has a typo - deprecated in 2.5.0
2015-04-13 15:37:22 +00:00
$ ( document . body ) . trigger ( 'contry-change.woocommerce' , [ country , $ ( this ) . closest ( 'div' ) ] ) ;
2015-10-01 09:04:23 +00:00
$ ( document . body ) . trigger ( 'country-change.woocommerce' , [ country , $ ( this ) . closest ( 'div' ) ] ) ;
2015-01-12 15:43:13 +00:00
} ,
2014-08-01 04:27:22 +00:00
2015-01-12 15:43:13 +00:00
change _state : function ( ) {
2014-08-01 04:27:22 +00:00
// Here we will find if state value on a select has changed and stick it to the country data
var $this = $ ( this ) ,
state = $this . val ( ) ,
2015-05-15 11:52:23 +00:00
$country = $this . parents ( 'div.edit_address' ) . find ( ':input.js_field-country' ) ,
2014-08-01 04:27:22 +00:00
country = $country . val ( ) ;
$country . data ( 'woocommerce.stickState-' + country , state ) ;
2015-01-12 15:43:13 +00:00
} ,
init _tiptip : function ( ) {
$ ( '#tiptip_holder' ) . removeAttr ( 'style' ) ;
$ ( '#tiptip_arrow' ) . removeAttr ( 'style' ) ;
$ ( '.tips' ) . tipTip ( {
'attribute' : 'data-tip' ,
'fadeIn' : 50 ,
'fadeOut' : 50 ,
'delay' : 200
} ) ;
} ,
edit _address : function ( e ) {
e . preventDefault ( ) ;
2017-02-08 10:55:57 +00:00
var $this = $ ( this ) ,
$wrapper = $this . closest ( '.order_data_column' ) ,
$edit _address = $wrapper . find ( 'div.edit_address' ) ,
$address = $wrapper . find ( 'div.address' ) ,
$country _input = $edit _address . find ( '.js_field-country' ) ,
$state _input = $edit _address . find ( '.js_field-state' ) ;
$address . hide ( ) ;
$this . parent ( ) . find ( 'a' ) . toggle ( ) ;
if ( ! $country _input . val ( ) ) {
$country _input . val ( woocommerce _admin _meta _boxes _order . default _country ) . change ( ) ;
}
if ( ! $state _input . val ( ) ) {
$state _input . val ( woocommerce _admin _meta _boxes _order . default _state ) . change ( ) ;
}
$edit _address . show ( ) ;
2015-01-12 15:43:13 +00:00
} ,
2014-07-17 20:17:54 +00:00
2015-07-08 05:19:56 +00:00
change _customer _user : function ( ) {
2015-05-15 11:52:23 +00:00
if ( ! $ ( '#_billing_country' ) . val ( ) ) {
$ ( 'a.edit_address' ) . click ( ) ;
wc _meta _boxes _order . load _billing ( true ) ;
wc _meta _boxes _order . load _shipping ( true ) ;
}
} ,
load _billing : function ( force ) {
if ( true === force || window . confirm ( woocommerce _admin _meta _boxes . load _billing ) ) {
2014-07-07 15:45:08 +00:00
2015-01-12 15:43:13 +00:00
// Get user ID to load data for
var user _id = $ ( '#customer_user' ) . val ( ) ;
2014-07-07 15:45:08 +00:00
2015-01-12 15:43:13 +00:00
if ( ! user _id ) {
window . alert ( woocommerce _admin _meta _boxes . no _customer _selected ) ;
return false ;
}
2014-07-10 10:25:50 +00:00
var data = {
2016-11-24 15:31:05 +00:00
user _id : user _id ,
action : 'woocommerce_get_customer_details' ,
security : woocommerce _admin _meta _boxes . get _customer _details _nonce
2014-07-10 10:25:50 +00:00
} ;
2015-05-15 11:52:23 +00:00
$ ( this ) . closest ( 'div.edit_address' ) . block ( {
2015-01-12 15:43:13 +00:00
message : null ,
overlayCSS : {
background : '#fff' ,
opacity : 0.6
}
} ) ;
2014-07-16 18:41:18 +00:00
$ . ajax ( {
2015-01-12 15:43:13 +00:00
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
2014-08-31 06:25:22 +00:00
success : function ( response ) {
2016-11-24 15:31:05 +00:00
if ( response && response . billing ) {
$ . each ( response . billing , function ( key , data ) {
$ ( ':input#_billing_' + key ) . val ( data ) . change ( ) ;
2015-10-27 15:53:54 +00:00
} ) ;
2015-01-12 15:43:13 +00:00
}
2015-05-15 11:52:23 +00:00
$ ( 'div.edit_address' ) . unblock ( ) ;
2014-07-10 10:25:50 +00:00
}
2014-07-16 18:41:18 +00:00
} ) ;
2014-07-10 10:25:50 +00:00
}
return false ;
2015-01-12 15:43:13 +00:00
} ,
2015-05-15 11:52:23 +00:00
load _shipping : function ( force ) {
if ( true === force || window . confirm ( woocommerce _admin _meta _boxes . load _shipping ) ) {
2014-07-16 20:12:33 +00:00
2015-01-12 15:43:13 +00:00
// Get user ID to load data for
var user _id = $ ( '#customer_user' ) . val ( ) ;
if ( ! user _id ) {
window . alert ( woocommerce _admin _meta _boxes . no _customer _selected ) ;
return false ;
}
2014-07-16 20:12:33 +00:00
var data = {
2015-01-12 15:43:13 +00:00
user _id : user _id ,
action : 'woocommerce_get_customer_details' ,
security : woocommerce _admin _meta _boxes . get _customer _details _nonce
2014-07-16 20:12:33 +00:00
} ;
2015-05-15 11:52:23 +00:00
$ ( this ) . closest ( 'div.edit_address' ) . block ( {
2015-01-12 15:43:13 +00:00
message : null ,
overlayCSS : {
background : '#fff' ,
opacity : 0.6
}
} ) ;
2014-07-16 20:12:33 +00:00
$ . ajax ( {
2015-01-12 15:43:13 +00:00
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
2014-08-31 06:25:22 +00:00
success : function ( response ) {
2016-11-24 15:31:05 +00:00
if ( response && response . billing ) {
$ . each ( response . shipping , function ( key , data ) {
$ ( ':input#_shipping_' + key ) . val ( data ) . change ( ) ;
2015-10-27 15:53:54 +00:00
} ) ;
2015-01-12 15:43:13 +00:00
}
2015-05-15 11:52:23 +00:00
$ ( 'div.edit_address' ) . unblock ( ) ;
2014-07-16 20:12:33 +00:00
}
} ) ;
}
return false ;
2015-01-12 15:43:13 +00:00
} ,
copy _billing _to _shipping : function ( ) {
if ( window . confirm ( woocommerce _admin _meta _boxes . copy _billing ) ) {
2015-10-27 15:53:54 +00:00
$ ( '.order_data_column :input[name^="_billing_"]' ) . each ( function ( ) {
var input _name = $ ( this ) . attr ( 'name' ) ;
input _name = input _name . replace ( '_billing_' , '_shipping_' ) ;
$ ( ':input#' + input _name ) . val ( $ ( this ) . val ( ) ) . change ( ) ;
} ) ;
2015-01-12 15:43:13 +00:00
}
return false ;
}
2015-07-08 05:19:56 +00:00
} ;
2015-01-12 15:43:13 +00:00
/ * *
* Order Items Panel
* /
var wc _meta _boxes _order _items = {
init : function ( ) {
2015-01-15 16:30:42 +00:00
this . stupidtable . init ( ) ;
2015-01-12 15:43:13 +00:00
$ ( '#woocommerce-order-items' )
. on ( 'click' , 'button.add-line-item' , this . add _line _item )
2017-08-09 13:55:53 +00:00
. on ( 'click' , 'button.add-discount' , this . add _discount )
2017-08-09 22:18:39 +00:00
. on ( 'click' , 'a.remove-coupon' , this . remove _coupon )
2015-01-12 15:43:13 +00:00
. on ( 'click' , 'button.refund-items' , this . refund _items )
. on ( 'click' , '.cancel-action' , this . cancel )
. on ( 'click' , 'button.add-order-item' , this . add _item )
. on ( 'click' , 'button.add-order-fee' , this . add _fee )
. on ( 'click' , 'button.add-order-shipping' , this . add _shipping )
. on ( 'click' , 'button.add-order-tax' , this . add _tax )
. on ( 'click' , 'button.save-action' , this . save _line _items )
. on ( 'click' , 'a.delete-order-tax' , this . delete _tax )
2017-04-18 18:44:42 +00:00
. on ( 'click' , 'button.calculate-action' , this . recalculate )
2015-01-12 15:43:13 +00:00
. on ( 'click' , 'a.edit-order-item' , this . edit _item )
. on ( 'click' , 'a.delete-order-item' , this . delete _item )
2017-08-09 18:02:10 +00:00
. on ( 'click' , 'tr.item, tr.fee, tr.discount, tr.shipping, tr.refund' , this . select _row )
. on ( 'click' , 'tr.item :input, tr.fee :input, tr.discount :input, tr.shipping :input, tr.refund :input, tr.item a, tr.fee a, tr.discount a, tr.shipping a, tr.refund a' , this . select _row _child )
2016-03-22 17:13:39 +00:00
. on ( 'click' , 'button.bulk-delete-items' , this . bulk _actions . do _delete )
. on ( 'click' , 'button.bulk-increase-stock' , this . bulk _actions . do _increase _stock )
. on ( 'click' , 'button.bulk-decrease-stock' , this . bulk _actions . do _reduce _stock )
2015-01-12 15:43:13 +00:00
// Refunds
. on ( 'click' , '.delete_refund' , this . refunds . delete _refund )
. on ( 'click' , 'button.do-api-refund, button.do-manual-refund' , this . refunds . do _refund )
2015-02-11 15:00:28 +00:00
. on ( 'change' , '.refund input.refund_line_total, .refund input.refund_line_tax' , this . refunds . input _changed )
2015-01-12 15:43:13 +00:00
. on ( 'change keyup' , '.wc-order-refund-items #refund_amount' , this . refunds . amount _changed )
. on ( 'change' , 'input.refund_order_item_qty' , this . refunds . refund _quantity _changed )
// Qty
. on ( 'change' , 'input.quantity' , this . quantity _changed )
// Subtotal/total
2016-03-21 18:35:00 +00:00
. on ( 'keyup change' , '.split-input :input' , function ( ) {
var $subtotal = $ ( this ) . parent ( ) . prev ( ) . find ( ':input' ) ;
if ( $subtotal && ( $subtotal . val ( ) === '' || $subtotal . is ( '.match-total' ) ) ) {
2015-01-12 15:43:13 +00:00
$subtotal . val ( $ ( this ) . val ( ) ) . addClass ( 'match-total' ) ;
}
} )
2016-03-21 18:35:00 +00:00
. on ( 'keyup' , '.split-input :input' , function ( ) {
2015-01-12 15:43:13 +00:00
$ ( this ) . removeClass ( 'match-total' ) ;
} )
// Meta
. on ( 'click' , 'button.add_order_item_meta' , this . item _meta . add )
. on ( 'click' , 'button.remove_order_item_meta' , this . item _meta . remove ) ;
2015-04-13 16:23:06 +00:00
$ ( document . body )
2015-01-12 15:43:13 +00:00
. on ( 'wc_backbone_modal_loaded' , this . backbone . init )
. on ( 'wc_backbone_modal_response' , this . backbone . response ) ;
} ,
block : function ( ) {
$ ( '#woocommerce-order-items' ) . block ( {
message : null ,
overlayCSS : {
background : '#fff' ,
opacity : 0.6
}
} ) ;
} ,
unblock : function ( ) {
$ ( '#woocommerce-order-items' ) . unblock ( ) ;
} ,
reload _items : function ( ) {
var data = {
order _id : woocommerce _admin _meta _boxes . post _id ,
action : 'woocommerce_load_order_items' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
wc _meta _boxes _order _items . block ( ) ;
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
success : function ( response ) {
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response ) ;
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2015-01-15 16:30:42 +00:00
wc _meta _boxes _order _items . stupidtable . init ( ) ;
2015-01-12 15:43:13 +00:00
}
} ) ;
} ,
2014-07-10 10:25:50 +00:00
// When the qty is changed, increase or decrease costs
2015-01-12 15:43:13 +00:00
quantity _changed : function ( ) {
2014-07-22 21:07:44 +00:00
var $row = $ ( this ) . closest ( 'tr.item' ) ;
var qty = $ ( this ) . val ( ) ;
var o _qty = $ ( this ) . attr ( 'data-qty' ) ;
var line _total = $ ( 'input.line_total' , $row ) ;
var line _subtotal = $ ( 'input.line_subtotal' , $row ) ;
// Totals
2014-09-10 14:07:24 +00:00
var unit _total = accounting . unformat ( line _total . attr ( 'data-total' ) , woocommerce _admin . mon _decimal _point ) / o _qty ;
2014-07-22 21:07:44 +00:00
line _total . val (
parseFloat ( accounting . formatNumber ( unit _total * qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
) ;
2014-09-10 14:07:24 +00:00
var unit _subtotal = accounting . unformat ( line _subtotal . attr ( 'data-subtotal' ) , woocommerce _admin . mon _decimal _point ) / o _qty ;
2014-07-22 21:07:44 +00:00
line _subtotal . val (
parseFloat ( accounting . formatNumber ( unit _subtotal * qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
) ;
// Taxes
2016-03-21 18:35:00 +00:00
$ ( 'input.line_tax' , $row ) . each ( function ( ) {
var $line _total _tax = $ ( this ) ;
var tax _id = $line _total _tax . data ( 'tax_id' ) ;
var unit _total _tax = accounting . unformat ( $line _total _tax . attr ( 'data-total_tax' ) , woocommerce _admin . mon _decimal _point ) / o _qty ;
var $line _subtotal _tax = $ ( 'input.line_subtotal_tax[data-tax_id="' + tax _id + '"]' , $row ) ;
var unit _subtotal _tax = accounting . unformat ( $line _subtotal _tax . attr ( 'data-subtotal_tax' ) , woocommerce _admin . mon _decimal _point ) / o _qty ;
2014-07-22 21:07:44 +00:00
if ( 0 < unit _total _tax ) {
2016-03-21 18:35:00 +00:00
$line _total _tax . val (
2014-07-22 21:07:44 +00:00
parseFloat ( accounting . formatNumber ( unit _total _tax * qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
) ;
}
if ( 0 < unit _subtotal _tax ) {
2016-03-21 18:35:00 +00:00
$line _subtotal _tax . val (
2014-07-22 21:07:44 +00:00
parseFloat ( accounting . formatNumber ( unit _subtotal _tax * qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
) ;
}
} ) ;
2014-07-10 10:25:50 +00:00
2014-07-22 19:52:36 +00:00
$ ( this ) . trigger ( 'quantity_changed' ) ;
2015-01-12 15:43:13 +00:00
} ,
2014-07-10 10:25:50 +00:00
2015-01-12 15:43:13 +00:00
add _line _item : function ( ) {
2014-07-17 02:08:59 +00:00
$ ( 'div.wc-order-add-item' ) . slideDown ( ) ;
2016-03-22 17:13:39 +00:00
$ ( 'div.wc-order-data-row-toggle' ) . not ( 'div.wc-order-add-item' ) . slideUp ( ) ;
2014-07-10 10:25:50 +00:00
return false ;
2017-08-09 13:55:53 +00:00
} ,
add _discount : function ( ) {
var value = window . prompt ( 'Enter a coupon code, percentage, or fixed discount amount.' ) ;
if ( value != null ) {
wc _meta _boxes _order _items . block ( ) ;
var data = {
action : 'woocommerce_add_order_discount' ,
dataType : 'json' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce ,
discount : value
} ;
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
if ( response . success ) {
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response . data . html ) ;
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
wc _meta _boxes _order _items . stupidtable . init ( ) ;
} else {
window . alert ( response . data . error ) ;
}
wc _meta _boxes _order _items . unblock ( ) ;
} ) ;
}
return false ;
2015-01-12 15:43:13 +00:00
} ,
2017-08-09 22:18:39 +00:00
remove _coupon : function ( ) {
var $this = $ ( this ) ;
wc _meta _boxes _order _items . block ( ) ;
var data = {
action : 'woocommerce_remove_order_coupon' ,
dataType : 'json' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce ,
coupon : $this . data ( 'code' )
}
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
if ( response . success ) {
2017-08-10 10:38:09 +00:00
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response . data . html ) ;
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
wc _meta _boxes _order _items . stupidtable . init ( ) ;
2017-08-09 22:18:39 +00:00
} else {
window . alert ( response . data . error ) ;
}
wc _meta _boxes _order _items . unblock ( ) ;
} ) ;
} ,
2015-01-12 15:43:13 +00:00
refund _items : function ( ) {
2014-07-17 02:08:59 +00:00
$ ( 'div.wc-order-refund-items' ) . slideDown ( ) ;
2016-03-22 17:13:39 +00:00
$ ( 'div.wc-order-data-row-toggle' ) . not ( 'div.wc-order-refund-items' ) . slideUp ( ) ;
2014-07-17 02:08:59 +00:00
$ ( 'div.wc-order-totals-items' ) . slideUp ( ) ;
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( 'div.refund' ) . show ( ) ;
2014-07-25 16:35:59 +00:00
$ ( '.wc-order-edit-line-item .wc-order-edit-line-item-actions' ) . hide ( ) ;
2014-07-10 10:25:50 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
cancel : function ( ) {
2016-03-22 17:13:39 +00:00
$ ( 'div.wc-order-data-row-toggle' ) . not ( 'div.wc-order-bulk-actions' ) . slideUp ( ) ;
2014-07-17 02:08:59 +00:00
$ ( 'div.wc-order-bulk-actions' ) . slideDown ( ) ;
$ ( 'div.wc-order-totals-items' ) . slideDown ( ) ;
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( 'div.refund' ) . hide ( ) ;
2014-07-25 16:35:59 +00:00
$ ( '.wc-order-edit-line-item .wc-order-edit-line-item-actions' ) . show ( ) ;
2014-07-17 02:08:59 +00:00
2014-07-17 20:17:54 +00:00
// Reload the items
if ( 'true' === $ ( this ) . attr ( 'data-reload' ) ) {
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . reload _items ( ) ;
2014-07-17 20:17:54 +00:00
}
2014-07-10 10:25:50 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
add _item : function ( ) {
2014-07-21 02:58:34 +00:00
$ ( this ) . WCBackboneModal ( {
2015-08-06 04:37:53 +00:00
template : 'wc-modal-add-products'
2014-07-21 02:58:34 +00:00
} ) ;
return false ;
2015-01-12 15:43:13 +00:00
} ,
add _fee : function ( ) {
wc _meta _boxes _order _items . block ( ) ;
2014-07-16 18:41:18 +00:00
var data = {
2016-08-25 13:22:27 +00:00
action : 'woocommerce_add_order_fee' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
dataType : 'json' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
2014-07-16 18:41:18 +00:00
} ;
2014-08-31 06:25:22 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
2016-08-25 13:22:27 +00:00
if ( response . success ) {
$ ( 'table.woocommerce_order_items tbody#order_fee_line_items' ) . append ( response . data . html ) ;
} else {
window . alert ( response . data . error ) ;
}
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . unblock ( ) ;
2014-07-16 18:41:18 +00:00
} ) ;
2014-07-21 02:58:34 +00:00
2014-07-16 18:41:18 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
add _shipping : function ( ) {
wc _meta _boxes _order _items . block ( ) ;
2013-08-16 15:43:26 +00:00
2012-11-12 18:53:40 +00:00
var data = {
2016-08-25 13:22:27 +00:00
action : 'woocommerce_add_order_shipping' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce ,
dataType : 'json'
2012-11-12 18:53:40 +00:00
} ;
2014-08-31 06:25:22 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
2016-08-25 13:22:27 +00:00
if ( response . success ) {
$ ( 'table.woocommerce_order_items tbody#order_shipping_line_items' ) . append ( response . data . html ) ;
} else {
window . alert ( response . data . error ) ;
}
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . unblock ( ) ;
2012-01-13 21:25:39 +00:00
} ) ;
2014-07-21 02:58:34 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
add _tax : function ( ) {
2014-07-21 02:58:34 +00:00
$ ( this ) . WCBackboneModal ( {
2015-08-06 04:37:53 +00:00
template : 'wc-modal-add-tax'
2014-07-21 02:58:34 +00:00
} ) ;
2014-07-10 10:25:50 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
2012-11-27 16:22:47 +00:00
2015-01-12 15:43:13 +00:00
edit _item : function ( ) {
$ ( this ) . closest ( 'tr' ) . find ( '.view' ) . hide ( ) ;
$ ( this ) . closest ( 'tr' ) . find ( '.edit' ) . show ( ) ;
$ ( this ) . hide ( ) ;
$ ( 'button.add-line-item' ) . click ( ) ;
$ ( 'button.cancel-action' ) . attr ( 'data-reload' , true ) ;
return false ;
} ,
2012-08-01 12:43:46 +00:00
2015-01-12 15:43:13 +00:00
delete _item : function ( ) {
var answer = window . confirm ( woocommerce _admin _meta _boxes . remove _item _notice ) ;
2012-08-01 12:43:46 +00:00
2015-01-12 15:43:13 +00:00
if ( answer ) {
2017-08-09 18:02:10 +00:00
var $item = $ ( this ) . closest ( 'tr.item, tr.fee, tr.discount, tr.shipping' ) ;
2015-01-12 15:43:13 +00:00
var order _item _id = $item . attr ( 'data-order_item_id' ) ;
2012-08-01 12:43:46 +00:00
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . block ( ) ;
2012-08-01 12:43:46 +00:00
2012-04-10 00:39:31 +00:00
var data = {
2015-01-12 15:43:13 +00:00
order _item _ids : order _item _id ,
action : 'woocommerce_remove_order_item' ,
2014-07-22 19:52:36 +00:00
security : woocommerce _admin _meta _boxes . order _item _nonce
2012-04-10 00:39:31 +00:00
} ;
2012-08-01 12:43:46 +00:00
2014-07-22 19:52:36 +00:00
$ . ajax ( {
2015-01-12 15:43:13 +00:00
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
2015-07-08 05:19:56 +00:00
success : function ( ) {
2015-01-12 15:43:13 +00:00
$item . remove ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2014-07-10 10:25:50 +00:00
}
2014-08-31 06:25:22 +00:00
} ) ;
2015-01-12 15:43:13 +00:00
}
return false ;
} ,
delete _tax : function ( ) {
if ( window . confirm ( woocommerce _admin _meta _boxes . i18n _delete _tax ) ) {
wc _meta _boxes _order _items . block ( ) ;
2012-11-27 16:22:47 +00:00
2014-07-10 10:25:50 +00:00
var data = {
2015-01-12 15:43:13 +00:00
action : 'woocommerce_remove_order_tax' ,
rate _id : $ ( this ) . attr ( 'data-rate_id' ) ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce
2014-07-10 10:25:50 +00:00
} ;
2012-11-12 13:41:54 +00:00
2014-07-22 19:52:36 +00:00
$ . ajax ( {
2015-01-12 15:43:13 +00:00
url : woocommerce _admin _meta _boxes . ajax _url ,
2014-07-10 10:25:50 +00:00
data : data ,
type : 'POST' ,
2014-08-31 06:25:22 +00:00
success : function ( response ) {
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response ) ;
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2015-01-15 16:30:42 +00:00
wc _meta _boxes _order _items . stupidtable . init ( ) ;
2014-07-10 10:25:50 +00:00
}
2014-07-22 19:52:36 +00:00
} ) ;
2014-07-10 10:25:50 +00:00
}
2014-07-18 20:24:34 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
2014-07-18 20:24:34 +00:00
2017-04-18 18:44:42 +00:00
recalculate : function ( ) {
if ( window . confirm ( woocommerce _admin _meta _boxes . calc _totals ) ) {
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . block ( ) ;
2014-07-18 20:24:34 +00:00
2015-10-05 14:31:58 +00:00
var country = '' ;
2015-01-12 15:43:13 +00:00
var state = '' ;
var postcode = '' ;
var city = '' ;
2015-10-05 14:31:58 +00:00
if ( 'shipping' === woocommerce _admin _meta _boxes . tax _based _on ) {
country = $ ( '#_shipping_country' ) . val ( ) ;
2015-01-12 15:43:13 +00:00
state = $ ( '#_shipping_state' ) . val ( ) ;
postcode = $ ( '#_shipping_postcode' ) . val ( ) ;
city = $ ( '#_shipping_city' ) . val ( ) ;
2015-10-05 14:31:58 +00:00
}
if ( 'billing' === woocommerce _admin _meta _boxes . tax _based _on || ! country ) {
country = $ ( '#_billing_country' ) . val ( ) ;
2015-01-12 15:43:13 +00:00
state = $ ( '#_billing_state' ) . val ( ) ;
postcode = $ ( '#_billing_postcode' ) . val ( ) ;
city = $ ( '#_billing_city' ) . val ( ) ;
}
var data = {
action : 'woocommerce_calc_line_taxes' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
items : $ ( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ) . serialize ( ) ,
country : country ,
state : state ,
postcode : postcode ,
city : city ,
security : woocommerce _admin _meta _boxes . calc _totals _nonce
} ;
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
success : function ( response ) {
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response ) ;
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2015-01-15 16:30:42 +00:00
wc _meta _boxes _order _items . stupidtable . init ( ) ;
2015-01-12 15:43:13 +00:00
}
} ) ;
}
return false ;
} ,
save _line _items : function ( ) {
2014-07-18 20:24:34 +00:00
var data = {
order _id : woocommerce _admin _meta _boxes . post _id ,
items : $ ( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ) . serialize ( ) ,
action : 'woocommerce_save_order_items' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . block ( ) ;
2014-07-18 20:24:34 +00:00
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
success : function ( response ) {
2015-09-07 17:51:10 +00:00
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response ) ;
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2015-01-15 16:30:42 +00:00
wc _meta _boxes _order _items . stupidtable . init ( ) ;
2014-07-18 20:24:34 +00:00
}
} ) ;
2014-09-10 04:17:12 +00:00
$ ( this ) . trigger ( 'items_saved' ) ;
2014-07-21 01:57:23 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
refunds : {
do _refund : function ( ) {
wc _meta _boxes _order _items . block ( ) ;
if ( window . confirm ( woocommerce _admin _meta _boxes . i18n _do _refund ) ) {
var refund _amount = $ ( 'input#refund_amount' ) . val ( ) ;
var refund _reason = $ ( 'input#refund_reason' ) . val ( ) ;
// Get line item refunds
var line _item _qtys = { } ;
var line _item _totals = { } ;
var line _item _tax _totals = { } ;
$ ( '.refund input.refund_order_item_qty' ) . each ( function ( index , item ) {
if ( $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ) {
if ( item . value ) {
line _item _qtys [ $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ] = item . value ;
}
}
} ) ;
$ ( '.refund input.refund_line_total' ) . each ( function ( index , item ) {
if ( $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ) {
line _item _totals [ $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ] = accounting . unformat ( item . value , woocommerce _admin . mon _decimal _point ) ;
}
} ) ;
$ ( '.refund input.refund_line_tax' ) . each ( function ( index , item ) {
if ( $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ) {
var tax _id = $ ( item ) . data ( 'tax_id' ) ;
if ( ! line _item _tax _totals [ $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ] ) {
line _item _tax _totals [ $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ] = { } ;
}
line _item _tax _totals [ $ ( item ) . closest ( 'tr' ) . data ( 'order_item_id' ) ] [ tax _id ] = accounting . unformat ( item . value , woocommerce _admin . mon _decimal _point ) ;
}
} ) ;
var data = {
action : 'woocommerce_refund_line_items' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
refund _amount : refund _amount ,
refund _reason : refund _reason ,
line _item _qtys : JSON . stringify ( line _item _qtys , null , '' ) ,
line _item _totals : JSON . stringify ( line _item _totals , null , '' ) ,
line _item _tax _totals : JSON . stringify ( line _item _tax _totals , null , '' ) ,
api _refund : $ ( this ) . is ( '.do-api-refund' ) ,
2016-03-01 13:08:25 +00:00
restock _refunded _items : $ ( '#restock_refunded_items:checked' ) . length ? 'true' : 'false' ,
2015-01-12 15:43:13 +00:00
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
if ( true === response . success ) {
wc _meta _boxes _order _items . reload _items ( ) ;
if ( 'fully_refunded' === response . data . status ) {
// Redirect to same page for show the refunded status
window . location . href = window . location . href ;
}
} else {
window . alert ( response . data . error ) ;
wc _meta _boxes _order _items . unblock ( ) ;
}
} ) ;
} else {
wc _meta _boxes _order _items . unblock ( ) ;
}
} ,
delete _refund : function ( ) {
if ( window . confirm ( woocommerce _admin _meta _boxes . i18n _delete _refund ) ) {
var $refund = $ ( this ) . closest ( 'tr.refund' ) ;
var refund _id = $refund . attr ( 'data-order_refund_id' ) ;
wc _meta _boxes _order _items . block ( ) ;
var data = {
action : 'woocommerce_delete_refund' ,
refund _id : refund _id ,
2015-07-08 05:19:56 +00:00
security : woocommerce _admin _meta _boxes . order _item _nonce
2015-01-12 15:43:13 +00:00
} ;
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
2015-07-08 05:19:56 +00:00
success : function ( ) {
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . reload _items ( ) ;
}
} ) ;
}
return false ;
} ,
input _changed : function ( ) {
var refund _amount = 0 ;
2017-08-09 18:02:10 +00:00
var $items = $ ( '.woocommerce_order_items' ) . find ( 'tr.item, tr.fee, tr.discount, tr.shipping' ) ;
2015-01-12 15:43:13 +00:00
$items . each ( function ( ) {
var $row = $ ( this ) ;
var refund _cost _fields = $row . find ( '.refund input:not(.refund_order_item_qty)' ) ;
refund _cost _fields . each ( function ( index , el ) {
refund _amount += parseFloat ( accounting . unformat ( $ ( el ) . val ( ) || 0 , woocommerce _admin . mon _decimal _point ) ) ;
} ) ;
} ) ;
$ ( '#refund_amount' )
. val ( accounting . formatNumber (
refund _amount ,
woocommerce _admin _meta _boxes . currency _format _num _decimals ,
'' ,
woocommerce _admin . mon _decimal _point
) )
. change ( ) ;
} ,
amount _changed : function ( ) {
var total = accounting . unformat ( $ ( this ) . val ( ) , woocommerce _admin . mon _decimal _point ) ;
$ ( 'button .wc-order-refund-amount .amount' ) . text ( accounting . formatMoney ( total , {
symbol : woocommerce _admin _meta _boxes . currency _format _symbol ,
decimal : woocommerce _admin _meta _boxes . currency _format _decimal _sep ,
thousand : woocommerce _admin _meta _boxes . currency _format _thousand _sep ,
precision : woocommerce _admin _meta _boxes . currency _format _num _decimals ,
format : woocommerce _admin _meta _boxes . currency _format
} ) ) ;
} ,
// When the refund qty is changed, increase or decrease costs
refund _quantity _changed : function ( ) {
var $row = $ ( this ) . closest ( 'tr.item' ) ;
var qty = $row . find ( 'input.quantity' ) . val ( ) ;
var refund _qty = $ ( this ) . val ( ) ;
var line _total = $ ( 'input.line_total' , $row ) ;
var refund _line _total = $ ( 'input.refund_line_total' , $row ) ;
// Totals
var unit _total = accounting . unformat ( line _total . attr ( 'data-total' ) , woocommerce _admin . mon _decimal _point ) / qty ;
refund _line _total . val (
parseFloat ( accounting . formatNumber ( unit _total * refund _qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
2015-02-04 15:12:47 +00:00
) . change ( ) ;
2015-01-12 15:43:13 +00:00
// Taxes
2016-03-21 18:35:00 +00:00
$ ( '.refund_line_tax' , $row ) . each ( function ( ) {
var $refund _line _total _tax = $ ( this ) ;
var tax _id = $refund _line _total _tax . data ( 'tax_id' ) ;
var line _total _tax = $ ( 'input.line_tax[data-tax_id="' + tax _id + '"]' , $row ) ;
var unit _total _tax = accounting . unformat ( line _total _tax . data ( 'total_tax' ) , woocommerce _admin . mon _decimal _point ) / qty ;
2015-01-12 15:43:13 +00:00
if ( 0 < unit _total _tax ) {
2016-03-21 18:35:00 +00:00
$refund _line _total _tax . val (
2015-01-12 15:43:13 +00:00
parseFloat ( accounting . formatNumber ( unit _total _tax * refund _qty , woocommerce _admin _meta _boxes . rounding _precision , '' ) )
. toString ( )
. replace ( '.' , woocommerce _admin . mon _decimal _point )
) . change ( ) ;
2015-01-15 18:57:33 +00:00
} else {
2016-03-21 18:35:00 +00:00
$refund _line _total _tax . val ( 0 ) . change ( ) ;
2015-01-12 15:43:13 +00:00
}
} ) ;
// Restock checkbox
if ( refund _qty > 0 ) {
2015-01-15 18:57:33 +00:00
$ ( '#restock_refunded_items' ) . closest ( 'tr' ) . show ( ) ;
2015-01-12 15:43:13 +00:00
} else {
2015-01-15 18:57:33 +00:00
$ ( '#restock_refunded_items' ) . closest ( 'tr' ) . hide ( ) ;
$ ( '.woocommerce_order_items input.refund_order_item_qty' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) > 0 ) {
$ ( '#restock_refunded_items' ) . closest ( 'tr' ) . show ( ) ;
2015-01-12 15:43:13 +00:00
}
} ) ;
}
$ ( this ) . trigger ( 'refund_quantity_changed' ) ;
}
} ,
item _meta : {
add : function ( ) {
var $button = $ ( this ) ;
2016-01-08 11:42:32 +00:00
var $item = $button . closest ( 'tr.item, tr.shipping' ) ;
2016-08-19 12:43:33 +00:00
var $items = $item . find ( 'tbody.meta_items' ) ;
var index = $items . find ( 'tr' ) . length + 1 ;
var $row = '<tr data-meta_id="0">' +
'<td>' +
2017-04-26 10:35:34 +00:00
'<input type="text" placeholder="' + woocommerce _admin _meta _boxes _order . placeholder _name + '" name="meta_key[' + $item . attr ( 'data-order_item_id' ) + '][new-' + index + ']" />' +
'<textarea placeholder="' + woocommerce _admin _meta _boxes _order . placeholder _value + '" name="meta_value[' + $item . attr ( 'data-order_item_id' ) + '][new-' + index + ']"></textarea>' +
2016-08-19 12:43:33 +00:00
'</td>' +
'<td width="1%"><button class="remove_order_item_meta button">×</button></td>' +
'</tr>' ;
$items . append ( $row ) ;
2015-01-12 15:43:13 +00:00
return false ;
} ,
remove : function ( ) {
if ( window . confirm ( woocommerce _admin _meta _boxes . remove _item _meta ) ) {
var $row = $ ( this ) . closest ( 'tr' ) ;
2016-08-19 12:43:33 +00:00
$row . find ( ':input' ) . val ( '' ) ;
$row . hide ( ) ;
2015-01-12 15:43:13 +00:00
}
return false ;
2014-07-22 14:26:18 +00:00
}
2015-01-12 15:43:13 +00:00
} ,
2014-07-24 20:33:26 +00:00
2016-03-22 17:13:39 +00:00
select _row : function ( ) {
var $row = false ;
if ( $ ( this ) . is ( 'tr' ) ) {
$row = $ ( this ) ;
} else {
2016-03-23 13:06:15 +00:00
$row = $ ( this ) . closest ( 'tr' ) ;
2016-03-22 17:13:39 +00:00
}
2016-03-23 13:06:15 +00:00
var $table = $ ( this ) . closest ( 'table' ) ;
2016-03-22 17:13:39 +00:00
if ( $row . is ( '.selected' ) ) {
2016-03-23 13:06:15 +00:00
$row . removeClass ( 'selected' ) ;
2016-03-22 17:13:39 +00:00
} else {
2016-03-23 13:06:15 +00:00
$row . addClass ( 'selected' ) ;
2016-03-22 17:13:39 +00:00
}
2016-03-23 13:06:15 +00:00
var $rows = $table . find ( 'tr.selected' ) ;
2016-03-22 17:13:39 +00:00
if ( $rows . length ) {
$ ( 'div.wc-order-item-bulk-edit' ) . slideDown ( ) ;
2016-03-23 16:32:12 +00:00
var selected _product = false ;
$rows . each ( function ( ) {
if ( $ ( this ) . is ( 'tr.item' ) ) {
selected _product = true ;
}
} ) ;
if ( selected _product ) {
$ ( '.bulk-increase-stock, .bulk-decrease-stock' ) . show ( ) ;
} else {
$ ( '.bulk-increase-stock, .bulk-decrease-stock' ) . hide ( ) ;
}
2016-03-22 17:13:39 +00:00
} else {
$ ( 'div.wc-order-item-bulk-edit' ) . slideUp ( ) ;
}
} ,
select _row _child : function ( e ) {
e . stopPropagation ( ) ;
} ,
2015-01-12 15:43:13 +00:00
bulk _actions : {
2014-07-24 20:33:26 +00:00
2016-03-22 17:13:39 +00:00
do _delete : function ( e ) {
e . preventDefault ( ) ;
var $table = $ ( 'table.woocommerce_order_items' ) ;
2016-03-23 13:06:15 +00:00
var $rows = $table . find ( 'tr.selected' ) ;
2015-01-12 15:43:13 +00:00
2016-03-22 17:13:39 +00:00
if ( $rows . length && window . confirm ( woocommerce _admin _meta _boxes . remove _item _notice ) ) {
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . block ( ) ;
2016-03-23 16:32:12 +00:00
var delete _items = [ ] ;
var delete _refunds = [ ] ;
var deferred = [ ] ;
2016-03-22 17:13:39 +00:00
2016-03-23 16:32:12 +00:00
$ . map ( $rows , function ( row ) {
var $row = $ ( row ) ;
2015-01-12 15:43:13 +00:00
2016-03-23 16:32:12 +00:00
if ( $row . is ( '.refund' ) ) {
delete _refunds . push ( parseInt ( $ ( $row ) . data ( 'order_refund_id' ) , 10 ) ) ;
} else {
delete _items . push ( parseInt ( $ ( $row ) . data ( 'order_item_id' ) , 10 ) ) ;
2015-01-12 15:43:13 +00:00
}
2016-03-23 16:32:12 +00:00
return ;
2015-01-12 15:43:13 +00:00
} ) ;
2016-03-23 16:32:12 +00:00
if ( delete _items . length ) {
deferred . push ( $ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : {
order _item _ids : delete _items ,
action : 'woocommerce_remove_order_item' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ,
type : 'POST'
} ) ) ;
}
if ( delete _refunds . length ) {
deferred . push ( $ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : {
action : 'woocommerce_delete_refund' ,
refund _id : delete _refunds ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ,
type : 'POST'
} ) ) ;
}
if ( deferred ) {
$ . when . apply ( $ , deferred ) . done ( function ( ) {
wc _meta _boxes _order _items . reload _items ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
} ) ;
} else {
wc _meta _boxes _order _items . unblock ( ) ;
}
2015-01-12 15:43:13 +00:00
}
2016-03-22 17:13:39 +00:00
} ,
do _increase _stock : function ( e ) {
e . preventDefault ( ) ;
wc _meta _boxes _order _items . block ( ) ;
var $table = $ ( 'table.woocommerce_order_items' ) ;
2016-03-23 13:06:15 +00:00
var $rows = $table . find ( 'tr.selected' ) ;
2016-03-22 17:13:39 +00:00
var quantities = { } ;
var item _ids = $ . map ( $rows , function ( $row ) {
return parseInt ( $ ( $row ) . data ( 'order_item_id' ) , 10 ) ;
} ) ;
$rows . each ( function ( ) {
if ( $ ( this ) . find ( 'input.quantity' ) . length ) {
quantities [ $ ( this ) . attr ( 'data-order_item_id' ) ] = $ ( this ) . find ( 'input.quantity' ) . val ( ) ;
}
} ) ;
var data = {
order _id : woocommerce _admin _meta _boxes . post _id ,
order _item _ids : item _ids ,
order _item _qty : quantities ,
action : 'woocommerce_increase_order_item_stock' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
success : function ( response ) {
window . alert ( response ) ;
wc _meta _boxes _order _items . unblock ( ) ;
}
} ) ;
} ,
do _reduce _stock : function ( e ) {
e . preventDefault ( ) ;
wc _meta _boxes _order _items . block ( ) ;
var $table = $ ( 'table.woocommerce_order_items' ) ;
2016-03-23 13:06:15 +00:00
var $rows = $table . find ( 'tr.selected' ) ;
2016-03-22 17:13:39 +00:00
var quantities = { } ;
var item _ids = $ . map ( $rows , function ( $row ) {
return parseInt ( $ ( $row ) . data ( 'order_item_id' ) , 10 ) ;
} ) ;
$rows . each ( function ( ) {
if ( $ ( this ) . find ( 'input.quantity' ) . length ) {
quantities [ $ ( this ) . attr ( 'data-order_item_id' ) ] = $ ( this ) . find ( 'input.quantity' ) . val ( ) ;
}
} ) ;
var data = {
order _id : woocommerce _admin _meta _boxes . post _id ,
order _item _ids : item _ids ,
order _item _qty : quantities ,
action : 'woocommerce_reduce_order_item_stock' ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
$ . ajax ( {
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
type : 'POST' ,
success : function ( response ) {
window . alert ( response ) ;
wc _meta _boxes _order _items . unblock ( ) ;
}
} ) ;
2014-07-10 12:33:05 +00:00
}
2015-01-12 15:43:13 +00:00
} ,
2012-11-27 16:22:47 +00:00
2015-01-12 15:43:13 +00:00
backbone : {
2014-07-16 18:11:48 +00:00
2015-01-12 15:43:13 +00:00
init : function ( e , target ) {
2015-08-06 04:37:53 +00:00
if ( 'wc-modal-add-products' === target ) {
2015-04-13 15:37:22 +00:00
$ ( document . body ) . trigger ( 'wc-enhanced-select-init' ) ;
2015-01-12 15:43:13 +00:00
}
} ,
2014-07-16 18:11:48 +00:00
2015-01-12 17:11:01 +00:00
response : function ( e , target , data ) {
2015-08-06 04:37:53 +00:00
if ( 'wc-modal-add-tax' === target ) {
2015-01-12 17:11:01 +00:00
var rate _id = data . add _order _tax ;
var manual _rate _id = '' ;
if ( data . manual _tax _rate _id ) {
manual _rate _id = data . manual _tax _rate _id ;
}
wc _meta _boxes _order _items . backbone . add _tax ( rate _id , manual _rate _id ) ;
2015-01-12 15:43:13 +00:00
}
2015-08-06 04:37:53 +00:00
if ( 'wc-modal-add-products' === target ) {
2015-01-12 17:11:01 +00:00
wc _meta _boxes _order _items . backbone . add _item ( data . add _order _items ) ;
2015-01-12 15:43:13 +00:00
}
} ,
2014-07-16 18:11:48 +00:00
2015-01-12 17:11:01 +00:00
add _item : function ( add _item _ids ) {
2015-01-12 15:43:13 +00:00
if ( add _item _ids ) {
wc _meta _boxes _order _items . block ( ) ;
2014-07-16 18:11:48 +00:00
2017-02-23 10:19:32 +00:00
var data = {
action : 'woocommerce_add_order_item' ,
item _to _add : add _item _ids ,
dataType : 'json' ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
2014-07-16 18:11:48 +00:00
2017-02-23 10:19:32 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
if ( response . success ) {
$ ( 'table.woocommerce_order_items tbody#order_line_items' ) . append ( response . data . html ) ;
} else {
window . alert ( response . data . error ) ;
}
2014-07-16 18:11:48 +00:00
2017-02-23 10:19:32 +00:00
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . unblock ( ) ;
2015-01-12 15:43:13 +00:00
} ) ;
}
} ,
2014-07-21 01:36:12 +00:00
2015-01-12 17:11:01 +00:00
add _tax : function ( rate _id , manual _rate _id ) {
2015-01-12 15:43:13 +00:00
if ( manual _rate _id ) {
rate _id = manual _rate _id ;
}
if ( ! rate _id ) {
return false ;
}
var rates = $ ( '.order-tax-id' ) . map ( function ( ) {
return $ ( this ) . val ( ) ;
} ) . get ( ) ;
// Test if already exists
if ( - 1 === $ . inArray ( rate _id , rates ) ) {
wc _meta _boxes _order _items . block ( ) ;
var data = {
action : 'woocommerce_add_order_tax' ,
rate _id : rate _id ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . order _item _nonce
} ;
$ . ajax ( {
2016-08-25 13:22:27 +00:00
url : woocommerce _admin _meta _boxes . ajax _url ,
data : data ,
dataType : 'json' ,
type : 'POST' ,
success : function ( response ) {
if ( response . success ) {
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . empty ( ) ;
$ ( '#woocommerce-order-items' ) . find ( '.inside' ) . append ( response . data . html ) ;
wc _meta _boxes _order . init _tiptip ( ) ;
wc _meta _boxes _order _items . stupidtable . init ( ) ;
} else {
window . alert ( response . data . error ) ;
}
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order _items . unblock ( ) ;
}
} ) ;
} else {
window . alert ( woocommerce _admin _meta _boxes . i18n _tax _rate _already _exists ) ;
}
}
2015-01-15 16:30:42 +00:00
} ,
stupidtable : {
init : function ( ) {
2016-03-22 14:54:02 +00:00
$ ( '.woocommerce_order_items' ) . stupidtable ( ) ;
$ ( '.woocommerce_order_items' ) . on ( 'aftertablesort' , this . add _arrows ) ;
2015-01-15 16:30:42 +00:00
} ,
add _arrows : function ( event , data ) {
var th = $ ( this ) . find ( 'th' ) ;
var arrow = data . direction === 'asc' ? '↑' : '↓' ;
var index = data . column ;
th . find ( '.wc-arrow' ) . remove ( ) ;
th . eq ( index ) . append ( '<span class="wc-arrow">' + arrow + '</span>' ) ;
}
2014-07-23 10:30:06 +00:00
}
2015-01-12 15:43:13 +00:00
} ;
2014-07-23 10:30:06 +00:00
2015-01-12 15:43:13 +00:00
/ * *
* Order Notes Panel
* /
var wc _meta _boxes _order _notes = {
init : function ( ) {
$ ( '#woocommerce-order-notes' )
2016-11-05 16:56:03 +00:00
. on ( 'click' , 'button.add_note' , this . add _order _note )
2015-01-12 15:43:13 +00:00
. on ( 'click' , 'a.delete_note' , this . delete _order _note ) ;
} ,
add _order _note : function ( ) {
if ( ! $ ( 'textarea#add_order_note' ) . val ( ) ) {
return ;
}
2014-07-21 01:36:12 +00:00
2015-01-12 15:43:13 +00:00
$ ( '#woocommerce-order-notes' ) . block ( {
message : null ,
overlayCSS : {
background : '#fff' ,
opacity : 0.6
}
} ) ;
2014-07-21 01:36:12 +00:00
2014-07-22 15:42:17 +00:00
var data = {
2015-01-12 15:43:13 +00:00
action : 'woocommerce_add_order_note' ,
post _id : woocommerce _admin _meta _boxes . post _id ,
note : $ ( 'textarea#add_order_note' ) . val ( ) ,
note _type : $ ( 'select#order_note_type' ) . val ( ) ,
2015-07-08 05:19:56 +00:00
security : woocommerce _admin _meta _boxes . add _order _note _nonce
2014-07-22 15:42:17 +00:00
} ;
2014-07-21 01:36:12 +00:00
2015-01-12 15:43:13 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
$ ( 'ul.order_notes' ) . prepend ( response ) ;
$ ( '#woocommerce-order-notes' ) . unblock ( ) ;
$ ( '#add_order_note' ) . val ( '' ) ;
} ) ;
return false ;
} ,
delete _order _note : function ( ) {
2016-05-12 08:04:32 +00:00
if ( window . confirm ( woocommerce _admin _meta _boxes . i18n _delete _note ) ) {
var note = $ ( this ) . closest ( 'li.note' ) ;
2016-05-12 19:48:01 +00:00
2016-05-12 08:04:32 +00:00
$ ( note ) . block ( {
message : null ,
overlayCSS : {
background : '#fff' ,
opacity : 0.6
}
} ) ;
2016-05-12 19:48:01 +00:00
2016-05-12 08:04:32 +00:00
var data = {
action : 'woocommerce_delete_order_note' ,
note _id : $ ( note ) . attr ( 'rel' ) ,
security : woocommerce _admin _meta _boxes . delete _order _note _nonce
} ;
2016-05-12 19:48:01 +00:00
2016-05-12 08:04:32 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( ) {
$ ( note ) . remove ( ) ;
} ) ;
}
2016-05-12 19:48:01 +00:00
return false ;
2015-07-08 05:19:56 +00:00
}
2015-03-03 13:41:52 +00:00
} ;
2012-11-27 16:22:47 +00:00
2015-01-12 15:43:13 +00:00
/ * *
* Order Downloads Panel
* /
var wc _meta _boxes _order _downloads = {
init : function ( ) {
$ ( '.order_download_permissions' )
. on ( 'click' , 'button.grant_access' , this . grant _access )
. on ( 'click' , 'button.revoke_access' , this . revoke _access ) ;
} ,
grant _access : function ( ) {
var products = $ ( '#grant_access_id' ) . val ( ) ;
2012-11-27 16:22:47 +00:00
2014-07-22 19:52:36 +00:00
if ( ! products ) {
return ;
}
$ ( '.order_download_permissions' ) . block ( {
message : null ,
overlayCSS : {
2014-11-13 18:28:15 +00:00
background : '#fff' ,
2014-07-22 19:52:36 +00:00
opacity : 0.6
}
} ) ;
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var data = {
2014-07-22 19:52:36 +00:00
action : 'woocommerce_grant_access_to_download' ,
product _ids : products ,
2016-03-01 13:08:25 +00:00
loop : $ ( '.order_download_permissions .wc-metabox' ) . length ,
2014-07-22 19:52:36 +00:00
order _id : woocommerce _admin _meta _boxes . post _id ,
2015-07-08 05:19:56 +00:00
security : woocommerce _admin _meta _boxes . grant _access _nonce
2012-11-12 12:22:35 +00:00
} ;
2012-11-27 16:22:47 +00:00
2014-07-10 10:25:50 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( response ) {
2013-11-27 14:03:04 +00:00
2014-07-10 10:25:50 +00:00
if ( response ) {
2014-07-22 19:52:36 +00:00
$ ( '.order_download_permissions .wc-metaboxes' ) . append ( response ) ;
2014-07-10 10:25:50 +00:00
} else {
2014-07-22 19:52:36 +00:00
window . alert ( woocommerce _admin _meta _boxes . i18n _download _permission _fail ) ;
2014-07-10 10:25:50 +00:00
}
2013-11-27 14:03:04 +00:00
2015-04-13 15:37:22 +00:00
$ ( document . body ) . trigger ( 'wc-init-datepickers' ) ;
2015-01-12 13:04:19 +00:00
$ ( '#grant_access_id' ) . val ( '' ) . change ( ) ;
2014-07-22 19:52:36 +00:00
$ ( '.order_download_permissions' ) . unblock ( ) ;
2013-11-27 14:03:04 +00:00
} ) ;
2014-07-10 10:25:50 +00:00
return false ;
2015-01-12 15:43:13 +00:00
} ,
revoke _access : function ( ) {
2014-07-22 19:52:36 +00:00
if ( window . confirm ( woocommerce _admin _meta _boxes . i18n _permission _revoke ) ) {
2016-07-14 13:54:28 +00:00
var el = $ ( this ) . parent ( ) . parent ( ) ;
var product = $ ( this ) . attr ( 'rel' ) . split ( ',' ) [ 0 ] ;
var file = $ ( this ) . attr ( 'rel' ) . split ( ',' ) [ 1 ] ;
var permission _id = $ ( this ) . data ( 'permission_id' ) ;
2014-07-10 10:25:50 +00:00
if ( product > 0 ) {
2014-07-22 19:52:36 +00:00
$ ( el ) . block ( {
message : null ,
overlayCSS : {
2014-11-13 18:28:15 +00:00
background : '#fff' ,
2014-07-22 19:52:36 +00:00
opacity : 0.6
}
} ) ;
2014-07-10 10:25:50 +00:00
var data = {
2016-07-14 13:54:28 +00:00
action : 'woocommerce_revoke_access_to_download' ,
product _id : product ,
download _id : file ,
permission _id : permission _id ,
order _id : woocommerce _admin _meta _boxes . post _id ,
security : woocommerce _admin _meta _boxes . revoke _access _nonce
2014-07-10 10:25:50 +00:00
} ;
2015-07-08 05:19:56 +00:00
$ . post ( woocommerce _admin _meta _boxes . ajax _url , data , function ( ) {
2014-07-10 10:25:50 +00:00
// Success
2014-07-22 19:52:36 +00:00
$ ( el ) . fadeOut ( '300' , function ( ) {
$ ( el ) . remove ( ) ;
2014-07-10 10:25:50 +00:00
} ) ;
} ) ;
2013-11-27 14:03:04 +00:00
2014-07-10 10:25:50 +00:00
} else {
2014-07-22 19:52:36 +00:00
$ ( el ) . fadeOut ( '300' , function ( ) {
$ ( el ) . remove ( ) ;
2013-11-27 14:03:04 +00:00
} ) ;
2014-07-10 10:25:50 +00:00
}
2013-11-27 14:03:04 +00:00
}
2014-07-10 10:25:50 +00:00
return false ;
2011-11-26 16:15:25 +00:00
}
2015-03-03 13:41:52 +00:00
} ;
2014-11-26 15:37:06 +00:00
2015-01-12 15:43:13 +00:00
wc _meta _boxes _order . init ( ) ;
wc _meta _boxes _order _items . init ( ) ;
wc _meta _boxes _order _notes . init ( ) ;
wc _meta _boxes _order _downloads . init ( ) ;
2015-01-12 17:11:01 +00:00
} ) ;