Skip to content

Instantly share code, notes, and snippets.

@bjornjohansen
Created January 26, 2021 14:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bjornjohansen/96047c5ac6e414a119e6b103d6a7b1ee to your computer and use it in GitHub Desktop.
Reorder the visible attributes on the frontend.
<?php
/**
* Reorder the visible attributes on the frontend.
*/
add_filter( 'woocommerce_product_get_attributes', function( $old_attributes, $obj ) {
$order = [ 'pa_colour', 'pa_size' ];
$new_attributes = [];
// Loop through our order items.
foreach ( $order as $key ) {
if ( isset( $old_attributes[ $key ] ) ) {
// Add the attribute to our new attribute array.
$new_attributes[ $key ] = $old_attributes[ $key ];
// Remove the item from the old attribute array.
unset( $old_attributes[ $key ] );
}
}
// Merge any possible leftover attributes into the new attribute array.
$new_attributes = array_merge( $new_attributes, $old_attributes );
return $new_attributes;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment