getting product category id on page.php template in wp-ecommerce plugin

I am using wp-ecommerce plugin to create a shopping cart in WordPress.

If I am on this URL, it displays all the products in services wpsc-product-category.

Read More
http://localhost/wordpress/products-page/services/

How can I get the wpsc-product-category id in the page template.

I have many categories as shown above and every category has different menu on page.php template.

I need to get category id or category slug to show different menu for different categories

After searching whole day on google I could find this:

<?php
if(is_category())
{
$cat = get_query_var(‘cat’);
$yourcat = get_category($cat);
echo $yourcat->slug;
}
else
{
echo ‘Country’; //default one
}
?>

But it does not work on page.php template.

I created my own logic and it worked for me. Pasting my code here, hoping it might help someone else:

  if(wpsc_display_products()):

 if(wpsc_is_in_category()) :

       if(wpsc_category_id() == 89 || 
      wpsc_category_id() == 66 || 
      wpsc_category_id() == 62 || 
      wpsc_category_id() == 61)
      { 

         $menu = "menu1";

      }else
      {
         $menu = "menu2";
      }
endif;
endif;

Related posts

Leave a Reply

2 comments

  1. Did you ever figure this out? I was able to do this with jQuery actually. Just make a separate widget for each category then in your js write something like this:

    if ($('body').filter("[class*='category']").length) {    
        $('body').addClass('category');  
        $('.category-nav').show();  
    } else if ($('body').filter("[class*='category']").length) {  
        $('body').addClass('category');  
        $('.category-nav').show();  
    }
    

    Just continue down with else if statements for each category. It could be a lot of code if you have a lot of categories but it did the trick for me. It just runs through all the classes on the body and if it contains the category name in any class it adds the category name as a class of it’s own.

  2. I too have been searching on how to do basically anything with WPeC functions outside of their template files, and I ran across this site here:

    Category wise products display in WP E-Commerce products page

    I believe there are a couple of coding errors in the example he provides but you can see where he can get the category ID here:

    $wpec_product_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0' );
    /* ---------- RUN A LOOP FOR EACH PRODUCT CATEGORIES ----------- */
    foreach ( $wpec_product_categories as $wpec_categories ):
        $wpec_term_id   = $wpec_categories->term_id; // Here is your category ID
       ...
    

    I’ve used his code and built on it a little for my website’s purpose (within the page.php template) which you can look at if you’d like to.

    https://gist.github.com/4553667