Reputation: 21
I want to display the "Attributes" in a specific order in the drop-down in woocommerce product admin section., The Attributes have not been entered in the correct order. It's now too late to redo it, so I want to list them in the drop-down menu as they are supposed to appear. It's too time consuming for the person entering the data to scroll up and down this list. It would be faster if they were already in the correct order. I have seen how to force them to display in a specific order on the front end but not in the admin section - Thanks
Upvotes: 2
Views: 17403
Reputation: 1
Actually the ordering is based on the descriptor, them being ordered alphabetically. The name can be whatever, but the list will display alphabetically BY THE DESCRIPTOR. So a simple solution for a medium sized finished website would be adding a few numbers at the beginning of the descriptor. EG:
check this image with my website
Upvotes: -1
Reputation: 26319
Run this code once and then remove it. So load it into a plugin, activate the plugin, then deactivate. I thought about auto-deactivating or creating a run once kind of transient, but I'll let you handle it. Those can actually be a hassle if the code isn't right the first time.
Maybe you could adapt the 2nd function to also run on save_post
or replace attributes_cmp()
in case shop managers add the attributes in the wrong order in the future. There actually is a filter called update_post_metadata
that would allow you to consistently apply this to every time the post is updated and the meta is saved.
WARNING backup your database! This code is destructive and will permanently change your DB and has not been tested beyond seeing the the new sort function worked on a demo array. Use at your own risk.
function so_35733629_update_products(){
$args = array(
'posts_per_page' => -1,
'meta_value' => '',
'post_type' => 'product',
'post_status' => 'any',
);
$products_array = get_posts( $args );
foreach( $products_array as $product ){
$attributes = get_post_meta( $product->ID, '_product_attributes', true );
if( ! empty( $atttributes ) ){
$attributes = so_35733629_reorder_attributes( $attributes );
update_post_meta( $product->ID, '_product_attributes', $attributes );
}
}
}
add_action( 'admin_init', 'so_35733629_update_products' );
function so_35733629_reorder_attributes( $attributes ){
// here is the desired order
$order = array(
'pa_maker',
'pa_model-name',
'pa_man-lady',
'pa_model-case-ref',
'pa_case-serial'
);
$new_attributes = array();
// create new array based on order of $order array
foreach( $order as $key ){
if( isset( $attributes[$key] ) ){
// add to new attributes array
$new_attributes[$key] = $attributes[$key];
// remove from the attributes array
unset( $attributes[$key] );
}
}
// merge any leftover $attributes in at the end so we don't accidentally lose anything
$new_attributes = array_merge( $new_attributes, $attributes );
// set the new position keys
$i = 0;
foreach( $new_attributes as $key => $attribute ){
// update the position
$new_attributes[$key]['position'] = $i;
$i++;
}
return $new_attributes;
}
Upvotes: 4
Reputation: 3965
Simply go to
Products -> Attributes
, Select attribute you want to reorder. Then click 'Configure Terms' icon. Here you will see all the attribute terms. Just drag and drop in the order you want. Hint :
Upvotes: 3