Hello Nick,
To apply the sorting to all documents you can simply remove the packing-slip
conditional from the snippet, changing:
if( $document_type == 'packing-slip' ) {
usort($items, 'wpo_wcpdf_sort_by_category');
}
to just:
usort($items, 'wpo_wcpdf_sort_by_category');
The entire resulting snippet being:
// Sort items alphabetically by category
add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_category', 10, 3 );
function wpo_wcpdf_sort_items_by_category ( $items, $order, $document_type ) {
usort($items, 'wpo_wcpdf_sort_by_category');
return $items;
}
function wpo_wcpdf_sort_by_category($a, $b) {
$categories_a = strip_tags( wc_get_product_category_list( $a['product_id'] ) );
$categories_b = strip_tags( wc_get_product_category_list( $b['product_id'] ) );
if ( $categories_a == $categories_b ) return 0;
return ( $categories_a < $categories_b ) ? -1 : 1;
}