{"id":2041,"date":"2011-11-30T18:54:02","date_gmt":"2011-11-30T17:54:02","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?p=2041"},"modified":"2014-04-21T08:04:56","modified_gmt":"2014-04-21T07:04:56","slug":"multiple-images-on-wp-e-commerce-product-pages","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2011\/11\/multiple-images-on-wp-e-commerce-product-pages\/","title":{"rendered":"Multiple images on WP e-Commerce product pages"},"content":{"rendered":"<p>Dismayed this morning to find that WP e-Commerce does not support multiple images on a product page unless you pay for the &#8220;gold cart&#8221; option. Since I&#8217;m basically stingy, I did it myself. Here&#8217;s how:<\/p>\r\n\r\n<h3 class=\"detail_header active\">Original fix, WP3.5, WPEC 3.8.7.6<\/h3>\r\n<div class=\"detail\">\r\n<p>I replaced this code in <code>wpsc-single_product.php<\/code>:<\/p>\r\n\r\n<pre>   &lt;?php if ( wpsc_the_product_thumbnail() ) : ?&gt;\r\n      &lt;a rel=\"&lt;?php echo wpsc_the_product_title(); ?&gt;\" class=\"&lt;?php echo wpsc_the_product_image_link_classes(); ?&gt;\" href=\"&lt;?php echo wpsc_the_product_image(); ?&gt;\"&gt;\r\n         &lt;img class=\"product_image\" id=\"product_image_&lt;?php echo wpsc_the_product_id(); ?&gt;\" alt=\"&lt;?php echo wpsc_the_product_title(); ?&gt;\" title=\"&lt;?php echo wpsc_the_product_title(); ?&gt;\" src=\"&lt;?php echo wpsc_the_product_thumbnail(get_option('product_image_width'),get_option('product_image_height'),'','single'); ?&gt;\"\/&gt;\r\n      &lt;\/a&gt;\r\n      &lt;?php if ( function_exists( 'gold_shpcrt_display_gallery' ) )\r\n         echo gold_shpcrt_display_gallery( wpsc_the_product_id() );\r\n      ?&gt;\r\n   &lt;?php else: ?&gt;<\/pre>\r\n\r\n<p>with this code:<\/p>\r\n\r\n<pre>   &lt;?php if ( wpsc_the_product_thumbnail()) :<span class=\"code_comment\">\/\/if the product has any images...\r\n<\/span>      if (has_post_thumbnail()): <span class=\"code_comment\"><span class=\"code_comment\">\/\/...display the thumbnail if there is one...?<\/span>&gt;\r\n<\/span>         &lt;a rel=\"lightbox[&lt;?php echo wpsc_the_product_title(); ?&gt;]\" class=\"&lt;?php echo wpsc_the_product_image_link_classes(); ?&gt;\" href=\"&lt;?php echo wpsc_the_product_image(); ?&gt;\"&gt;\r\n         &lt;?php echo get_the_post_thumbnail(wpsc_the_product_id(),'thumbnail',array('alt' =&gt; wpsc_the_product_title(),'title' =&gt; wpsc_the_product_title() ));?&gt;\r\n         &lt;\/a&gt;\r\n      &lt;?php endif;\r\n      sb_get_images_for_product(wpsc_the_product_id());<span class=\"code_comment\">\/\/...and then display all the rest of the images\r\n<\/span>else: ?&gt;<\/pre>\r\n\r\n<p>I&#8217;ve used the standard WP function <code>get_the_post_thumbnail()<\/code> instead of the WP e-commerce function <code>wpsc_the_product_thumbnail()<\/code> simply to ensure a consistent display of thumbnails. So, I&#8217;m displaying the product thumbnail and then using a custom function <code>sb_get_images_for_product()<\/code> to retrieve any other images associated with the product. This function effectively replaces the <code>gold_shpcrt_display_gallery()<\/code> function for which you have to pay. I saved <code>sb_get_images_for_product()<\/code> 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 &#8211; we&#8217;ve already displayed that. Here&#8217;s the code (last tested against WP 3.5):<\/p>\r\n<pre>function sb_get_images_for_product($id){\r\n   global $wpdb;\r\n   $post_thumbnail = get_post_thumbnail_id();<span class=\"code_comment\">\/\/read the thumbnail id\r\n<\/span>   $attachments = $wpdb-&gt;get_results($wpdb-&gt;prepare(\"SELECT * FROM $wpdb-&gt;posts WHERE post_parent = $id AND post_type = 'attachment' ORDER BY menu_order ASC\",$id));\r\n   foreach ($attachments as $attachment){\r\n      if ($attachment-&gt;ID &lt;&gt; $post_thumbnail){<span class=\"code_comment\">\/\/if we haven't already got the attachment as the post thumbnail\r\n<\/span>         $image_attributes = wp_get_attachment_image_src($attachment-&gt;ID,'thumbnail');?&gt;\r\n\t&lt;a rel=\"lightbox[&lt;?php echo wpsc_the_product_title(); ?&gt;]\" href=\"&lt;?php echo $attachment-&gt;guid; ?&gt;\" class=\"&lt;?php echo wpsc_the_product_image_link_classes(); ?&gt;\"&gt;\r\n\t&lt;img src=\"&lt;?php echo $image_attributes[0]; ?&gt;\" alt=\"&lt;?php echo wpsc_the_product_title(); ?&gt;\"\/&gt;\r\n\t&lt;\/a&gt;\r\n   &lt;?php }\t\t\r\n   }\r\n}<\/pre>\r\n<p class=\"orange\">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 <code>if (has_post_thumbnail())<\/code> condition in the first code snippet above to trap this. Thanks to Brian for flagging this up.<\/p>\r\n<\/div>\r\n<h3>Updated 21 April 2014, tested against WP 3.9, WPSC 3.8.13.4<\/h3>\r\n<p>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 <code>wpsc-single_product.php<\/code>. Change this:<\/p>\r\n<pre>&lt;?php\r\nif ( function_exists( 'gold_shpcrt_display_gallery' ) )\r\n   echo gold_shpcrt_display_gallery( wpsc_the_product_id() );\r\n?&gt;<\/pre>\r\n<p>with this:<\/p>\r\n<pre>&lt;?php\r\n\tsb_get_images_for_product(wpsc_the_product_id());<span class=\"code_comment\">\/\/...and then display all the rest of the images\r\n<\/span>?&gt;<\/pre>\r\n<p>and add this to your functions.php:<\/p>\r\n<pre>function sb_get_images_for_product($id){\r\n   $post_thumbnail = get_post_thumbnail_id();<span class=\"code_comment\">\/\/read the thumbnail id\r\n<\/span>   \r\n   $atts = get_post_meta($id,'_wpsc_product_gallery');\r\n   \r\n   $attachments = $atts[0];\r\n   \r\n   foreach ($attachments as $attachment){\r\n      if ($attachment&lt;&gt; $post_thumbnail){<span class=\"code_comment\">\/\/if we haven't already got the attachment as the post thumbnail\r\n<\/span>         $image_attributes = wp_get_attachment_image_src($attachment,'post-thumbnail');\r\n\t\t $full_image_attributes = wp_get_attachment_image_src($attachment,'full');\r\n\t\t ?&gt;\r\n\t&lt;a rel=\"lightbox[&lt;?php echo wpsc_the_product_title(); ?&gt;]\" href=\"&lt;?php echo $full_image_attributes[0]; ?&gt;\" class=\"&lt;?php echo wpsc_the_product_image_link_classes(); ?&gt;\"&gt;\r\n\t&lt;img src=\"&lt;?php echo $image_attributes[0]; ?&gt;\" alt=\"&lt;?php echo wpsc_the_product_title(); ?&gt;\"\/&gt;\r\n\t&lt;\/a&gt;\r\n   &lt;?php }\t\t\r\n   }\r\n}<\/pre>\r\n\r\n\r\n<script type=\"text\/javascript\">\r\n$(document).ready(function(){\r\n   $('.detail').hide();\r\n   $('.detail_header').removeClass('active').append(' <\\span>[<\\span>show details<\\\/span>]<\\\/span>');\r\n   $('.detail_header span span').click(function(event){\r\n          if(!event.detail || event.detail==1){\r\n          $(this).parents('h3').next().slideToggle();\r\n          $(this).parents('h3').toggleClass('active');\r\n          $(this).text($(this).text() == 'show details' ? 'hide details' : 'show details');\r\n          return false;\r\n          }\r\n   });\r\n})\r\n<\/script>","protected":false},"excerpt":{"rendered":"<p>Dismayed this morning to find that WP e-Commerce does not support multiple images on a product page unless you pay for the &#8220;gold cart&#8221; option. Since I&#8217;m basically stingy, I did it myself. Here&#8217;s how: Original fix, WP3.5, WPEC 3.8.7.6 I replaced this code in wpsc-single_product.php: &lt;?php if ( wpsc_the_product_thumbnail() ) : ?&gt; &lt;a rel=&#8221;&lt;?php [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10,24],"class_list":["post-2041","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build","tag-wordpress","tag-wp-e-commerce"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":5,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2957,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2957"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}