Checking if you’re on a product page

There are a few ways to check if your on a product page, you would most likely want to do this within your theme. for example on your page.php file when you call your side bars you might want to call a different side bar for product related pages.

There are a few ways you can do this see below for a code snippet and description of each.

get_post_type()

get_post_type will check the Post type for the current page – WP-e-Commerce has the post type of wpsc-product so you could do a check in your theme like so:

[php]

if ( get_post_type == ‘wpsc-product’ ){

//do some product page only related stuff in here

}

[/php]

Of course for single products pages you could do the following:

[php]

if ( get_post_type == ‘wpsc-product’  && is_single){

//do some product page only related stuff in here

}

[/php]

Note: This function will not return true if your looking at the main product page, get_post_type must also be used within the loop

is_products_page();

This will check if your currently on the main products page you could use this combined with the get_post_type() function this will then cover every type of product page in WP-e-Commerce

[php]

if ( get_post_type == ‘wpsc-product’  &&  is_products_page()){

//do some product page only related stuff in here

}[/php]

Ultimate Check for all products Pages

The function below is an in loop check to see if the cuurent page is a products page, it returns true or false.

[php]

function is_a_page_containing_products(){

global $post;

$is_a_page_containing_products = false;

if(get_post_type($post) == ‘wpsc-product’){

$is_a_page_containing_products = true;

}

if ( function_exists( ‘is_products_page’ ) && !$is_a_page_containing_products){

if(is_products_page()){

$is_a_page_containing_products = true;

}

}

if(!$is_a_page_containing_products){

global $wpdb;

if(!empty($post->ID)){

$sql = "SELECT * FROM `{$wpdb->posts}` WHERE `post_type` IN(‘page’,’post’) AND `post_content` LIKE ‘%".wpsc_products."%’

AND `ID` = ".$post->ID;

$result = $wpdb->get_results($sql);

if($result){

$is_a_page_containing_products = true;

//error_log(‘has found shortcode wpsc_products’ );

}

}

}

return $is_a_page_containing_products;

}

[/php]