Limit results for wc_display_product_attributes

198 Views Asked by At

I got a snippet that is showing attributes of my product on the archive page as shown in the picture. At some results there are a lot of results and i want to limit that to let's say 5 and after 5 maybe a show more button. The piece of code i use now is down below. I would like to have it separated as well that it only targets one attribute and not all.

Not an expert in PHP so i'm struggling to find a resolution.

    add_action( 'woocommerce_shop_loop_item_title', 'add_motorcode' );

function add_motorcode() {
    global $product;
    echo wc_display_product_attributes ( $product );
    
}
   

I've tried to select the brand by itself it worked but the clickable attribute was gone.

     
    add_action( 'woocommerce_shop_loop_item_title', 'fdev_display_attribute_brand', 10 );
function fdev_display_attribute_brand() {

    global $product;
   
        $taxonomy = 'pa_merk'; 
        
        if ( ! $product->get_attribute($taxonomy) ) { 
            return;
        }

        echo '<div class="wc-attribute-brand" style="margin-top: 20px;"><span style="font-size:16px; font-weight:600;">' . __( 'brand : ', 'woocommerce' ) ;
        echo '<p class="attribute-brand">' . $product->get_attribute($taxonomy) . '</p> </span> </div>' ;  
}

Anyone that could push me in the right direction?

1

There are 1 best solutions below

0
Khaled Developer On

i not found solution for limit this thing but i found source of this function HERE

SO i rebuild this function

Step1: first of all i Changed the name function!!!

Step2: add $limitation = -1 param

Step3: add this part of code ->

// <------- I add This 
if($limitation != -1)
    $product_attributes  = array_splice($product_attributes, count($product_attributes) - $limitation);

BEFORE this line :

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

i write it here so everyone use :

<?php 
function kdev_wc_display_product_attributes( $product , $limitation = -1) {
$product_attributes = array();

// Display weight and dimensions before attribute list.
$display_dimensions = apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() );

if ( $display_dimensions && $product->has_weight() ) {
    $product_attributes['weight'] = array(
        'label' => __( 'Weight', 'woocommerce' ),
        'value' => wc_format_weight( $product->get_weight() ),
    );
}

if ( $display_dimensions && $product->has_dimensions() ) {
    $product_attributes['dimensions'] = array(
        'label' => __( 'Dimensions', 'woocommerce' ),
        'value' => wc_format_dimensions( $product->get_dimensions( false ) ),
    );
}

// Add product attributes to list.
$attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' );

foreach ( $attributes as $attribute ) {
    $values = array();

    if ( $attribute->is_taxonomy() ) {
        $attribute_taxonomy = $attribute->get_taxonomy_object();
        $attribute_values   = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( '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() ) ] = array(
        'label' => wc_attribute_label( $attribute->get_name() ),
        'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
    );
}

// <------- I add This 
if($limitation != -1)
    $product_attributes  = array_splice($product_attributes, count($product_attributes) - $limitation);

/**
 * Hook: woocommerce_display_product_attributes.
 *
 * @since 3.6.0.
 * @param array $product_attributes Array of atributes to display; label, value.
 * @param WC_Product $product Showing attributes for this product.
 */
$product_attributes = apply_filters( 'woocommerce_display_product_attributes', $product_attributes, $product );

wc_get_template(
    'single-product/product-attributes.php',
    array(
        'product_attributes' => $product_attributes,
        // Legacy params.
        'product'            => $product,
        'attributes'         => $attributes,
        'display_dimensions' => $display_dimensions,
    )
);
}
?>

YOU can use as :

$limit = 5;
kdev_wc_display_product_attributes($product, $limit);

if anyone has best solution , answer the question pls :)