FOR
FOR

Reputation: 45

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.

Upvotes: 2

Views: 1946

Answers (2)

Seth M
Seth M

Reputation: 1

That solution by LoicTheAztec was great, but it will leave all your thumbnails blank except for the ones that matches the condition within the if statement. It got me 90% of the way there. To overcome that, I have added...

echo $thumbnail;

After adding that (outside the if block) you can see all the thumbnails unscathed along with your custom thumb. The LoicTheAztec solution also has an OR operator but because it uses exactly the same condition as the first condition before the OR it isn't needed. Perhaps it is just a placeholder for scenarios where youd need to meet multiple conditions.

In a scenario like this one, a switch statement seems like the better bet. It allows you to easily customize as many thumbs as you like in a very readable way. This is the final code what worked for me...

add_filter( 'woocommerce_cart_item_thumbnail', 'aw_change_woocommerce_cart_item_thumbnail', 20, 3 );
function aw_change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){
  $theProductID = $cart_item['product_id']; // save product ID into a variable
  switch ($theProductID) {
    case 47056:
      //this is a gift membership which needs a different shaped thumbnail than other products;
    echo '<img width="73" height="46" src="/wp-content/uploads/2020/05/giftcard-thumb.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="gift card">';
    break;
    case 47149:
      //this is a gift card which also needs a different shaped thumbnail than other products;
    echo '<img width="73" height="46" src="/wp-content/uploads/2020/05/giftcard73x58.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="gift card">';
    break;
    default:
    echo $thumbnail;
  }
}

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253784

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:

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 || $cart_item['product_id'] == $targeted_id ){
        echo  'New Image Here';
    }
}

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

Upvotes: 2

Related Questions