Dismayed this morning to find that WP e-Commerce does not support multiple images on a product page unless you pay for the “gold cart” option. Since I’m basically stingy, I did it myself. Here’s how:
I replaced this code in wpsc-single_product.php
:
<?php if ( wpsc_the_product_thumbnail() ) : ?>
<a rel="<?php echo wpsc_the_product_title(); ?>" class="<?php echo wpsc_the_product_image_link_classes(); ?>" href="<?php echo wpsc_the_product_image(); ?>">
<img class="product_image" id="product_image_<?php echo wpsc_the_product_id(); ?>" alt="<?php echo wpsc_the_product_title(); ?>" title="<?php echo wpsc_the_product_title(); ?>" src="<?php echo wpsc_the_product_thumbnail(get_option('product_image_width'),get_option('product_image_height'),'','single'); ?>"/>
</a>
<?php if ( function_exists( 'gold_shpcrt_display_gallery' ) )
echo gold_shpcrt_display_gallery( wpsc_the_product_id() );
?>
<?php else: ?>
with this code:
<?php if ( wpsc_the_product_thumbnail()) : if (has_post_thumbnail()): <a rel="lightbox[<?php echo wpsc_the_product_title(); ?>]" class="<?php echo wpsc_the_product_image_link_classes(); ?>" href="<?php echo wpsc_the_product_image(); ?>">
<?php echo get_the_post_thumbnail(wpsc_the_product_id(),'thumbnail',array('alt' => wpsc_the_product_title(),'title' => wpsc_the_product_title() ));?>
</a>
<?php endif;
sb_get_images_for_product(wpsc_the_product_id());else: ?>
I’ve used the standard WP function get_the_post_thumbnail()
instead of the WP e-commerce function wpsc_the_product_thumbnail()
simply to ensure a consistent display of thumbnails. So, I’m displaying the product thumbnail and then using a custom function sb_get_images_for_product()
to retrieve any other images associated with the product. This function effectively replaces the gold_shpcrt_display_gallery()
function for which you have to pay. I saved sb_get_images_for_product()
in the functions.php (located in the theme folder). This function retrieves all images attached to the post and then displays any which are not the thumbnail – we’ve already displayed that. Here’s the code (last tested against WP 3.5):
function sb_get_images_for_product($id){
global $wpdb;
$post_thumbnail = get_post_thumbnail_id(); $attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = $id AND post_type = 'attachment' ORDER BY menu_order ASC",$id));
foreach ($attachments as $attachment){
if ($attachment->ID <> $post_thumbnail){ $image_attributes = wp_get_attachment_image_src($attachment->ID,'thumbnail');?>
<a rel="lightbox[<?php echo wpsc_the_product_title(); ?>]" href="<?php echo $attachment->guid; ?>" class="<?php echo wpsc_the_product_image_link_classes(); ?>">
<img src="<?php echo $image_attributes[0]; ?>" alt="<?php echo wpsc_the_product_title(); ?>"/>
</a>
<?php }
}
}
Note that WP 3.3 changed the way images work and messed up the WP e-commerce product images on version 3.8.7.4, with the result that it's possible to have a product without a product thumbnail. This is fixed with WP e-commerce version 3.8.7.6. However, I've added the if (has_post_thumbnail())
condition in the first code snippet above to trap this. Thanks to Brian for flagging this up.
Updated 21 April 2014, tested against WP 3.9, WPSC 3.8.13.4
Somewhere along the line, in conjunction with core WP galleries, WPEC has changed the way additional images are handled, and the fix above no longer works. Now try making this replacement in wpsc-single_product.php
. Change this:
<?php
if ( function_exists( 'gold_shpcrt_display_gallery' ) )
echo gold_shpcrt_display_gallery( wpsc_the_product_id() );
?>
with this:
<?php
sb_get_images_for_product(wpsc_the_product_id());?>
and add this to your functions.php:
function sb_get_images_for_product($id){
$post_thumbnail = get_post_thumbnail_id();
$atts = get_post_meta($id,'_wpsc_product_gallery');
$attachments = $atts[0];
foreach ($attachments as $attachment){
if ($attachment<> $post_thumbnail){ $image_attributes = wp_get_attachment_image_src($attachment,'post-thumbnail');
$full_image_attributes = wp_get_attachment_image_src($attachment,'full');
?>
<a rel="lightbox[<?php echo wpsc_the_product_title(); ?>]" href="<?php echo $full_image_attributes[0]; ?>" class="<?php echo wpsc_the_product_image_link_classes(); ?>">
<img src="<?php echo $image_attributes[0]; ?>" alt="<?php echo wpsc_the_product_title(); ?>"/>
</a>
<?php }
}
}