ajay
ajay

Reputation: 355

how to get category name with respect to product id in WP eCommerce plugin

I am using WP eCommerce for displaying products in my website.I want to check the category name of each product displaying in the page.

    while (wpsc_have_products()) :  wpsc_the_product(); 
          $product_id=wpsc_the_product_id();
         //here I want to get the category name with respect to $product_id in which this product exists.

           my code continues...

    endwhile;

Is it possible?

Please help me

Upvotes: 0

Views: 5351

Answers (2)

Rohan
Rohan

Reputation: 19

I looked into the bredcrumbs class and that helped me find a solution to a very similar problem that I had. Anyway, paste this code in and it will print out the category name for you.

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();

Hope this helps. I will be exploring this a bit more and will put up my results on my blog, http://www.consofas.com/

Rohan.

Upvotes: 1

Denis de Bernardy
Denis de Bernardy

Reputation: 78581

Best I recollect, they're stored as posts, so you can use the normal category api. get_the_category(), off the top of my head, should yield the list of terms.

Upvotes: 0

Related Questions