WP eCommerce
[Plugin: WP e-Commerce] How can I display the categories a product is in? (4 posts)

  1. Jean-Francois Arseneault
    Posted 4 years ago #

    Ok, I've tried adding every function I could find in the wpsc_single_product template, to no avail: WP's the_category, WPSC various functions I could find in the core files, to no avail.

    WPEC website doesn't help: no in-depth documentation. Their forums are broken. Wat gives?

    Very simple: I want my single product page to display every category the product is in.

    Why is this so difficult?


  2. Lee Willis
    Plugin Author

    Posted 4 years ago #

    It shouldn't be. Products are posts of type wpsc-product, and categories are a WordPress taxonomy of type wpsc_product_category.

    So - to get the categories that a product is assigned to:

    $categories = wp_get_object_terms ($post->ID, 'wpsc_product_category');

  3. Jean-Francois Arseneault
    Posted 4 years ago #

    Actually, trying to display the data gives an error "Cannot use object of type stdClass as array", since this is a multi-dimensional array.

    I've had to resort to using this function (http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html), and then echo'ing each potential array value like this, as an iteration using a for/count loop was taking more than 30 seconds...

    $categories = wp_get_object_terms ($post->ID, 'wpsc_product_category');
    $prodcats = objectToArray( $categories );
    echo "<div class='prodcats-items'>" . $prodcats[0]['name'] . ' ';
    echo $prodcats[1]['name'] . ' ';
    echo $prodcats[2]['name'] . ' ';
    echo $prodcats[3]['name'] . ' ';
    echo $prodcats[4]['name'] . ' ';
    echo $prodcats[5]['name'] . ' ';
    echo $prodcats[6]['name'] . ' ';
    echo $prodcats[7]['name'] . '</div>';

  4. Lee Willis
    Plugin Author

    Posted 4 years ago #

    The following works fine for me without any messing around - or having to assume a fixed number of categories.

    $categories = wp_get_object_terms ( $post->ID, 'wpsc_product_category' );
    foreach ( $categories as $category ) {
        esc_html_e ( $category->name );
        echo " ";

    It's worth noting that wp_get_object_terms can return different formats depending on what you need. See http://codex.wordpress.org/Function_Reference/wp_get_object_terms

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic