0

I want to add the product thumbnail to the review order area of the checkout page in a custom plugin. I want to restrict the apply_filter function so that it only applies to the checkout page, but no matter what I try it also applies it to the cart page.

This is the latest version of what I've tried. I can't figure out why the is_checkout part doesn't seem to do what I thought it should.

/* add product thumbnail to order review table on checkout page only */
add_filter('woocommerce_cart_item_name', 'jwf_order_review_thumb', 20, 2);
function jwf_order_review_thumb($cart_item, $cart_item_key){
global $product;
if (is_checkout()) {
$item_data = $cart_item_key['data'];
$post = get_post($item_data->id);
$thumb = get_the_post_thumbnail($item_data->id, array( 32, 50));
echo '<div id="jwf_checkout_thumbnail" style="float: left; padding-right: 8px">' . $thumb . '</div>' ;
}
}

The second part of the problem is that I want to also give the product name (including variations). For this, I'm starting with something like this:

add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3);
function cart_variation_description( $name, $cart_item, $cart_item_key ) {
    $product_item = $cart_item['data'];
    if(!empty($product_item) && $product_item->is_type( 'variation' ) ) {
        return $name;
    } else
        return $name;
}

What I'd ultimately like to do is combine these two into a single filter that:

  1. determines if the page is the checkout page, then
  2. a. if it is checkout: return both $thumb and $name b. if it isn't checkout: return only $name

I've tried several times to combine these, but am failing miserably. Any help would be much appreciated! Some of the code I have here comes from other sources, so tips/examples of how to clean it up, if needed, would be helpful too. Thanks in advance.

0

You can use the conditional checks to simply build a new return value for the filter. So run the filter once and put all your code in there.

Your second example doesn't really make sense because no matter what, you're returning $name, can you explain that more?

add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3);
function wpse306625_cart_variation_description( $name, $cart_item, $cart_item_key ) {

    $output = '';

    if ( is_checkout() ) {
        $item_data = $cart_item_key['data'];
        $post = get_post($item_data->id);
        $thumb = get_the_post_thumbnail($item_data->id, array( 32, 50));
        $output = '<div id="jwf_checkout_thumbnail" style="float: left; padding-right: 8px">' . $thumb . '</div>' ;
    }
    else {
        $output .= $name;
    }

    return $output;
}
  • Sorry, I didn't explain that well. That second bit of code is just a starting point, which originally had one part outputting $name and the other outputting $thumb and $name. What I essentially need is an if statement that will make it so: if the page is checkout, then it outputs $thumb and $name, else it outputs only $name. – Jon Fergus Jun 23 '18 at 9:22
  • In that case I removed the variation check and then above is all you'd need. – Jeffrey von Grumbkow Jun 25 '18 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.