/**
 * Get The Product Thumbnail ID
 *
 * If no post thumbnail is set, this will return the ID of the first image
 * associated with a product. If no image is found and the product is a variation it will
 * then try getting the parent product's image instead.
 *
 * @param  int  $product_id  Product ID
 * @return int               Product thumbnail ID
 */
function wpsc_the_product_thumbnail_id($product_id)
{
    $thumbnail_id = null;
    // Use product thumbnail...
    if (has_post_thumbnail($product_id)) {
        $thumbnail_id = get_post_thumbnail_id($product_id);
    } else {
        // ... or get first image in the product gallery
        $attached_images = wpsc_get_product_gallery($product_id);
        if (!empty($attached_images)) {
            $thumbnail_id = $attached_images[0]->ID;
        }
    }
    return apply_filters('wpsc_the_product_thumbnail_id', $thumbnail_id, $product_id);
}
Exemple #2
0
function _wpsc_get_product_gallery_json($id)
{
    $attachments = wpsc_get_product_gallery($id);
    return array_map('wp_prepare_attachment_for_js', $attachments);
}
Exemple #3
0
/**
* Function to display additional images in the image gallery
*/
function gold_shpcrt_display_gallery($product_id, $invisible = false)
{
    global $wpdb;
    $output = '';
    $siteurl = get_option('siteurl');
    /* No GD? No gallery.	 No gallery option? No gallery.	 Range variable set?  Apparently, no gallery. */
    if (get_option('show_gallery') == 1 && !isset($_GET['range']) && function_exists("getimagesize")) {
        if (version_compare(WPSC_VERSION, '3.8', '<')) {
            /* get data about the base product image */
            $product = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='" . $product_id . "' LIMIT 1", ARRAY_A);
            $image_link = WPSC_IMAGE_URL . $product['image'] . "";
            $image_file_name = $product['image'];
            $imagepath = WPSC_THUMBNAIL_DIR . $image_file_name;
            $base_image_size = @getimagesize($imagepath);
            /* get data about the extra product images */
            $images = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` WHERE `product_id` = '{$product_id}' AND `id` NOT IN('{$image_file_name}')\tORDER BY `image_order` ASC", ARRAY_A);
            $output = "";
            $new_height = get_option('wpsc_gallery_image_height');
            $new_width = get_option('wpsc_gallery_image_width');
            if (count($images) > 0) {
                /* display gallery */
                if ($invisible == true) {
                    foreach ($images as $image) {
                        $extra_imagepath = WPSC_IMAGE_DIR . $image['image'] . "";
                        $extra_image_size = @getimagesize($extra_imagepath);
                        $thickbox_link = WPSC_IMAGE_URL . $image['image'] . "";
                        $image_link = "index.php?image_id=" . $image['id'] . "&amp;width=" . $new_width . "&amp;height=" . $new_height . "";
                        $output .= "<a href='" . $thickbox_link . "' class='thickbox hidden_gallery_link'  rel='" . str_replace(array(" ", '"', "'", '&quot;', '&#039;'), array("_", "", "", "", ''), $product['name']) . "' rev='{$image_link}'>&nbsp;</a>";
                    }
                } else {
                    $output .= "<h2 class='prodtitles'>" . __('Gallery', 'wpsc_gold_cart') . "</h2>";
                    $output .= "<div class='wpcart_gallery'>";
                    if ($images != null) {
                        foreach ($images as $image) {
                            $extra_imagepath = WPSC_IMAGE_DIR . $image['image'] . "";
                            $extra_image_size = @getimagesize($extra_imagepath);
                            $thickbox_link = WPSC_IMAGE_URL . $image['image'] . "";
                            $image_link = "index.php?image_id=" . $image['id'] . "&amp;width=" . $new_width . "&amp;height=" . $new_height . "";
                            $output .= "<a href='" . $thickbox_link . "' class='thickbox'  rel='" . str_replace(array(" ", '"', "'", '&quot;', '&#039;'), array("_", "", "", "", ''), $product['name']) . "'><img src='{$image_link}' alt='{$product_name}' title='{$product_name}' /></a>";
                        }
                    }
                    $output .= "</div>";
                }
            }
        } else {
            $output = '';
            $product_name = get_the_title($product_id);
            $output .= "<div class='wpcart_gallery'>";
            if (function_exists('wpsc_get_product_gallery')) {
                $attachments = wpsc_get_product_gallery($product_id);
            } else {
                $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1, 'post_parent' => $product_id);
                $attachments = get_posts($args);
            }
            $featured_img = get_post_thumbnail_id($product_id);
            $thumbnails = array();
            if (count($attachments) > 1) {
                foreach ($attachments as $post) {
                    setup_postdata($post);
                    $link = wp_get_attachment_link($post->ID, 'gold-thumbnails');
                    $size = is_single() ? 'medium-single-product' : 'product-thumbnails';
                    $preview_link = wp_get_attachment_image_src($post->ID, $size);
                    $link = str_replace('a href', 'a rev="' . $preview_link[0] . '" class="thickbox" rel="' . $product_name . '" href', $link);
                    // always display the featured thumbnail first
                    if ($post->ID == $featured_img) {
                        array_unshift($thumbnails, $link);
                    } else {
                        $thumbnails[] = $link;
                    }
                }
            }
            $output .= implode("\n", $thumbnails);
            $output .= "</div>";
            wp_reset_postdata();
        }
        //closes if > 3.8 condition
    }
    //closes if gallery setting condition
    return $output;
}