Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook for customizing product image thumbnail

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.

like image 550
krishna Avatar asked Sep 17 '25 00:09

krishna


2 Answers

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).

like image 175
Justas Avatar answered Sep 19 '25 14:09

Justas


To change thumbnail image size of WooCommerce cart page you need next steps:

  1. 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
    }
    
  2. 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 );
    
like image 32
Arif Rahman Avatar answered Sep 19 '25 15:09

Arif Rahman