skip to Main Content

How can I remove the link (but keep theproduct thumbnail image) fromthe following code

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

    if ( ! $product_permalink ) {
        echo $thumbnail; // PHPCS: XSS ok.
    } else {
        printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
    }
?>

It is part of the Woocommerce cart.php. I want to keep the link for the product name, but remove it from the thumbnail.

2

Answers


  1. To remove the product permalink from each cart item, simply use the following into the functions.php file of your active child theme (or active theme):

    add_filter( 'woocommerce_cart_item_permalink', '__return_false' );
    

    Tested and works.


    If you want to remove the product link only from the thumbnail in cart page use the following:

    First read “Template structure & Overriding templates via a theme” official documentation to understand how to Override WooCommerce templates via the active child theme (or active theme).

    Once you have copied the template cart/cart.php into your theme as explained before, open edit it and replace the lines:

    if ( ! $product_permalink ) {
        echo $thumbnail; // PHPCS: XSS ok.
    } else {
        printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
    }
    

    by:

    echo $thumbnail;
    

    You are done. The product link is now removed from the thumbnail.

    Login or Signup to reply.
  2. The OP asks about how to remove the link from the product thumbnail on the cart page. The first solution from the LoicTheAztec answer will remove the link both from the product thumbnail and product title, that is great, but even if it is necessary, it is too much.

    add_filter( 'woocommerce_cart_item_permalink', '__return_false' );
    

    The second solution of the LoicTheAztec answer, implying a template override, is too complex for this. Less difficult is to add a second filter, in addition to the first solution, that will add back the link only to the product titles.

    add_filter( 'woocommerce_cart_item_name', 'add_back_product_link', 10, 2 );
    function add_back_product_link( $cart_item_name,  $cart_item ) {
        $cart_item_name = '<a href="' . $cart_item['data']->get_permalink() . '">' . $cart_item_name . '</a>';
        return $cart_item_name;
    }
    

    Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search