O
O
Oviplokos2020-05-18 21:27:45
WordPress
Oviplokos, 2020-05-18 21:27:45

How to limit the number of attributes displayed in the product catalog?

Can you help me figure out how to limit the number of attributes displayed in the product catalog?
Through the woocommerce show attributes plugin, I display attributes in the product catalog, but the problem is that everything is displayed in general, but I would like 2-3 attributes to be displayed in the product catalog. But in the product card, let all the attributes that you set for the product be displayed.
How to display a specific attribute of a product in a catalog in woocommerce?
In this question, someone had a solution to display only certain attributes in the product card, but the link to the solution in that question is no longer clickable.
How to deal with this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2020-05-18
@wppanda5

I don't know how your woocommerce show attribute works.
But this is how the standard woo function works, slightly tweaked so that you can set how many attributes you need. And immediately added output.
of course, it’s not worth doing this from the point of view of the beauty of the code, but as far as I understand, you don’t give a damn about it, plus everything is visible at once in one place, if necessary, after seeing how it works, you can do it as you like

function wpp_get_product_attributes( $product = null, $count = null ) {

    if ( empty( $product ) ) :
      global $product;
    endif;

    $attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' );

    $i = 1;
    foreach ( $attributes as $attribute ) {

      if ( ! empty( (int) $count ) && (int) $count > $i ) {
        break;
      }

      $values = [];

      if ( $attribute->is_taxonomy() ) {
        $attribute_taxonomy = $attribute->get_taxonomy_object();
        $attribute_values   = wc_get_product_terms( $product->get_id(), $attribute->get_name(), [ 'fields' => 'all' ] );

        foreach ( $attribute_values as $attribute_value ) {
          $value_name = esc_html( $attribute_value->name );

          if ( $attribute_taxonomy->attribute_public ) {
            $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
          } else {
            $values[] = $value_name;
          }
        }
      } else {
        $values = $attribute->get_options();

        foreach ( $values as &$value ) {
          $value = make_clickable( esc_html( $value ) );
        }
      }

      $product_attributes[ 'attribute_' . sanitize_title_with_dashes( $attribute->get_name() ) ] = [
        'label' => wc_attribute_label( $attribute->get_name() ),
        'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
      ];

      $i ++;
    }

    $product_attributes = apply_filters( 'woocommerce_display_product_attributes', $product_attributes, $product );

    ?>
    <table class="woocommerce-product-attributes shop_attributes">
      <?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
        <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
          <th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
          <td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
        </tr>
      <?php endforeach; ?>
    </table>


    <?php
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question