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 (https://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 https://codex.www.ads-software.com/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.