Advertisement
lorro

WooCommerce - Sort attributes by name

Sep 25th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. <?php
  2.   // Sorts attributes alphabetically on the Attributes page and in the select dropdown on product pages
  3.   // not tested exhaustively!
  4.   // Code goes in functions.php of your child theme
  5.  
  6.   add_filter('woocommerce_attribute_taxonomies', 'custom_attribute_sort');
  7.   function custom_attribute_sort($array) {
  8.     usort($array, 'compareName');
  9.     return $array;
  10.   }
  11.  
  12.   function compareName($attribute_1, $attribute_2) {
  13.     if ($attribute_1->attribute_name == $attribute_2->attribute_name) {
  14.       return 0;
  15.     }
  16.     return ($attribute_1->attribute_name < $attribute_2->attribute_name) ? -1 : 1;
  17.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement