Woocommerce: Featured image different than product image revision needed - wordpress

i've been trying to implement the modification discussed on the page: Woocommerce: Featured image different than product image
But it doesn't seem to work with the latest release of woocommerce v2.4.7
does anyone have any ideas on how to fix it?
Here is the current code from v2.4.7 i'm trying to replace. when the fix is applied it breaks the page.
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title,
'alt' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_caption, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
And here the original modified code from the page mentioned above:
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
isset ($placeholder_width)? : $placeholder_width=0;
isset ($placeholder_height)? : $placeholder_height=0;
if ( $attachment_ids ) {
$attachment_id = $attachment_ids[0];
if ( ! $placeholder_width )
$placeholder_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
if ( ! $placeholder_height )
$placeholder_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
$output = '<div class="imagewrapper">';
//$classes = array( 'imagewrapper' );
$classes = array();
$image_link = wp_get_attachment_url( $attachment_id );
if ( $image_link ) {
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_thumbnail_size', 'shop_thumbnail' ) );
$image_class = esc_attr( implode( ' ', $classes ) );
$image_title = esc_attr( get_the_title( $attachment_id ) );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
Any help would be much appreciated.
Thanks
Darrell

I've been struggling with the same here, but this seems to have done the trick for me:
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
isset ($placeholder_width)? : $placeholder_width=0;
isset ($placeholder_height)? : $placeholder_height=0;
if ( $attachment_ids ) {
$attachment_id = $attachment_ids[0];
if ( ! $placeholder_width )
$size = wc_get_image_size( 'shop_catalog' );
$placeholder_width = $size['width'];
if ( ! $placeholder_height )
$size = wc_get_image_size( 'shop_catalog' );
$placeholder_width = $size['height'];
$output = '<div class="imagewrapper">';
//$classes = array( 'imagewrapper' );
$classes = array();
$image_link = wp_get_attachment_url( $attachment_id );
if ( $image_link ) {
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_class = esc_attr( implode( ' ', $classes ) );
$image_title = esc_attr( get_the_title( $attachment_id ) );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_caption, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
Just recently figured it out, so it possibly needs some finishing touches, but might point you in the right direction.
However, I'm not able to remove the product image from the gallery. The code that's supposed to make that happen is this (added to the product-thumbnails.php):
unset($attachment_ids);
All this does for me is removing ALL of the gallery thumbnails. Maybe you have figured this one out?

Put this:
unset($attachment_ids); // Don't display first image in product gallery, only as product image
right before the foreach loop in product-thumbnails.php. It's working fine for me.

Hege got it right with the replacement code for page product-image.php
To remove just the first thumbnail, and not all of them, use
unset($attachment_ids[0]);
on product-thumbnails.php

Related

Woocommerce - How to add one link "add to cart" to the loop product thumbnail?

I've tried several things adding the code to functions.php.
I think that the time I've been closer was with this code:
printf('<a class="button" href=" ..... "?>');
echo woocommerce_get_product_thumbnail();
printf('</a>');
}
But I don't know what to href on there.
previously, I removed the other liks with:
/*remove links to loop, add link to title*/
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 20);
function woocommerce_template_loop_product_title() {
echo sprintf('<h2 class="woocommerce-loop-product__title"><a title="%2$s" href="%1$s">%2$s</a></h2>',
get_the_permalink(),
get_the_title()
);
}
Thank you very much
There may be some modifications that need to be made for it to fit your scenario.
Add the following code to your functions.php file.
This will only add the link to the thumbnail if you are on the products page.
function add_link_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
/**
* This will only add the link on a single product page.
*/
if ( is_product() ) :
global $product;
$product_id = $product->get_id();
$product_url = get_permalink( $product_id );
$main_image = true;
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $post_thumbnail_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $post_thumbnail_id, $full_size );
$alt_text = trim( wp_strip_all_tags( get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true ) ) );
$image = wp_get_attachment_image(
$post_thumbnail_id,
$image_size,
false,
apply_filters(
'woocommerce_gallery_image_html_attachment_image_params',
array(
'title' => _wp_specialchars( get_post_field( 'post_title', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-src' => esc_url( $full_src[0] ),
'data-large_image' => esc_url( $full_src[0] ),
'data-large_image_width' => esc_attr( $full_src[1] ),
'data-large_image_height' => esc_attr( $full_src[2] ),
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
),
$post_thumbnail_id,
$image_size,
$main_image
)
);
$html = '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image">' . $image . '</div>';
return $html;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'add_link_woocommerce_single_product_image_thumbnail_html', 10, 2 );

Woocommerce: How to get product image and put it in default storefront image?

I want to change the layout of the storefront theme of woocommerce. So far I'm getting the product image by:
echo $product->get_image('full');
It displays the image without the lightbox and zoom effect. How do I add the "regular" storefront style to it?
If you look to the woocommerce template file single-product/product-image.php, you will need some similar code to get the displayed WooCommerce image with the Zoom and the style.
This code should be:
global $post, $product;
if( ! is_object ( $product ) && $post->post_type = 'product' )
$product = wc_get_product($post->ID);
if( is_object ( $product ) ):
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$thumbnail_size = apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' );
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$full_size_image = wp_get_attachment_image_src( $post_thumbnail_id, $thumbnail_size );
$placeholder = has_post_thumbnail() ? 'with-images' : 'without-images';
$wrapper_classes = apply_filters( 'woocommerce_single_product_image_gallery_classes', array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . $placeholder,
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
) );
?>
<div id="product-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 0; transition: opacity .25s ease-in-out;">
<figure class="woocommerce-product-gallery__wrapper">
<?php
$attributes = array(
'title' => get_post_field( 'post_title', $post_thumbnail_id ),
'data-caption' => get_post_field( 'post_excerpt', $post_thumbnail_id ),
'data-src' => $full_size_image[0],
'data-large_image' => $full_size_image[0],
'data-large_image_width' => $full_size_image[1],
'data-large_image_height' => $full_size_image[2],
);
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '</div>';
}
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, get_post_thumbnail_id( $post->ID ) );
// Uncommenting this will display the product thumbnails gallery below the main image
// do_action( 'woocommerce_product_thumbnails' );
?>
</figure>
</div>
</div>
<?php
endif;
This will display the image with the zoom in a classic storefront styling

