Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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