Support » Plugin: WP eCommerce » [Plugin: WP e-Commerce] How can I display the categories a product is in?

  • 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?

    http://wordpress.org/extend/plugins/wp-e-commerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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’);

    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…

    <?php
    $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>’;
    ?>

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: WP e-Commerce] How can I display the categories a product is in?’ is closed to new replies.