Sort & Display WooCommerce Product Attributes by Order

admin2025-01-08  4

I'm attempting to display some WooCommerce attributes on a page based on their menu order using get_the_terms and cannot get it to work. Originally, I used this code and it worked fine:

$product_values = get_the_terms( $product_id, 'my_value_here');
foreach ( $product_values as $product_value ) {
    // do stuff here like echo $product_value->slug
}

But when I tried applying a sorting solution (such as Sorting Attributes order when using get_the_terms), it does not work. Here's another one I tried that did not work: get_the_terms in descending alphabetical order

In the WordPress Admin, I have the ability to drag-and-drop these attributes to re-sort them. How can I then display them on the site in my sorted order?

Also, to give more detail, I'm displaying the Product Variations on the page using the Attributes created in the back-end of WP. So maybe it's the variations that I need to be able to sort.

I'm attempting to display some WooCommerce attributes on a page based on their menu order using get_the_terms and cannot get it to work. Originally, I used this code and it worked fine:

$product_values = get_the_terms( $product_id, 'my_value_here');
foreach ( $product_values as $product_value ) {
    // do stuff here like echo $product_value->slug
}

But when I tried applying a sorting solution (such as Sorting Attributes order when using get_the_terms), it does not work. Here's another one I tried that did not work: get_the_terms in descending alphabetical order

In the WordPress Admin, I have the ability to drag-and-drop these attributes to re-sort them. How can I then display them on the site in my sorted order?

Also, to give more detail, I'm displaying the Product Variations on the page using the Attributes created in the back-end of WP. So maybe it's the variations that I need to be able to sort.

Share Improve this question edited May 10, 2017 at 18:54 Recoil Design asked May 10, 2017 at 17:58 Recoil DesignRecoil Design 411 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Before we begin we should clarify what "Attribute values" actually are. They are either custom values, or Term IDs (they cannot be both). If your attributes are custom values those are manually entered so you can easily sort those yourself, but for Term ID's that's a little bit harder.

Here is a function that will sort Attribute values by their Term ordering in the admin panel. We'll use get_terms() as a sort of "Helper" because it already has the correct ordering.

//Sort an array of Attribute values (options) by their Term ordering in the admin panel 
function sort_options_by_term_order($options, $taxonomy_slug) {
    
    //Check to see if the values are actual terms (custom attribute values are not actual terms, so we can't sort those)
    foreach($options as $term_id) {

        $term = get_term_by('id', $term_id, $taxonomy_slug);
        if (!$term) {
            break;
        } else {

            //Sort by the ordering in the admin panel
            $sorted_options = array();

            //Get all of the terms, but only keep the ones in our $options array
            $terms = get_terms([
                'taxonomy' => $taxonomy_slug, 
                'hide_empty' => false
            ]);

            foreach ($terms as $term) {
                if (in_array($term->term_id, $options)) $sorted_options[] = $term->term_id;                                     
            }

            $options = $sorted_options;

        }

    }
    
    return $options;
    
}

Example usage:

//Get the product (WC_Product)
$product = wc_get_product($product_id);

//$attributes are actually taxonomies
$attributes = $product->get_attributes();
    
foreach($attributes as $taxonomy_slug => $attribute) {
   $sorted_options = sort_options_by_term_order($attribute->get_options(), $taxonomy_slug);

   //Do something with the $sorted_options

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266765a1146.html

最新回复(0)