display woocommerce related products under product gallery

I removed related products from single product
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
now I want to display related products under product image gallery. Here is product gallery code.
/*******Single Product ********/
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_product_thumbnails', 'show_product_thumbnails', 20 );
add_action( 'woocommerce_before_single_product_summary', 'show_product_images', 10 );
function show_product_thumbnails(){
global $post, $product, $woocommerce;
$attachment_ids = $product->get_gallery_attachment_ids();
$count_gallery = count($attachment_ids);
echo '<div class="thumbnails" id="gallery">';
if ( $attachment_ids ) {
echo '<div class="main-slider" style="width:85%; float:right;position: relative;">'
echo '<div class="slider slider-for">';
foreach ( $attachment_ids as $attachment_id ) {
$totem_single_image = wp_get_attachment_image_src( $attachment_id, 'shop_single', true );
$image_link = $totem_single_image[0];
$main_image_link = wp_get_attachment_url( $attachment_id );
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image = wp_get_attachment_image(
$attachment_id,
apply_filters( 'single_product_small_thumbnail_size', 'shop_single' ),
0,
$attr = array(
'title' => $image_title,
'alt' => $image_title
)
);
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<img class="lily_zoom" src="%s" alt="%s" data-zoom-image="%s">', $main_image_link, $image_caption, $image_link, $image_title, $main_image_link), $attachment_id, $post->ID );
}
echo '</div>';
echo'</div>';
echo '<div class="pagination" style="width:13%; display:block;">';
echo '<div class="slider slider-nav" role="toolbar">';
foreach ( $attachment_ids as $attachment_id ) {
$totem_single_image = wp_get_attachment_image_src( $attachment_id, 'medium', true );
$image_link = $totem_single_image[0];
$main_image_link = wp_get_attachment_url( $attachment_id );
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image = wp_get_attachment_image(
$attachment_id,
apply_filters( 'single_product_small_thumbnail_size', 'shop_single' ),
0,
$attr = array(
'title' => $image_title,
'alt' => $image_title
)
);
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<img src="%s" alt="%s"></a>', $image_link,$image_title), $attachment_id, $post->ID );
}
echo '</div>';
echo '</div>';
}
echo '</div>';
}
function show_product_images(){
global $post, $woocommerce, $product;
echo '<div class="images lily-main-image">';
if ( has_post_thumbnail() ) {
$image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
$main_image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail(
$post->ID,
apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ),
array(
'title' => get_the_title( get_post_thumbnail_id() ),
'data-zoom-image' => $main_image_link,
'id' => 'totemImgZoom'
)
);
$attachment_count = count( $product->get_gallery_attachment_ids() );
// echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img width="320px"src="%s" id="totemImgZoom" data-zoom-image="%s" title="%s" alt="">', $main_image_link, $main_image_link, $image_caption), $post->ID);
}
else {
// echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img class="zoomImage" src="%s" alt="%s"/>', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
do_action( 'woocommerce_product_thumbnails' );
do_action( 'woocommerce_output_related_products');
echo '</div>';
}
Ok, here is the solution.
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_output_related_products', 25);
function woocommerce_output_related_products(){
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand'
);
woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
}

