[Resolved] Attributes in Product Archiv pages

Home Forums Support [Resolved] Attributes in Product Archiv pages

Home Forums Support Attributes in Product Archiv pages

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #969762
    retroreiz

    Good Morning,

    I’m struggling with attributes that I would like to add to the product archive pages.

    On product single pages I have been able to add this successfully, unfortunately I succeed on the product archive pages.

    I use the following PHP snippet for this:

    add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
    function display_some_product_attributes(){
        // HERE define the desired product attributes to be displayed
        $defined_attributes = array('zustand', 'produktart');
    
        global $product;
        $attributes = $product->get_attributes();
    
        if ( ! $attributes ) {
            return;
        }
    
        $out = '<div 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 .= '<div class="' . esc_attr( $name ) . '">';
                $out .= '<b><p class="attribute-label">' . esc_html( $tax_label ) . ': </b> ';
                $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 .= '<class="attribute-value">' . implode(', ', $tax_terms) . '</p></div>';
    
            } else {
                $value_string = implode( ', ', $attribute->get_options() );
                $out .= '<p class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
                $out .= '<p class="attribute-label">' . $name . ': </p> ';;
            }
        }
    
        $out .= '</div>';
    
        echo $out;
    }

    For the product archive pages, the code should read like this, but it does not work (i have the snippet from the official Woocommerce page). Do you perhaps have a way to display attributes on the product archive pages as well?

    add_action( 'woocommerce_shop_loop_item_title', 'wc_show_attribute_links' );
    // if you'd like to show it on archive page, replace "woocommerce_product_meta_end" with "woocommerce_shop_loop_item_title"
    
    function wc_show_attribute_links() {
    	global $post;
    	$attribute_names = array( '<pa_zustand>', '<pa_produktart>' ); // Add attribute names here and remember to add the pa_ prefix to the attribute name
    		
    	foreach ( $attribute_names as $attribute_name ) {
    		$taxonomy = get_taxonomy( $attribute_name );
    		
    		if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
    			$terms = wp_get_post_terms( $post->ID, $attribute_name );
    			$terms_array = array();
    		
    	        if ( ! empty( $terms ) ) {
    		        foreach ( $terms as $term ) {
    			       $archive_link = get_term_link( $term->slug, $attribute_name );
    			       $full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>';
    			       array_push( $terms_array, $full_line );
    		        }
    		        echo $taxonomy->labels->name . ' ' . implode( $terms_array, ', ' );
    	        }
        	}
        }
    }

    In addition, the CSS code for the Product Archive pages would need to be adjusted so that the shopping cart button anchored to the bottom edge of the image to make it look even. How can I do this for deskopt view and mobile view?

    I hope the screenshot shows better what I mean: https://i.ibb.co/5B2WQFX/06.png

    Many thanks and greetings, Andreas

    #970261
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Is your code inside the hooks actually working? For example, what happens if you just do this inside the function?:

    echo 'hi';

    Does the word “hi” appear where you want your attributes to appear?

    #971785
    retroreiz

    Hi Tom,

    thanks to your reference, I then looked at the code again and I made a mistake.

    I have used the following:
    '<pa_zustand>', '<pa_produktart>' );

    And that’s right:
    'pa_zustand', 'pa_produktart' );

    Without the <> characters.

    Thank you for your help ๐Ÿ™‚

    Greetings, Andreas

    #972290
    Tom
    Lead Developer
    Lead Developer

    No problem ๐Ÿ™‚

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.