Set a default sort order for wishlist items

NM Gift Registry uses datatables, a jQuery plugin, to compose all the tables used by the plugin. This gives you the most flexibility for customizing the tables to your liking as all you need to do is to understand how to work with datatables and also understand how NM Gift Registry implements the jquery plugin.

NM Gift Registry provides a single filter which acts as an API for setting many datatables options, so to set some options all you need to do is to use the filter. Of course not all options can be set by the filter but the ones you would most likely need to set have been configured to be set by the filter.

In this article, we would see how the wishlist items table can be sorted by favourites by default. Below is how the table appears on the wishlist page by default. It has no sort order.

Let’s say now you want to sort the items by favourite status, the code below shows all you need to do.

add_filter( 'nmgr_datatables', 'my_nmgr_setup_datatables' );

function my_nmgr_setup_datatables( $tables ) {
	if ( !is_nmgr_wishlist() ) {
		return $tables;
	}

	foreach ( array_keys( $tables ) as $name ) {
		if ( 'items_table' === $name ) {
			$tables[ $name ][ 'options' ][ 'order' ] = [ [ 5, 'desc' ] ];
		}
	}
	return $tables;
}

In the code above, we are targetting the sort to work only on the wishlist page with the conditional is_nmgr_wishlist, as this is where you would most likely want to set default sorting.

Next, we are looping through the tables registered for the plugin with datatables to access the wishlist items table specifically. When we access it, we then access the options registered for it, and then set the sort order. The order is set as [ [ 5, 'desc' ] ] because it has to be an array of arrays from the datatables documentation. 5 is the index of the column we want to order, and this is equals to the favourite column if we start counting the columns from the left with the first column being 0, and desc is the direction of order.

This simple code hence easily allows you to configure the wishlist items table to be displayed with the items sorted according to favourite status by default. Our sorted table would now appear like this (notice the arrow next to the favourite icon in the table header):

To provide a more advanced example, let’s say you have customized the items table by adding some columns and you don’t know the index of the favourite column which you want to sort, or you simply dont’ want to manually type the index of the column, you can automatically get the index using the code below.

function my_nmgr_setup_datatables( $tables ) {
	if ( !is_nmgr_wishlist() ) {
		return $tables;
	}

	$view = new NMGR_Items_View( nmgr_get_current_wishlist_id() );
	$index = array_search( 'favourite', $view->get_parts() );

	if ( $index ) {
		foreach ( array_keys( $tables ) as $name ) {
			if ( 'items_table' === $name ) {
				$tables[ $name ][ 'options' ][ 'order' ] = [ [ --$index, 'desc' ] ];
			}
		}
	}
	return $tables;
}

The code above does the same thing as the first code only that instead of manually entering 5 as the default column to sort by, we are getting the index number programmatically. What we do is that we get the view object for the wishlist items. This object stores all the registered columns for the current view. From this we can get the index of our column if it exists in the registered columns. After getting the index, we are setting the order as [ [ --$index, 'desc' ] ]. By this we’re simply reducing the $index variable by one to account for the 0 index in the array.