Display the menu order field in the quick edit window

This code snippet comes really handy if you use the menu order option in woocommerce to sort the products in categories. The quick edit option in the all products table is very usefull but with some limitations and one of them is that in order to change the menu order you have to enter the full edit product page.

With this code snippet you won’t have to, you will now be able to change the menu order for a product directly from the quick edit window.

/* !Display the Menu order field in the quick edit window */
add_action( 'woocommerce_product_quick_edit_start', 'show_menu_order_quick_edit' );
function show_menu_order_quick_edit() {
?>
	<label>
		<span class="title">Menu Order</span>
		<span class="input-text-wrap">
			<input type="number" name="_menu_order" class="text" value="">
		</span>
	</label>
	<br class="clear" />
<?php
}

/* !Get the current data for the Menu order of the product */
add_action( 'manage_product_posts_custom_column', 'show_menu_order_quick_edit_data', 9999, 2 );
function show_menu_order_quick_edit_data( $column, $post_id ) {
	if ( 'name' !== $column ) return;
	$menu_order = get_post_field( 'menu_order', $post_id );
	echo '<div>Menu Order: <span id="menu_order_' . $post_id . '">' . esc_html( $menu_order ) . '</span></div>';
	
	wc_enqueue_js( "
		$('#the-list').on('click', '.editinline', function() {
			var post_id = $(this).closest('tr').attr('id');
			post_id = post_id.replace('post-', '');
			var menu_order = $('#menu_order_' + post_id).text();
			$('input[name=\'_menu_order\']', '.inline-edit-row').val(menu_order);
		});
	" );
}

/* !Save the Menu order field after modified in the quick edit window */
add_action( 'woocommerce_product_quick_edit_save', 'save_menu_order_quick_edit' );
function save_menu_order_quick_edit( $product ) {
	$post_id = $product->get_id();
	if ( isset( $_REQUEST['_menu_order'] ) ) {
		$menu_order = intval( $_REQUEST['_menu_order'] );
		wp_update_post( [
			'ID' => $post_id,
			'menu_order' => $menu_order,
		] );
	}
}
Code language: PHP (php)

Leave a Comment