user13639445
user13639445

Reputation:

Make product name clickable (linked) + thumbnail on checkout page in WooCommerce

I have code that I found online that I have modified slightly to better match the theme I'm working with. What I cannot seem to figure out is how to link to the actual product.

In other words; the product name should be clickable (linked) to the product page. This happens on the cart page, but not on checkout.

The code I am using:

add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );
function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ){

    if (is_checkout()){

    $thumbnail = $cart_item['data']->get_image(array( 80, 80));

    $image_html = '<div class="product-item-thumbnail">'.$thumbnail.'</div> ';

    $product_name = $image_html . $product_name;

    }

    return $product_name;
}

I tried to fix it by looking at the cart template where I found this:

$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

I then added that to the code, turning it into this:

add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );
function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ){

    if (is_checkout()){

    $thumbnail = $cart_item['data']->get_image(array( 80, 80));

    $image_html = '<div class="product-item-thumbnail">'.$thumbnail.'</div> ';

    $product_name_link = $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

    $product_name = $image_html . $product_name_link;

    }

    return $product_name;
}

But that gave me an error and now I am stuck.

Notice: Undefined variable: _product

Upvotes: 1

Views: 1122

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

_product is not defined, hence the notice

This should suffice, comment with explanation added to the code

function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ) {
    // Returns true on the checkout page.
    if ( is_checkout() ) {
        // Get product
        $product = $cart_item['data'];

        // Get image - thumbnail
        $thumbnail = $product->get_image(array( 80, 80));

        // Output
        $image_html = '<div class="product-item-thumbnail">' . $thumbnail . '</div>';                   

        // Product name + link
        $product_name_link = '<a href="' . $product->get_permalink() . '">' . $product_name . '</a>';

        // Output
        $product_name = $image_html . $product_name_link;
    }

    return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );

Upvotes: 3

Related Questions