Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce Product Category Archive/Overview WordPress

  • SOLVED

I want a page that displays an overview of the product categories. It's like a category top-level page I guess. But WooComm is confusing me (which is not hard).

<strong>I'm not trying to display the products of the category</strong> - that is already handled by WooComm's taxonomy-product_cat.php file, and it works fine. Be it a custom page template, or any solution that works(!), the page simply needs to display ....

(for each product category)
- product_cat title
- product_cat description
- product_cat image
- link to product_cat page


So if there are 2 product categories (shorts and jumpers) this page will show ....

Shorts Category Title
- shorts category description
- shorts category image
- link to shorts category page

Jumper Category Title
- jumper category description
- jumper category image
- jumper category page

Answers (1)

2012-05-16

John Cotton answers:

This isn't directly supported by the WordPress hierarchy - you need to create your own template file and then use [[LINK href="http://codex.wordpress.org/Function_Reference/get_terms"]]get_terms[[/LINK]] with product_cat as the taxonomy argument to get a list of the terms.

In a loop of the array that is returned, you can use [[LINK href="http://codex.wordpress.org/Function_Reference/get_metadata"]]get_metadata[[/LINK]] to retrieve the extra info you want for each (you get description and slug from the get_terms call, however the image url is stored in a custom meta table) and output as desired.

You could create the template with a custom permalink added to the rewrite rules and then grab that in the template redirect function to route to your template file. However, you may find it easier to create a page template and an empty page with that template assigned to it.


TheLoneCuber comments:

I fine with the page template John. But writing that loop is beyond me. Can you help there?


TheLoneCuber comments:

I have the description and slugs sorted John. Can you direct me to the method of retrieving the product category thumbnail?


John Cotton comments:

Use something along these lines in your for loop of terms:


$meta = get_metadata( 'product_cat', $term_id );
echo wp_get_attachment_image( $meta['thumbnail_id'][0], $size );


...setting $size to whatever you need (the id coming back is the id of the image in the wp_posts table so you can use various standards methods here to control the actual output.


TheLoneCuber comments:

Not sure where I'm meant to use it? This is not working for me.

$terms = get_terms( 'product_cat', 'orderby=count&hide_empty=0' );
if ( $terms && !is_wp_error( $terms ) ) {
echo '';
foreach ( $terms as $term ) {
$meta = get_metadata( 'product_cat', $term_id );
echo '<h1>' . $term->name . '</h1>';
echo wp_get_attachment_image( $meta['thumbnail_id'][0], 250 );
echo '<p>' . $term->description . '</p>';
echo ' <span class="muted small">We\'re currently stocking ' . $term->count . ' wines from';
echo ' ' . $term->name . '</span> ';
echo '&nbsp;&nbsp;<code><a href="/wineries/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">See these wines <i class="icon-share-alt"></i></a>
<br/><hr>';
}
}
</code>

* I found some code code in the WooComm forums. Not sure if it helps. I noticed it uses wp_get_attachment_image<strong>_src</strong> instead of wp_get_attachment_image.

$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );

if ($thumbnail_id) :
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
else :
$image = woocommerce_placeholder_img_src();
endif;

echo '<img src="'.$image.'" alt="'.$category->name.'" width="'.$image_width.'" height="'.$image_height.'" />';


John Cotton comments:

You needed to change $term_id to match your code.

So the line should read:

$meta = get_metadata( 'product_cat', $term->term_id );

You could use the WooCommerce code. It does the same thing and has the advantage of wrapping up the taxonomy structure in a woocommerce function which means there's less likely to be a problem if they change the way they do things (which I doubt since WooCommerce is already very WP compliant). Just change $category to $term in the first line.