WP-Ecommerce show category name on product page

I am using WP-Ecommerce and would like to show the product’s category on the single product page. I have searched around and found something (posted by Rohan on this site) that almost works:

function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();

$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
//if product is associated w more than one category
if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
elseif(count($categories) > 0)
$query_data['category'] = $categories[0]->slug;

return $query_data['category'];
}
echo cdl_get_cat();

While this code does what I need it to, it displays the category name as a slug (i.e. eye-shadow instead of Eye Shadow). I’m not very fluent with PHP so I’m not sure what needs to be changed in order to display the name instead of the slug.

Related posts

Leave a Reply

3 comments

  1. I’ve been struggling with this all day and I’m not great at php either… But I’ve got it to work by deleting this bit (which i didnt understand..):

    //if product is associated w more than one category
    if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
    $query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
    elseif(count($categories) > 0)
    

    then changing ‘slug’ to ‘name’ here:

    $query_data['category'] = $categories[0]->slug;
    

    so all you need is this:

    <?php function cdl_get_cat() {
    global $wp_query, $wpsc_query;
    $query_data = Array();
    $cdl_post_id = wpsc_the_product_id();
    
    $categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
    $query_data['category'] = $categories[0]->name;
    
    return $query_data['category'];
    
    }
    echo cdl_get_cat();
    ?>
    

    if you have more that one category for one product changing the [0] will give you a different one as I think $categories is an array with all associated categories in it. You should probably be able to loop through all of them if needed. I should look up how to do that!

  2. Thanks, found this useful for adding a class to a product of the category it’s in:

    Before the product loop:

        <?php function cdl_get_cat() {
        global $wp_query, $wpsc_query;
        $query_data = Array();
        $cdl_post_id = wpsc_the_product_id();
    
        $categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
    
        $count = count($categories);
    
            for ($i = 0; $i < ($count); $i++) {
                $query_data['category'] = $categories[$i]->name;
                echo $query_data['category'];
                if ($i < ($count - 1))
                echo ", ";
            }
    
        }  
        cdl_get_cat();
        ?>
    

    Then in the loop:

    <li class="product_grid_item product_view_<?php echo wpsc_the_product_id(); ?> <?php cdl_get_cat(); ?>">
    
  3. Wow, i think I’ve got it, or got somewhere better…
    This should get all the categories associated with the displayed product then loop out a list, separated by commas.

    <?php function cdl_get_cat() {
    global $wp_query, $wpsc_query;
    $query_data = Array();
    $cdl_post_id = wpsc_the_product_id();
    
    $categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
    
    $count = count($categories);
    
        for ($i = 0; $i < ($count); $i++) {
            $query_data['category'] = $categories[$i]->name;
            echo $query_data['category'];
            if ($i < ($count - 1))
            echo ", ";
        }
    
    }  
    cdl_get_cat();
    ?>