Display a custom thumbnail for a specific product ID in Woocommerce cart

Display a custom thumbnail for a specific product ID in Woocommerce cart



In Woocommerce, I would like to change a specific cart item thumbnail with a personalized image. I can change the image, but it change all cart item thumbnails.

I only want to change the image of a specific product ID.



How can I do this?



Here is the code I'm using:


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

function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item,

$cart_item_key )

$cart = WC()->cart->get_cart();

foreach( $cart as $cart_item )

$product = wc_get_product( $cart_item['product_id'] );

if($cart_item['product_id'] == 75)

echo 'New Image Here';




;



enter image description here



Any help please.




1 Answer
1



You don't need a foreach loop because the $cart_item is already included as an argument in the function. The following code will work for all product types and will allow you yo have a custom thumbnail for a specific product in cart page:


$cart_item


add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 20, 3 );
function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key )
// HERE your targeted product ID
$targeted_id = 75;

if( $cart_item['product_id'] == $targeted_id



Code goes in function.php file of your active child theme (or active theme). Tested and works.





Worked like a charm! Thank you so much for your help!!!!
– FOR
Aug 26 at 1:49






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế