quick-edit.js 5.53 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/*global inlineEditPost, woocommerce_admin, woocommerce_quick_edit */
jQuery(function( $ ) {
	$( '#the-list' ).on( 'click', '.editinline', function() {

		inlineEditPost.revert();

		var post_id = $( this ).closest( 'tr' ).attr( 'id' );

		post_id = post_id.replace( 'post-', '' );

		var $wc_inline_data = $( '#woocommerce_inline_' + post_id );

		var sku            = $wc_inline_data.find( '.sku' ).text(),
			regular_price  = $wc_inline_data.find( '.regular_price' ).text(),
			sale_price     = $wc_inline_data.find( '.sale_price ').text(),
			weight         = $wc_inline_data.find( '.weight' ).text(),
			length         = $wc_inline_data.find( '.length' ).text(),
			width          = $wc_inline_data.find( '.width' ).text(),
			height         = $wc_inline_data.find( '.height' ).text(),
			shipping_class = $wc_inline_data.find( '.shipping_class' ).text(),
			visibility     = $wc_inline_data.find( '.visibility' ).text(),
			stock_status   = $wc_inline_data.find( '.stock_status' ).text(),
			stock          = $wc_inline_data.find( '.stock' ).text(),
			featured       = $wc_inline_data.find( '.featured' ).text(),
			manage_stock   = $wc_inline_data.find( '.manage_stock' ).text(),
			menu_order     = $wc_inline_data.find( '.menu_order' ).text(),
			tax_status     = $wc_inline_data.find( '.tax_status' ).text(),
			tax_class      = $wc_inline_data.find( '.tax_class' ).text(),
			backorders     = $wc_inline_data.find( '.backorders' ).text();

		var formatted_regular_price = regular_price.replace('.', woocommerce_admin.mon_decimal_point ),
			formatted_sale_price	= sale_price.replace('.', woocommerce_admin.mon_decimal_point );

		$( 'input[name="_sku"]', '.inline-edit-row' ).val( sku );
		$( 'input[name="_regular_price"]', '.inline-edit-row' ).val( formatted_regular_price );
		$( 'input[name="_sale_price"]', '.inline-edit-row' ).val( formatted_sale_price );
		$( 'input[name="_weight"]', '.inline-edit-row' ).val( weight );
		$( 'input[name="_length"]', '.inline-edit-row' ).val( length );
		$( 'input[name="_width"]', '.inline-edit-row' ).val( width );
		$( 'input[name="_height"]', '.inline-edit-row' ).val( height );

		$( 'select[name="_shipping_class"] option:selected', '.inline-edit-row' ).attr( 'selected', false ).change();
		$( 'select[name="_shipping_class"] option[value="' + shipping_class + '"]' ).attr( 'selected', 'selected' ).change();

		$( 'input[name="_stock"]', '.inline-edit-row' ).val( stock );
		$( 'input[name="menu_order"]', '.inline-edit-row' ).val( menu_order );

		$( 'select[name="_tax_status"] option, select[name="_tax_class"] option, select[name="_visibility"] option, select[name="_stock_status"] option, select[name="_backorders"] option' ).removeAttr( 'selected' );

		$( 'select[name="_tax_status"] option[value="' + tax_status + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
		$( 'select[name="_tax_class"] option[value="' + tax_class + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
		$( 'select[name="_visibility"] option[value="' + visibility + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
		$( 'select[name="_stock_status"] option[value="' + stock_status + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
		$( 'select[name="_backorders"] option[value="' + backorders + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );

		if ( 'yes' === featured ) {
			$( 'input[name="_featured"]', '.inline-edit-row' ).attr( 'checked', 'checked' );
		} else {
			$( 'input[name="_featured"]', '.inline-edit-row' ).removeAttr( 'checked' );
		}

		if ( 'yes' === manage_stock ) {
			$( '.stock_qty_field', '.inline-edit-row' ).show().removeAttr( 'style' );
			$( 'input[name="_manage_stock"]', '.inline-edit-row' ).attr( 'checked', 'checked' );
		} else {
			$( '.stock_qty_field', '.inline-edit-row' ).hide();
			$( 'input[name="_manage_stock"]', '.inline-edit-row' ).removeAttr( 'checked' );
		}

		// Conditional display
		var product_type       = $wc_inline_data.find( '.product_type' ).text(),
			product_is_virtual = $wc_inline_data.find( '.product_is_virtual' ).text();

		if ( 'simple' === product_type || 'external' === product_type ) {
			$( '.price_fields', '.inline-edit-row' ).show().removeAttr( 'style' );
		} else {
			$( '.price_fields', '.inline-edit-row' ).hide();
		}

		if ( 'yes' === product_is_virtual ) {
			$( '.dimension_fields', '.inline-edit-row' ).hide();
		} else {
			$( '.dimension_fields', '.inline-edit-row' ).show().removeAttr( 'style' );
		}

		if ( 'grouped' === product_type ) {
			$( '.stock_fields', '.inline-edit-row' ).hide();
		} else {
			$( '.stock_fields', '.inline-edit-row' ).show().removeAttr( 'style' );
		}

		// Rename core strings
		$( 'input[name="comment_status"]' ).parent().find( '.checkbox-title' ).text( woocommerce_quick_edit.strings.allow_reviews );
	});

	$( '#the-list' ).on( 'change', '.inline-edit-row input[name="_manage_stock"]', function() {

		if ( $( this ).is( ':checked' ) ) {
			$( '.stock_qty_field', '.inline-edit-row' ).show().removeAttr( 'style' );
		} else {
			$( '.stock_qty_field', '.inline-edit-row' ).hide();
		}

	});

	$( '#wpbody' ).on( 'click', '#doaction, #doaction2', function() {
		$( 'input.text', '.inline-edit-row' ).val( '' );
		$( '#woocommerce-fields' ).find( 'select' ).prop( 'selectedIndex', 0 );
		$( '#woocommerce-fields-bulk' ).find( '.inline-edit-group .change-input' ).hide();
	});

	$( '#wpbody' ).on( 'change', '#woocommerce-fields-bulk .inline-edit-group .change_to', function() {

		if ( 0 < $( this ).val() ) {
			$( this ).closest( 'div' ).find( '.change-input' ).show();
		} else {
			$( this ).closest( 'div' ).find( '.change-input' ).hide();
		}

	});
});