One of the neat things about WP e-Commerce is the vast array of filters and actions available. I had a client ask me this week for a custom sorting routine for their variations on the front-end.
For anyone unaware – variations are essentially different attributes of a product. If the product were a T-shirt, for example, variations would be Size (with Small, Medium and Large) as options or Color (Black, Red, Green), etc.
The code below would allow you to sort each group (and options within that group) according to a custom sorting algorithm. A pretty handy little code snippet!
// This compares the names of the variation term object. function zao_compare_variations( $a, $b ) { return strcmp( $a->name, $b->name ); } // This pops off the first option (which is always --Please Select--) // Then it sorts the remaining actual options with our custom sort function // Finally, it pops the first option back on and returns the $variations array. function zao_resort_variations( $variations, $groups, $product_id ) { foreach ( $variations as $group => $variants ) { $first = array_shift( $variants ); usort( $variants, 'zao_compare_variations' ); array_unshift( $variants, $first ); $variations[ $group ] = $variants; } return $variations; } add_filter( 'wpsc_all_associated_variations', 'zao_resort_variations', 10, 3 );