I need a help on woo-commerce to override the cart product image thumbnail.
I am creating a plugin for customizing the product in the detail page and if we do "add to cart" it will be updated in the cart page with a customized thumbnail.
If any hook is available for overriding the image, please let me know.
I've spent many hours searching for the answer also and even asked a Stackoverflow question (WooCommerce: change product image permalink with filter/action hook) which now happens to be duplicate (could not find this question prior to submitting my own).
The answer:
The hook is woocommerce_cart_item_thumbnail.
So in your functions.php add
function custom_new_product_image($a) {
$class = 'attachment-shop_thumbnail wp-post-image'; // Default cart thumbnail class.
$src = [PATH_TO_YOUR_NEW_IMAGE];
// Construct your img tag.
$a = '<img';
$a .= ' src="' . $src . '"';
$a .= ' class="' . $class . '"';
$a .= ' />';
// Output.
return $a;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );
and your thumbnails will be replaced (more processing needed if you want to change each thumbnail individually).
To change thumbnail image size of WooCommerce cart page you need next steps:
In function.php create the size you need:
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'custom-thumb', 100, 100, true ); // 100 wide and 100 high
}
In cart.php which should be located in your_theme\woocommerce\cart\cart.php find
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image( 'custom-thumb' ), $cart_item, $cart_item_key );
Please review the WooCommerce cart templates in woocommerce/templates/cart/cart.php.
There is a clear filter woocommerce_cart_item_thumbnail for the product thumbnail in the cart.
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
Change 'medium' for your required image size:
/**
* Update cart product thumbnail
*/
function woocommerce_cart_item_thumbnail_2912067($image, $cartItem, $cartItemKey)
{
$id = ($cartItem['variation_id'] !== 0 ? $cartItem['variation_id'] : $cartItem['product_id']);
return wp_get_attachment_image(get_post_thumbnail_id((int) $id), 'medium');
}
add_filter('woocommerce_cart_item_thumbnail', 'woocommerce_cart_item_thumbnail_2912067', 10, 3);
This results in your not having to update the core WooCommerce template files.
Related
I have several thousand products on the store that do not have a thumbnail. So I came up with a code to get them from the "featured-images" attribute, which has a link in it. On the store page, the category page works fine, but the product in the cart does not display the image. What am I doing wrong?
function alterImageSRC($image, $attachment_id, $size, $icon){
global $product;
$url = "https://website.com/wp-content/uploads/thumbs/placeholder.png";
if (!is_null($product)) {
$optimex_featured = $product->get_attribute( 'pa_featured-images' );
$url = "https://website.com/wp-content/uploads/thumbs/$optimex_featured";
}
$image[0] = $url;
return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
I'm building a website with some articles accessible only for Members. All articles, accessible or not are shown on a page and i would like to add an icon on top of the featured image for people to understand which articles they can see for free or not.
Could you help ? I've already read the doc about featured image but my brain can't make the whole thing...
Here's what i already tried :
function article_reserve( $title ) {
if ( has_tag( 'abonne' ) ) {
$title = 'test ' . $title;
}
return $title;
}
add_filter( 'the_title', 'article_reserve' );
It change the title but i'm trying to change the featured image
Also tried that after comments :
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Optionally add any logic here for determining what markup to output.
// $html will be an empty string if there is no post thumbnail.
if ( has_tag( 'abonne' ) ) {
$new_html = '<img src="https://placekitten.com/g/600/600" alt="kitten">';
}
return $new_html;
}
add_action( 'post_thumbnail_html', 'wpse_post_thumbnail_html', 10, 5 );
I have a short piece of code that changes store images through the first gallery image.
This works great, but how do you change this code for displaying cart thumbnails as the first gallery image?
Any help or pointers to what I should be doing to achieve this would be very much appreciated.
My code:
add_action( 'woocommerce_init', 'new_replace_loop_product_thumbnail' );
function new_replace_loop_product_thumbnail() {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
function new_replace_product_thumbnail() {
global $product;
$attachment_id = $product->get_gallery_attachment_ids()[0];
echo "<img src='" . wp_get_attachment_url( $attachment_id ) . "'>";
}
add_action( 'woocommerce_before_shop_loop_item_title', 'new_replace_product_thumbnail', 10 );
}
You could use the woocommerce_cart_item_thumbnail filter hook.
Note: wp_get_attachment_image() contains several (optional) parameters with which the HTML img element that is returned can be adjusted.
Parameters
$attachment_id
(int) (Required) Image attachment ID.
$size
(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).
Default value: 'thumbnail'
$icon
(bool) (Optional) Whether the image should be treated as an icon.
Default value: false
$attr
(string|array) (Optional) Attributes for the image markup.
'src'
(string) Image attachment URL.
'class'
(string) CSS class name or space-separated list of classes. Default attachment-$size_class size-$size_class, where $size_class is the image size being requested.
'alt'
(string) Image description for the alt attribute.
'srcset'
(string) The 'srcset' attribute value.
'sizes'
(string) The 'sizes' attribute value.
'loading'
(string|false) The 'loading' attribute value. Passing a value of false will result in the attribute being omitted for the image. Defaults to 'lazy', depending on wp_lazy_loading_enabled().
Default value: ''
Return
(string) HTML img element or empty string on failure.
So to answer your question, you could use:
function filter_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ) {
// Get product
$product = $cart_item['data'];
// Get gallery image ids
$attachment_ids = $product->get_gallery_image_ids();
// NOT empty
if ( ! empty ( $attachment_ids ) ) {
// First
$attachment_id = $attachment_ids[0];
// New thumbnail
$thumbnail = wp_get_attachment_image( $attachment_id, 'woocommerce_thumbnail' );
}
return $thumbnail;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'filter_woocommerce_cart_item_thumbnail', 10, 3 );
I'm trying to add an image from the product gallery to the Woocommerce checkout. I've managed it on the cart using the following code:
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
But this won't work in the checkout. I've currently got the main product image showing using this code:
$thumbnail = apply_filters( 'woocommerce_in_cart_product_thumbnail', $_product->get_image(), $values, $cart_item_key );
echo $thumbnail;
But I can't figure out how to adapt it to allow the gallery image through.
Any help would be appreciated!
Cheers,
Ash
I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.
Example:
You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/"
When you click on the button, it adds "-sample" to the URL
"http://www.example.com/shop/product-1-sample/"
How can I achieve this?
Thanks
For woocommerce 3+ (only):
In woocommerce 3 you will use woocommerce_after_shop_loop_item action hook instead, as the hook woocommerce_after_add_to_cart_button will not work anymore.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = $product->get_permalink();
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Before woocommerce 3:
This is possible using hook woocommerce_after_add_to_cart_button to add your additional button on product pages, using this custom function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = get_permalink( get_the_id() );
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
This code goes on function.php file of your active child theme or theme.
This code is tested and fully functional.
Based on this: Add a button after add to cart and redirect it to some custom link in WooCommerce
And this: PHP - How to remove all specific characters at the end of a string?
It's been a long time since the original question, but here's a recent update that works for me, WooCommerce 6.3.1:
/* WooCommerce customization */
add_action( 'woocommerce_after_shop_loop_item', 'custom_select_link', 11 );
function custom_select_link() {
global $product;
// Custom "Select" button.
echo '<a class="custom-button" href="' . esc_url( get_permalink( $product->id ) ) . '"><button class="custom-button"> </button></a>';
}
This answer cites an answer in wordpress.stackexchange.