Wordpress Woocommerce Show attributes on shop page

Viewed 2560

I want to add some attributes to the shop page of wordpress. This code i found on Stackoverflow, is shows all attribute labes but on all the same attribute names.

add_action('woocommerce_after_shop_loop_item_title','add_attribute');
function add_attribute() {
global $product;

$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );
$attr_output = array();


foreach( $product_attributes as $taxonomy ){
    if( taxonomy_exists($taxonomy) ){
        $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
        $value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');

        if( ! empty($value) ){

            $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
        }
    }}


echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';

}

current state

I just need a bit help to get it to show all the right attributes.

2 Answers

There a some little mistakes in your code. Try the following instead:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

Code goes in functions.php file of your active child theme (or active theme). tested and works.


For simple products only you will use the following instead:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Only for simple products
    if ( ! $product->is_type( 'simple' ) ) return;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

Code goes in functions.php file of your active child theme (or active theme). tested and works.

I solved the problem. its not pretty but it works.

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_country';
    echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_class';
    echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_faction';
    echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_gender';
    echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}
Related