Home Forums Plugins Wholesale Pricing Support Question About Edits to Product Templates Reply To: Question About Edits to Product Templates

#8506
seriouslysean
Participant

No matter what I was trying, I couldn’t get the guest visibility to work. I had to copy the wpsc_wp_is_product_visible() function, rename it, and make changes to it in my own functions.php file to get it to work.

I’m not sure if it will help anyone on here, but hopefully it will. The changes I made will request the post data for all user roles (including guest, which wasn’t happening before). In order to get this to work, do the following steps in order.

Just be advised, for any update after the 3.8 version of WP-eCommerce, this fix MAY break if they decide to make changes to the way their built in function works. When that happens, you will have to go back and change these back to the way they suggested.

1.) Copy the following to your ‘functions.php’ file in your theme directory and then save the file

function sp_wpsc_wp_is_product_visible()
{
// Check if the user is logged in
if(is_user_logged_in())
{
global $current_user;
get_currentuserinfo();
$role = $current_user->roles[0];
}
else
{
$role = ‘guest’;
}

// Get the product information
switch(wpsc_get_major_version())
{
case ‘3.7’:
$checkbox = get_product_meta( wpsc_the_product_id(), ‘wpsc_wp_checkbox’, true );
break;

case ‘3.8’:
global $post;
$post_id = $post->ID;
if(!$post_id)
$post_id = $product_id;
$product_data = get_post_custom($post_id);
$product_data[‘meta’] = maybe_unserialize($product_data);
if($product_data[‘meta’])
{
foreach($product_data[‘meta’] as $meta_key => $meta_value)
$product_data[‘meta’][$meta_key] = $meta_value[0];
$product_meta = maybe_unserialize($product_data[‘_wpsc_product_metadata’][0]);

$checkbox = $product_meta[‘wpsc_wp_checkbox’];
}
break;
}

// See if the visibility is actuall enabled
if($checkbox[$role] == ‘on’)
{
// User per-Product visibility for that User Role, including Site Visitors
switch(wpsc_get_major_version())
{
case ‘3.7’:
$visibility = get_product_meta( wpsc_the_product_id(), ‘wpsc_wp_visibility’, true );
break;

default:
case ‘3.8’:
$visibility = $product_meta[‘wpsc_wp_visibility’][$role];
break;
}

switch($visibility)
{
case ‘0’:
return false;
break;

default:
case ‘1’:
return true;
break;
}
}
else
{
return true;
}
}

2.) In your ‘wpsc-products_page.php’, search for

<?php while (wpsc_have_products()) : wpsc_the_product(); ?>

and change it to the following:

<?php while (wpsc_have_products()) : wpsc_the_product(); ?>
<?php if(sp_wpsc_wp_is_product_visible()) : ?>

Then look for the closing tag of the loop

<?php endwhile; ?>
<?php /** end the product loop here */?>

And change it to the following

<?php endif; ?>
<?php endwhile; ?>
<?php /** end the product loop here */?>

3.) In your ‘wpsc-single_product.php’, search for

<?php
/**
* Start the product loop here.
* This is single products view, so there should be only one
*/

while ( wpsc_have_products() ) : wpsc_the_product(); ?>

and change it to the following:

<?php
/**
* Start the product loop here.
* This is single products view, so there should be only one
*/

while ( wpsc_have_products() ) : wpsc_the_product(); ?>
<?php if(sp_wpsc_wp_is_product_visible()) : ?>

Then look for the closing tag of the loop

<?php endwhile;

do_action( 'wpsc_theme_footer' ); ?>

And change it to the following

<?php endif; endwhile;

do_action( 'wpsc_theme_footer' ); ?>