skip to Main Content

I would like to display the short description of my product rather than its name in my cart and the checkout I went to the cart.php file and I found a code I think I must change it
someone would have a solution for me ?

    <td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
    <?php
    if ( ! $product_permalink ) {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), 
    $cart_item, $cart_item_key ) . '&nbsp;' );
    } else {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', 
    esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
                    }

2

Answers


  1. A better solution is to use a hook. for example:

    add_filter( 'woocommerce_cart_item_name', 'ywp_custom_text_cart_item_name', 10, 3 );
    function ywp_custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
        $item_name .= '<br /><div class="my-custom-text-class">Custom text comes here</div>';
    
        return $item_name;
    }
    

    Code goes in functions.php file of your active theme/child theme. Tested and works.

    Login or Signup to reply.
  2. If you want to display the short description in the cart and in the checkout you can do it using the woocommerce_cart_item_name hook.

    If you want to show the short description of all product types you can use this code:

    // returns the short description as the cart item name
    add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
    function change_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {
    
        $product = $cart_item['data'];
    
        // get short description
        $short_description = $product->get_short_description();
    
        // if it exists, it returns the short description as the cart item name
        if ( ! empty( $short_description ) ) {
            return $short_description;
        } else {
            return $item_name;
        }
    
        return $item_name;
    
    }
    

    If, in the case of product variations, you want to show the short description of the parent product (variable product) you can use the following code:

    // returns the short description as the cart item name
    add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
    function change_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {
    
        $product = $cart_item['data'];
    
        // if it is a variation it gets the description of the variable product
        if ( $product->is_type( 'variation' ) ) {
            $parent_id = $product->get_parent_id();
            $variable = wc_get_product( $parent_id );
            $short_description = $variable->get_short_description();
        } else {
            // get short description
            $short_description = $product->get_short_description();
        }
    
        // if it exists, it returns the short description as the cart item name
        if ( ! empty( $short_description ) ) {
            return $short_description;
        } else {
            return $item_name;
        }
    
        return $item_name;
    
    }
    

    It works both on the cart page and in the checkout.

    The code must be added in your theme’s functions.php file.

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