skip to Main Content

I am trying to display certain attributes that have data on a custom tab in woocommerce. I have found several examples of code but none are working for me.

I was given the following code


add_filter( 'woocommerce_display_product_attributes', 'remove_product_information', 10, 2 );
function remove_product_information( $product_attributes, $product ) {
        // Remove an attribute from the array
    unset($product_attributes['color']);
    
    return $product_attributes;
}
echo wc_display_product_attributes( $product );

but its not filtering anything out it still displays ‘color’ or any other attribute name I put in there. Also I need t filter out several attributes, so do I just add additional unset lines? or is there a cleaner way to do that? Any insight on this?

2

Answers


  1. Chosen as BEST ANSWER

    So the code I posted does work, I was just using the wrong name for the attribute. I needed to add 'attribute_pa_' so for example 'attribute_pa_brand'


  2. Solution 1:

    If you want to customize single product page attributes, You need to customize/override the product attribute page with your custom attribute page, after that, you can modify the attribute accordingly.

    To customise the page like:

    Copy 
     plugins/woocommerce/templates/single-product/product-attributes.php 
    To 
     themes/your-theme/woocommerce/single-product/product-attributes.php 
    

    and modify that.

    Solution 2:

    You have to define first the desired product attributes slugs in an array, after that you will get in single product pages:

    add_action( 'display_product_attributes', 'display_product_attributes', 25 );
    function display_some_product_attributes(){
    // HERE define the desired product attributes to be displayed
    $defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');
    
    global $product;
    $attributes = $product->get_attributes();
    
    if ( ! $attributes ) {
        return;
    }
    
    $out = '<ul class="taste-attributes">';
    
    foreach ( $attributes as $attribute ) {
    
        // Get the product attribute slug from the taxonomy
        $attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );
    
        // skip all non desired product attributes
        if ( ! in_array($attribute_slug, $defined_attributes) ) {
            continue;
        }
    
        // skip variations
        if ( $attribute->get_variation() ) {
            continue;
        }
    
        $name = $attribute->get_name();
    
        if ( $attribute->is_taxonomy() ) {
    
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            // get the taxonomy
            $tax = $terms[0]->taxonomy;
            // get the tax object
            $tax_object = get_taxonomy($tax);
            // get tax label
            if ( isset ( $tax_object->labels->singular_name ) ) {
                $tax_label = $tax_object->labels->singular_name;
            } elseif ( isset( $tax_object->label ) ) {
                $tax_label = $tax_object->label;
                // Trim label prefix since WC 3.0
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                   $tax_label = substr( $tax_label, 8 );
                }                
            }
    
            $out .= '<li class="' . esc_attr( $name ) . '">';
            $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
            $tax_terms = array();
    
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                // Insert extra code here if you want to show terms as links.
                array_push( $tax_terms, $single_term );
            }
    
            $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
            '" max="10"><div class="progress-bar"><span style="width:'
            . implode(', ', $tax_terms) . '0%">'
            . implode(', ', $tax_terms) . '</span></div></progress></li>';
    
        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<p class="attribute-label">' . $name . ': </p> ';
            $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
        }
     }
    
     $out .= '</ul>';
    
     echo $out;
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search