Giovanni
Giovanni

Reputation: 45

Show attribute values on Woocommerce category archive pages

In a Wordpress + Woocommerce website I have a menu with some custom links that recall specific shop pages URLs using advanced queries taxonomies.

These are example links I use for these custom menu entries:

https://example.com/product-category/cat1/?pa_attrib1=value1&pa_attrib2=value3
https://example.com/product-category/cat2/?pa_attrib1=value2&pa_attrib3=value4`
https://example.com/product-category/cat2/?pa_attrib1=value5&pa_attrib4=value5,value7,value8,value9

In this way I can configure "direct" entries to the various shop departments.

Now I need to show, in the shop and archive page, the name and the values of the attributes set in the url in a similar way:

Category Name - Attribute 1 name: value1 - Atttribute 2 Name: value2, ...

I suppose I need to use the hook woocommerce_archive_description in functions.php but I don't know how recall and show the values I need

add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
function show_term_description() {
   echo term_description( $term_id, $taxonomy ); // print Category Name
   // ...
   // ...
}

thanks

Upvotes: 2

Views: 3036

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253901

This can be done with the following commented code inserted in your hooked function:

add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
function show_term_description() {
    // Only for product category archive pages
    if( ! is_product_category()) return;

    global $wp;

    // Get the requested Url category/subcategories
    $request = $wp->request;
    // Set them in an array
    $request = explode( '/', $request );

    // Remove 'product-category' from the array
    if( 'product-category' == $request[0] )
        unset($request[0]);

    // Starting html formatting
    $html = '<p><strong>';

    // The main category and sub-categories names
    foreach( $request as $category ){
        // Get the category name
        $category_term = get_term_by( 'slug', $category, 'product_cat' );
        // Set category and subcategories in an array
        $categories_name[] = $category_term->name;
    }
    // Formatting the category/subcategories in an html string
    $html .= implode( '</strong> - <strong>', $categories_name );

    // The product attributes names and related values
    if( ! empty($_GET) ){
        $html .= '</strong> - <strong>'; // Formatting html
        $count = 0; // Initializing the counter
        $attr_count = count($_GET); // get the length of the array
        // Loop through the attribute/values from the $_GET
        foreach($_GET as $taxonomy => $values ){
            $count++;
            $terms_names = array(); // Initializing

            // If the taxonomy doesn't exist,
            if( ! taxonomy_exists( $taxonomy ) ){
                continue; // We go to next attribute
            }

            // Set the attribute terms in an array
            $term_slugs = explode( ',', $values );
            // Loop through the attribute term
            foreach( $term_slugs as $term_slug ){
                // Get the WP_Term object
                $term = get_term_by( 'slug', $term_slug, $taxonomy );
                if( ! term_exists( $term->term_id, $taxonomy ) )
                continue; // We go to next term

                // Set The term name in an array
                $terms_names[] = $term->name;
            }
            // If there is no corresponding terms for the taxonomy
            if( sizeof($terms_names) == 0 ){
                continue; // We go to next attribute
            }

            // Add the attribute label name to the output
            if( $count > 1 ) $html .= ' - ';
            $html .= wc_attribute_label( $taxonomy );
            // Formatting the term names in a string and add them to the output
            $html .= ':</strong> ' . implode( ', ', $terms_names) . '<strong>';
        }
    }
    // Outputing The html
    echo $html.'</p>';
    echo $attr_count .' | '.$count;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works…

Upvotes: 2

Related Questions