Skip to content

Commit

Permalink
Reduce (and make filterable) the amount of terms shown in attributes …
Browse files Browse the repository at this point in the history
…page

The attributes page displays all the terms for all the existing
product attributes, which causes a performance problem in sites
with a lot of attributes and terms.

This commit limits the number of attributes shown to 100 by default,
but it also introduces a woocommerce_max_terms_displayed_in_attributes_page
filter to customize it.
  • Loading branch information
Konamiman committed Jul 18, 2022
1 parent fa5c243 commit 00b99de
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions plugins/woocommerce/includes/admin/class-wc-admin-attributes.php
Expand Up @@ -317,7 +317,16 @@ public static function add_attribute() {
<tbody>
<?php
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( $attribute_taxonomies ) :
if ( $attribute_taxonomies ) {
/**
* Filters the maximum number of terms that will be displayed for each taxonomy in the Attributes page.
*
* @param int @default_max_terms_to_display Default value.
* @returns int Actual value to use, may be zero.
*
* @since 6.9.0
*/
$max_terms_to_display = apply_filters( 'woocommerce_max_terms_displayed_in_attributes_page', 100 );
foreach ( $attribute_taxonomies as $tax ) :
?>
<tr>
Expand Down Expand Up @@ -353,30 +362,54 @@ public static function add_attribute() {
$taxonomy = wc_attribute_taxonomy_name( $tax->attribute_name );

if ( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( $taxonomy, 'hide_empty=0' );
$terms_string = implode( ', ', wp_list_pluck( $terms, 'name' ) );
if ( $terms_string ) {
$total_count = (int) get_terms(
array(
'taxonomy' => $taxonomy,
'fields' => 'count',
'hide_empty' => false,
)
);
if ( 0 === $total_count ) {
echo '<span class="na">&ndash;</span>';
} elseif ( $max_terms_to_display > 0 ) {
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
'number' => $max_terms_to_display,
'fields' => 'names',
'hide_empty' => false,
)
);
$terms_string = implode( ', ', $terms );
if ( $total_count > $max_terms_to_display ) {
$remaining = $total_count - $max_terms_to_display;
/* translators: 1: Comma-separated terms list, 2: how many terms are hidden */
$terms_string = sprintf( __( '%1$s... (%2$s more)', 'woocommerce' ), $terms_string, $remaining );
}
echo esc_html( $terms_string );
} elseif ( 1 === $total_count ) {
echo esc_html( __( '1 term', 'woocommerce' ) );
} else {
echo '<span class="na">&ndash;</span>';
/* translators: %s: Total count of terms available for the attribute */
echo esc_html( sprintf( __( '%s terms', 'woocommerce' ), $total_count ) );
}
} else {
echo '<span class="na">&ndash;</span>';
echo '<span class="na">&ndash;</span><br />';
}
?>
<br /><a href="edit-tags.php?taxonomy=<?php echo esc_attr( wc_attribute_taxonomy_name( $tax->attribute_name ) ); ?>&amp;post_type=product" class="configure-terms"><?php esc_html_e( 'Configure terms', 'woocommerce' ); ?></a>
</td>
</tr>
<?php
endforeach;
else :
?>
} else {
?>
<tr>
<td colspan="6"><?php esc_html_e( 'No attributes currently exist.', 'woocommerce' ); ?></td>
</tr>
<?php
endif;
?>
}
?>
</tbody>
</table>
</div>
Expand Down

0 comments on commit 00b99de

Please sign in to comment.