I'm using Removing link from product thumbnail on cart page answer code to remove the permalink from the thumbnail image on the cart page.
Now I would like to wrap the thumbnail in a span or another element. I would like to leave the cart thumbnail intact. Is there a hook for this? I don't use a theme but a plugin.
Currently I am using the following code
function custom_woocommerce_cart_item_thumbnail($a) {
$class = 'attachment-shop_thumbnail wp-post-image'; // Default cart thumbnail class.
$src = 'dummy';
// Construct your img tag.
$a = '<img';
$a .= ' src="' . $src . '"';
$a .= ' class="' . $class . '"';
$a .= ' />';
// Output.
return $a;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'custom_woocommerce_cart_item_thumbnail', 10, 3 );
Someone who can guide me on how to apply this?
The cart/cart.php template file contains (line 65-75)
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</td>
This ensures the output of the thumbnail with associated HTML code
The woocommerce_cart_item_thumbnail filter hook, first paramater contains $_product->get_image()
Which then leads us to the next code
/**
* Returns the main product image.
*
* #param string $size (default: 'woocommerce_thumbnail').
* #param array $attr Image attributes.
* #param bool $placeholder True to return $placeholder if no image is found, or false to return an empty string.
* #return string
*/
public function get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true ) {
$image = '';
if ( $this->get_image_id() ) {
$image = wp_get_attachment_image( $this->get_image_id(), $size, false, $attr );
} elseif ( $this->get_parent_id() ) {
$parent_product = wc_get_product( $this->get_parent_id() );
if ( $parent_product ) {
$image = $parent_product->get_image( $size, $attr, $placeholder );
}
}
if ( ! $image && $placeholder ) {
$image = wc_placeholder_img( $size, $attr );
}
return apply_filters( 'woocommerce_product_get_image', $image, $this, $size, $attr, $placeholder, $image );
}
which in turn invokes other functions.
However, because you want to keep the product thumbnail intact and wrap it in span tags, you can just use.
function filter_woocommerce_cart_item_thumbnail( $product_image, $cart_item, $cart_item_key ) {
// Wrap the thumbnail in a span
$product_image = '<span>' . $product_image . '</span>';
return $product_image;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'filter_woocommerce_cart_item_thumbnail', 10, 3 );
Note: if you want to change something in the element you can use the str_replace PHP function
Like:
$image = '<img src="myimage.jpg" width="500" height="600">';
Result before:
<img src="myimage.jpg" width="500" height="600">
With str_replace:
// Replace <img with <span><img
$image = str_replace( '<img', '<span><img', $image );
Result after:
<span><img src="myimage.jpg" width="500" height="600">
Another option is to overwrite the template file (see the answer you already referred to on how to apply), so in conjunction with that answer would
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
Become
echo '<span>' . $thumbnail . '</span>';
Related
I'm trying to change default image placeholder for products with no images to images according to their product category.
I'm using:
add_filter( 'woocommerce_placeholder_img_src', 'my_custom_woocommerce_placeholder', 10 );
// Function to return new placeholder image URL.
function my_custom_woocommerce_placeholder( $image_url )
{
if ($product_category == 'category1') {
$image_url = 'http://website.com/imageforCategory.png';
}
elseif ($product_category == 'category2') {
$image_url = 'http://website.com/imageforCategory.png';
}
else
{
$image_url = 'http://website.com/defaultImage.png';
}
return $image_url;
}
I'm stuck at getting product category ($product_category) detected in functions.php file of my theme.
In content-single-product.php page I'm just defining
global $product
and then just using
$product->get_categories();
to get the category of product.
How do I do it in functions.php of my theme?
You could use has_term() - Checks if the current post has any of given terms.
So you get:
// Single product page
function filter_woocommerce_placeholder_img_src( $src ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Has term (product category)
if ( has_term( 'categorie-1', 'product_cat', $product->get_id() ) ) {
$src = 'http://website.com/imageforCategory1.png';
} elseif ( has_term( array('categorie-2', 'categorie-3'), 'product_cat', $product->get_id() ) ) {
$src = 'http://website.com/imageforCategory2.png';
}
}
return $src;
}
add_filter( 'woocommerce_placeholder_img_src', 'filter_woocommerce_placeholder_img_src', 10, 1 );
// Archive/Shop page
function filter_woocommerce_placeholder_img ( $image_html, $size, $dimensions ) {
$dimensions = wc_get_image_size( $size );
$default_attr = array(
'class' => 'woocommerce-placeholder wp-post-image',
'alt' => __( 'Placeholder', 'woocommerce' ),
);
$attr = wp_parse_args( '', $default_attr );
$image = wc_placeholder_img_src( $size );
$hwstring = image_hwstring( $dimensions['width'], $dimensions['height'] );
$attributes = array();
foreach ( $attr as $name => $value ) {
$attribute[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
}
$image_html = '<img src="' . esc_url( $image ) . '" ' . $hwstring . implode( ' ', $attribute ) . '/>';
return $image_html;
}
add_filter( 'woocommerce_placeholder_img', 'filter_woocommerce_placeholder_img', 10, 3 );
Note: the woocommerce_placeholder_img hook, used on archive/shop page returns $image_html. That string can be adapted to your needs. Such as image src, class, alt, size, etc ..
All the thumbnails in the cart are run through the following woocommerce_cart_item_thumbnail filter:
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
Any idea how to add a custom class to the <img> tag of the thumbnail through a function in the functions.php file of my child theme?
You can specify class in attr argument of get_image function like this
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(null, array('class' => 'my-custom-image-class')), $cart_item, $cart_item_key );
get_image function accepts 3 arguments
/**
* Returns the main product image.
*
* #param string $size (default: 'woocommerce_thumbnail').
* #param array $attr Image attributes.
* #param bool $placeholder True to return $placeholder if no image is found, or false to return an empty string.
* #return string
*/
public function get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true ) {
$image = '';
if ( $this->get_image_id() ) {
$image = wp_get_attachment_image( $this->get_image_id(), $size, false, $attr );
} elseif ( $this->get_parent_id() ) {
$parent_product = wc_get_product( $this->get_parent_id() );
if ( $parent_product ) {
$image = $parent_product->get_image( $size, $attr, $placeholder );
}
}
if ( ! $image && $placeholder ) {
$image = wc_placeholder_img( $size, $attr );
}
return apply_filters( 'woocommerce_product_get_image', $image, $this, $size, $attr, $placeholder, $image );
}
In second $attr argument you can specify img tag attributes including class, if you defined your own custom thumbnail size, instead of passing null in first argument you can pass it, like this
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image('my-custom-thumbnail-size', array('class' => 'my-custom-image-class')), $cart_item, $cart_item_key );
you can simply find and replace the class
add_filter( 'woocommerce_cart_item_thumbnail', 'custom_class_woocommerce_cart_item_thumbnail_filter', 10, 3 );
function custom_class_woocommerce_cart_item_thumbnail_filter( $product_image, $cart_item, $cart_item_key ){
$product_image = str_replace('class="', 'class="your-class ', $product_image);
return $product_image;
}
I am trying to display how many percent the user saves on the sales badge in WooCommerce. To make this work without this overwriting the "Featured" sales badge, I had to add some filters to my functions.php. Everything works fine now, exept that the product image on the product page disappears and I can't for the life of me understand why. The "onsale featured" code in the theme file product-image.php is almost the same as the "onsale" code, and I can't understand why the image would disappear when a product is on sale, but not when it is featured?
My code in functions.php
add_filter('woocommerce_sale_flash', 'my_custom_sale_flash' );
function my_custom_sale_flash($text) {
global $product;
if($product->is_on_sale()){
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return '<span class="onsale">SPAR '.$percentage.' %</span>';
}elseif($product->is_featured()){
return '<span class="onsale featured">Utvalgt</span>';
}else if(!$product->is_in_stock()){
return '<span class="onsale">Utsolgt</span>';
}else if(!$product->get_price()){
return '<span class="onsale">GRATIS</span>';
}
}
The code in product-image.php in the ROEN theme
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.14
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = "";
$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) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
$zoom = "";
} else {
$gallery = '';
$zoom = 'zoom';
}
$html = '';
if($product->is_featured()){
$html = apply_filters('woocommerce_sale_flash', '<span class="featured onsale">'.__('Featured','ROEN').'</span>', $post, $product);
}else if($product->is_on_sale()){
$html = apply_filters('woocommerce_sale_flash', '<span class="onsale">'.__( 'Sale!', 'woocommerce' ).'</span>', $post, $product);
}else if(!$product->is_in_stock()){
$html = '<span class="outofstock">'.__('Out of Stock','ROEN').'</span>';
}else if(!$product->get_price()){
$html = '<span class="free onsale">'.__('Free','ROEN').'</span>';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s'.$html.'', $image_link, $image_title, $image_link , $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>
I can't see why the problem arises? Featured products that are not on sale displays correctly both when viewed from a category and on the product page. Products that are on sale displays correctly when viewed from a category, but the image disappears on the product page.
Thank you for your time.
I got this fixed by changing
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s'.$html.'', $image_link, $image_title, $image_link , $image ), $post->ID );
To
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s%s', $image_link, $image_title, $image_link , $image, $html ), $post->ID );
Turns out Wordpress did not like the percentage sign in my span that was going to show in the sales badge, because of the way the ROEN theme is coded.
I tell you man, lot's of weird code in that theme.
I'm building a Woocommerce site.
In the Shop grid overview, I'm showing the Featured Image of each product (see link). This Featured image will be cropped to maintain the same image ratio in the Shop Grid.
In the Single Product page, I managed to hide the Featured Image, and make the first of the thumbnails appear in big (see link).
I did so with the following code:
<div class="images">
<?php
$imgid = $product->get_gallery_attachment_ids();
?>
<a href="<?php echo wp_get_attachment_url( $imgid[0] ); ?>"
class="woocommerce-main-image zoom first"
rel="lightbox[product-gallery]">
<img src="<?php echo wp_get_attachment_url( $imgid[0] ); ?>" alt="">
</a>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
<script>
jQuery('.thumbnails.columns-3 a:first-child').hide()
</script>
The first part will find the first image in the gallery array and show it in big size (class woocommerce-main-image zoom first) while linking to the lightbox.
Then I call the thumbnails, and I hide the first one using jQuery to avoid a duplicate (first big size image and first thumb are the same).
The problem now is that in the Lightbox, the first image will appear duplicated, as it exists two times in the array, the first one I call in big, and the one from the thumbs array.
Any tips on how to not show the image twice in the lightbox?
Someone mentioned that I should filter the following function, but as of now I don't know how to do that.
public function get_gallery_attachment_ids() {
return apply_filters( 'woocommerce_product_gallery_attachment_ids', array_filter( (array) explode( ',', $this->product_image_gallery ) ), $this );
}
I think that using Multiple Post Thumbnails is the easiest solution. It is exactly for displaying different featured images in different locations.
Option #1: Multiple Post Thumbnails
You would install the plugin and then add the following to your theme's functions.php. It isn't 100% tested so there may be a stray typo or something. Full documentation is here.
// register the new thumbnail
function so_31835142_register_extra_thumbnail() {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => __('Product Loop Image', 'your-theme'),
'id' => 'product-loop-image',
'post_type' => 'product'
)
);
}
}
add_action( 'after_setup_theme', 'so_31835142_register_extra_thumbnail' );
// remove the existing loop thumbnail
function so_31835142_swap_loop_product_thumbnail(){
if (class_exists('MultiPostThumbnails')) {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'so_31835142_loop_product_thumbnail', 10 );
}
}
add_action( 'woocommerce_before_shop_loop_item, 'so_31835142_swap_loop_product_thumbnail' );
// Display the Secondary Thumbnail
function so_31835142_loop_product_thumbnail(){
global $product;
$thumbnail = MultiPostThumbnails::get_post_thumbnail(
'product',
'product-loop-image',
$product->id,
'shop_catalog'
);
if ( $thumbnail ) {
return $thumbnail;
} elseif ( wc_placeholder_img_src() ) {
return wc_placeholder_img( $size );
}
}
Then to use it, you'd set "Product Loop Image" the same way you traditionally set the "featured image". And this new image would be used in the loop.
Option #2: Template Overrides
But as an alternative, if you insist, you can write a custom single-product/product-image.php template and put it in your theme's woocommerce templates folder.
In this alternative we will only show images from the image gallery on the single product page, $product->get_gallery_attachment_ids(), and we will use a basic PHP loop and counter system to display the images differently depending on where they are in the loop. IE. the first image will display as the post thumbnail used to display and the remaining items will display as thumbs.
This section I have tested, so it should (in theory) be good to go.
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.14
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
if ( $attachment_ids ) {
$loop = 0;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
$attachment_count = count( $attachment_ids );
foreach ( $attachment_ids as $attachment_id ) {
// here's your first image
if( $loop === 0 ){
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image_caption = get_post( $attachment_id )->post_excerpt;
$image_link = wp_get_attachment_url( $attachment_id );
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), null, array(
'title' => $image_title,
'alt' => $image_title
) );
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 );
// resume the thumbnails for the rest
} else {
// open the thumbnails div
if( $loop === 1 ) { ?>
<div class="thumbnails <?php echo 'columns-' . $columns; ?>">
<?php }
$classes = array( 'zoom' );
if ( $loop == 0 || $loop % $columns == 0 )
$classes[] = 'first';
if ( ( $loop + 1 ) % $columns == 0 )
$classes[] = 'last';
$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_thumbnail' ), 0, $attr = array(
'title' => $image_title,
'alt' => $image_title
) );
$image_class = esc_attr( implode( ' ', $classes ) );
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '%s', $image_link, $image_class, $image_caption, $image ), $attachment_id, $post->ID, $image_class );
// close the thumbnails div
if( $loop === $attachment_count ) { ?>
</div>
<?php }
}
$loop++;
}
?>
<?php
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
</div>
I am having a little trouble editing a Woocommerce template with hooks. Essentially I would just like to change the product-image template so instead of linking to the uploaded product image, it links to the product post.
The current product-image.php woocommerce template has
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$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
) );
$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_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>
I am unsure of how to adapt the echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID ); to change the %s to a link to the post.
The hook I am using is:
add_action('woocommerce_product_thumbnails', 'custom_links');
function custom_links() {
//code
}
Could someone help me gain some direction with this?
You are calling action not filter. Also you are calling the wrong one.
Change this:
add_action('woocommerce_product_thumbnails', 'custom_links');
to this:
add_filter('woocommerce_single_product_image_html', 'custom_links', 10, 2);
The 2 represents the argument count for the function and your custom_links() should be something like:
function custom_links($link, $post_id) {
$pattern = '/(?<=href=")([^"]*)/';
$replacement = get_permalink($post->ID);
return preg_replace($pattern, $replacement, $link);
}
Process the $link variable as needed and then return it.