Display WooCommerce Product Gallery with FlexSlider

I set up a WooCommerce Theme. Now I'm trying to visualize the Product Gallery Images on the Product Detail-Pages with Flexslider.
I implemented the ".js" and the Styles that are needed.
But Im not quite sure where to put in which Code. Im guessing I have to manipulate the "product-image.php" inside the "WooCommerce/single-product"-Folder.
This is what I have done so far inside that File:
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '
<div class="flexslider">
<ul class="slides">
<li>
%s
</li>
</ul>
</div>', $image_link, $image_caption, $image ), $post->ID );
I want it to look something similar to this:
http://www.twothirds.com/product/aruba_sand/
Any Ideas of what am I doing wrong so far?
Looking forward for any help :)
Custom Flexslider for Woocommerce.
* This template needs be overridden by copying it to yourtheme/woocommerce/single-product/product-thumbnails.php. replace the contents of this template with code provided. Woocommerce Version 2.5.5 April 2016.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $product, $woocommerce;
$attachment_ids = $product->get_gallery_attachment_ids();
if ( $attachment_ids ) {
$loop = 0;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
?>
<div id="shop-slider" class="flexslider">
<ul class="slides"><?php
foreach ( $attachment_ids as $attachment_id ) {
$image_link = wp_get_attachment_url( $attachment_id );
if ( ! $image_link )
continue;
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image_caption = esc_attr( get_post_field( 'post_excerpt', $attachment_id ) );
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_single' ), 0, $attr = array(
'title' => $image_title,
'alt' => $image_title
) );
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<li>%s</li>', $image ), $attachment_id, $post->ID, $image_class );
$loop++;
}
?></ul>
</div>
<?php
}
Then follow this tutorial to get flexslider going http://flexslider.woothemes.com/.
Hope this helps.

Modification to WooCommerce for WordPress featured image link

I need to change the link code that is being used for external products for woo commerce.
This is the code that generates the product image:
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.3
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$attachment_count = count( get_children( array( 'post_parent' => $post->ID, 'post_mime_type' => 'image', 'post_type' => 'attachment' ) ) );
if ( $attachment_count != 1 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
So I just want to change out the image link to the external link url so if someone clicks on the picture, they will go to the external link instead of a blow up of the image
The code to do so is within the add to cart code, but I don't know how to apply it to the image:
<?php
/**
* Loop Add to Cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
?>
<?php if ( ! $product->is_in_stock() ) : ?>
<?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?>
<?php else : ?>
<?php
$link = array(
'url' => '',
'label' => '',
'class' => ''
);
$handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->product_type, $product );
switch ( $handler ) {
case "variable" :
$link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
break;
case "grouped" :
$link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
break;
case "external" :
$link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
break;
default :
if ( $product->is_purchasable() ) {
$link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
$link['label'] = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
$link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
} else {
$link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
}
break;
}
echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('%s', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
?>
Sorry I'm such a nube, but I can't figure out how to get the link code the same as the 'add to cart' link code, thanks for the help!
Why not copy product-image.php to your theme or template (this will overwrite the Woocommerce default) and then change the following:
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
Where it says
<a href="%s"
Replace %s with your link, or you can register a custom post field and have a different link for each of your products.
You can ignore the "add to cart" code if this is all you are trying to accomplish. I might have misunderstood - so let me know if it's still unclear.

Resources