I wish to filter the items printed, or change their order

The code sample below sorts order items by SKU. A competent PHP developer will easily be able to adapt this to fulfil other requirements (e.g. sort differently, or omit unwanted items).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter('woocommerce_printorders_printnode_print_order_items', function ($order_items) {
if (is_array($order_items)) {
uasort($order_items, 'my_print_order_sorting_function');
}
return $order_items;
});
// This function sorts based on SKU
function my_print_order_sorting_function($a, $b) {
// Confirm that both items are order product items
if (!is_a($a, 'WC_Order_Item_Product')) return is_a($b, 'WC_Order_Item_Product') ? 1 : 0;
if (!is_a($b, 'WC_Order_Item_Product')) return -1;
$product_a = $a->get_product();
$product_b = $b->get_product();
// Confirm that both products exist
if (!is_a($product_a, 'WC_Product')) return is_a($product_b, 'WC_Product') ? 1 : 0;
if (!is_a($product_b, 'WC_Product')) return -1;
// In this example, we sort via SKU
return strcmp($product_a->get_sku(), $product_b->get_sku());
}
add_filter('woocommerce_printorders_printnode_print_order_items', function ($order_items) { if (is_array($order_items)) { uasort($order_items, 'my_print_order_sorting_function'); } return $order_items; }); // This function sorts based on SKU function my_print_order_sorting_function($a, $b) { // Confirm that both items are order product items if (!is_a($a, 'WC_Order_Item_Product')) return is_a($b, 'WC_Order_Item_Product') ? 1 : 0; if (!is_a($b, 'WC_Order_Item_Product')) return -1; $product_a = $a->get_product(); $product_b = $b->get_product(); // Confirm that both products exist if (!is_a($product_a, 'WC_Product')) return is_a($product_b, 'WC_Product') ? 1 : 0; if (!is_a($product_b, 'WC_Product')) return -1; // In this example, we sort via SKU return strcmp($product_a->get_sku(), $product_b->get_sku()); }
add_filter('woocommerce_printorders_printnode_print_order_items', function ($order_items) {
  if (is_array($order_items)) {
    uasort($order_items, 'my_print_order_sorting_function');
  }
return $order_items;
});

// This function sorts based on SKU
function my_print_order_sorting_function($a, $b) {

  // Confirm that both items are order product items
  if (!is_a($a, 'WC_Order_Item_Product')) return is_a($b, 'WC_Order_Item_Product') ? 1 : 0;
  if (!is_a($b, 'WC_Order_Item_Product')) return -1;

  $product_a = $a->get_product();
  $product_b = $b->get_product();

  // Confirm that both products exist
  if (!is_a($product_a, 'WC_Product')) return is_a($product_b, 'WC_Product') ? 1 : 0;
  if (!is_a($product_b, 'WC_Product')) return -1;

  // In this example, we sort via SKU
  return strcmp($product_a->get_sku(), $product_b->get_sku());
}