Search code examples
wordpresswoocommerceattributesproduct

Display specific products Attibute value in custom page woocommerce


I am trying to show specific attribute value in custom page and how i can show it Alphabatically

<?php
$query = new WP_Query($args);
$products_by_attribute = array(); // To hold products grouped by attribute

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        global $product;

        // Get product attributes
        $attributes = $product->get_attributes();

        // Change 'color' to your desired attribute slug
        if (isset($attributes['color'])) {
            $attribute_value = $attributes['color']['value'];

            // Group products by their attribute value
            if (!isset($products_by_attribute[$attribute_value])) {
                $products_by_attribute[$attribute_value] = array();
            }
            $products_by_attribute[$attribute_value][] = $product;
        }
    }
    wp_reset_postdata(); // Reset post data

    // Sort the attribute values alphabetically
    ksort($products_by_attribute);
  
        echo '</div>'; // .product-items
    }
}
?>

Attributes Color = Aa,Bb,yellow2,red,green,green1,green2,red1,yellow


Solution

  • Could you please try to replace your code with the given code. This code will ensure that the attribute values are sorted alphabetically and the products will display accordingly.

    <?php
    $query                 = new WP_Query( $args );
    $products_by_attribute = array(); // This is to hold products grouped by attribute.
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            global $product;
    
            // Here we are getting product attributes
            $attributes = $product->get_attributes();
    
            // Here we have changed 'color' to our desired attribute slug.
            if ( isset($attributes['color']) ) {
                $attribute_value = $attributes['color']['value'];
    
                // Here we have grouped products by their attribute value.
                if ( ! isset( $products_by_attribute[$attribute_value] ) ) {
                    $products_by_attribute[$attribute_value] = array();
                }
                $products_by_attribute[$attribute_value][] = $product;
            }
        }
        wp_reset_postdata(); // Reset post data
    
        // Here we are sorting the attribute values alphabetically.
        ksort( $products_by_attribute );
    
        // We can display the products grouped by attribute value.
        foreach ( $products_by_attribute as $attribute_value => $products ) {
            echo '<h2>' . esc_html( $attribute_value ) . '</h2>';
            echo '<div class="product-items">';
            foreach ( $products as $product ) {
                // We can customize this part to display the product as desired.
                echo '<div class="product-item">';
                echo '<a href="' . get_permalink( $product->get_id() ) . '">' . $product->get_name() . '</a>';
                echo '</div>';
            }
            echo '</div>';
        }
    }
    ?>