2017-05-17 21:34:49 +00:00
|
|
|
/*global ajaxurl, wc_product_import_params */
|
|
|
|
;(function ( $, window ) {
|
|
|
|
|
|
|
|
/**
|
2017-05-17 22:00:16 +00:00
|
|
|
* productImportForm handles the import process.
|
2017-05-17 21:34:49 +00:00
|
|
|
*/
|
|
|
|
var productImportForm = function( $form ) {
|
2017-05-19 17:58:31 +00:00
|
|
|
this.$form = $form;
|
|
|
|
this.xhr = false;
|
|
|
|
this.mapping = wc_product_import_params.mapping;
|
|
|
|
this.position = 0;
|
|
|
|
this.file = wc_product_import_params.file;
|
|
|
|
this.skip_existing = wc_product_import_params.skip_existing;
|
|
|
|
this.security = wc_product_import_params.import_nonce;
|
2017-05-17 21:34:49 +00:00
|
|
|
|
|
|
|
// Number of import successes/failures.
|
|
|
|
this.imported = 0;
|
2017-05-18 16:49:58 +00:00
|
|
|
this.failed = 0;
|
2017-05-17 21:34:49 +00:00
|
|
|
|
|
|
|
// Initial state.
|
|
|
|
this.$form.find('.woocommerce-importer-progress').val( 0 );
|
|
|
|
|
|
|
|
this.run_import = this.run_import.bind( this );
|
|
|
|
|
|
|
|
//Start importing.
|
|
|
|
this.run_import();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the import in batches until finished.
|
|
|
|
*/
|
|
|
|
productImportForm.prototype.run_import = function() {
|
|
|
|
var $this = this;
|
|
|
|
|
|
|
|
$.ajax( {
|
|
|
|
type: 'POST',
|
|
|
|
url: ajaxurl,
|
|
|
|
data: {
|
2017-05-19 17:58:31 +00:00
|
|
|
action : 'woocommerce_do_ajax_product_import',
|
|
|
|
position : $this.position,
|
|
|
|
mapping : $this.mapping,
|
|
|
|
file : $this.file,
|
|
|
|
skip_existing : $this.skip_existing,
|
|
|
|
security : $this.security
|
2017-05-17 21:34:49 +00:00
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
success: function( response ) {
|
|
|
|
if ( response.success ) {
|
2017-05-18 16:49:58 +00:00
|
|
|
$this.position = response.data.position;
|
2017-05-17 21:34:49 +00:00
|
|
|
$this.imported += response.data.imported;
|
2017-05-18 16:49:58 +00:00
|
|
|
$this.failed += response.data.failed;
|
2017-05-17 21:34:49 +00:00
|
|
|
$this.$form.find('.woocommerce-importer-progress').val( response.data.percentage );
|
|
|
|
|
|
|
|
if ( 'done' === response.data.position ) {
|
|
|
|
window.location = response.data.url + '&imported=' + parseInt( $this.imported, 10 ) + '&failed=' + parseInt( $this.failed, 10 );
|
|
|
|
} else {
|
|
|
|
$this.run_import();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ).fail( function( response ) {
|
|
|
|
window.console.log( response );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to call productImportForm on jQuery selector.
|
|
|
|
*/
|
|
|
|
$.fn.wc_product_importer = function() {
|
|
|
|
new productImportForm( this );
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
$( '.woocommerce-importer' ).wc_product_importer();
|
|
|
|
|
|
|
|
})( jQuery, window );
|