pcoder
pcoder

Reputation: 117

woocommerce cart custom thumbnail image image in cart

I want to show custom thumbnail images in cart. I made product with a custom attribute, say imageurl.

I used the hook below to make it work:

function custom_new_product_image($cart_object) {
    $a = '<img src="imageurlhere" />';
    return $a;
}

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );

My code works well if I put a static url in place of "imageurlhere" but I want to pass a custom product attribute image url.

I am able to get the image url with

$cart_object->cart_contents['wccpf_imageurl']

How do I use a custom product attribute image url in place of a static one?

Upvotes: 1

Views: 12211

Answers (2)

Motahar Hossain
Motahar Hossain

Reputation: 563

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

Step-1: in function.php create the size you need.

if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'custom-thumb', 100, 100 ); // 100 wide and 100 high
}

Step-2: in cart.php which should be located in your_tneme\woocommerce\cart find

$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

and in get_image() write the name of your size. In our case, it will be like this: get_image(‘custom-thumb’)

Step-3: If you check the image source, you can see that the image size is changed. But still, you may need to customize the CSS to display your expected style.

Checked work on WooCommerce latest version 3.7.1

Upvotes: 0

hemnath mouli
hemnath mouli

Reputation: 2755

function custom_new_product_image( $_product_img, $cart_item, $cart_item_key ) {
    $a      =   '<img src="'.$cart_item['wccpf_width'].'" />';
    return $a;
}

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image', 10, 3 );

Upvotes: 11

Related Questions