noscript-img

Reihenfolge auf Rechnungen/Lieferscheinen nach SKU sortieren.

Mit dem folgenden Code-Schnipsel kannst du die Bestellungen Aufwärts nach der SKU sortieren lassen:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 );
function filter_order_get_items( $items, $order, $types = 'line_item' ) {
	if ( ! ( is_array( $types ) && isset( $types[ 0 ] ) && $types[ 0 ] == 'line_item' ) ) {
		return $items;
	} 
	$item_skus = $sorted_items = array();
	
	// Loop through order line items
	foreach ( $items as $items_id => $item ) { 
		// Check items type: for versions before Woocommerce 3.3
		if ( $item->is_type( 'line_item' ) ) {
		$product = $item->get_product(); // 
			if ( method_exists( $product, 'get_sku' ) ) {
				$item_skus[$product->get_sku()] = $items_id;
			} else { 		 
				$item_skus[$product->get_sku()] = ''; 
			} 
		} 
	} 
	// Check items type: for versions before Woocommerce 3.3 (2) 
	if ( count($item_skus) == 0 ) return $items; 
	// Sorting in ASC order based on SKUs; 
	uksort( $item_skus,
		   function( $a, $b ) { 
			   return strnatcmp( $a, $b );
		   } 
		  ); 
	// Loop through sorted $item_skus array 
	foreach ( $item_skus as $sku => $item_id ) { 
		// Set items in the correct order 
		$sorted_items[ $item_id ] = $items[ $item_id ]; 
	} return $sorted_items; 
}

…oder Abwärts sortieren!

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 );
function filter_order_get_items( $items, $order, $types = 'line_item' ) {
    if ( ! ( is_array( $types ) && isset( $types[ 0 ] ) && $types[ 0 ] == 'line_item' ) ) {
        return $items;
    } 
    $item_skus = $sorted_items = array();
    // Loop through order line items
    foreach ( $items as $items_id => $item ) {
        // Check items type: for versions before Woocommerce 3.3
        if ( $item->is_type( 'line_item' ) ) {
            $product = $item->get_product(); //
            if ( method_exists( $product, 'get_sku' ) ) {
                 $item_skus[$product->get_sku()] = $items_id;
                } else {
                         $item_skus[$product->get_sku()] = '';
            }
        }
    }
    // Check items type: for versions before Woocommerce 3.3 (2)
    if ( count($item_skus) == 0 ) return $items;
    // Sorting in ASC order based on SKUs;
    uksort( $item_skus, 
          function( $a, $b ) { 
            return strnatcmp( $b, $a ); 
          }
        );
    // Loop through sorted $item_skus array
    foreach ( $item_skus as $sku => $item_id ) {
        // Set items in the correct order
        $sorted_items[ $item_id ] = $items[ $item_id ];
    }
    return $sorted_items;
}