Skip to content
Advertisement

Issue when adding product description at WooCommerce checkout

I’m trying to display the product description at WooCommerce checkout.

I’m using:

add_filter( 'woocommerce_get_item_data', 'wc_checkout_product_description_2', 10, 2 );
function wc_checkout_product_description_2( $other_data, $cart_item )
{
    $post_data = get_post( $cart_item['product_id'] );
    $other_data[] = array( 'name' =>  $post_data->post_excerpt );
    return $other_data;
}

It takes the product short description and displays it below the product.

It almost works perfectly, but it displays a colon (:) at the end of the description and the colon remains even if there is no short description.

Result:

enter image description here


I can’t figure out why it appears any how to get rid of it. I need some help.

Advertisement

Answer

The colon is automatically added to the key. Because you only use name, this will be added at the end of the product description.

To display this correctly, you can use:

// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
    // Product excerpt
    $post_excerpt = get_the_excerpt( $cart_item['product_id'] );
    
    // NOT empty
    if ( ! empty( $post_excerpt ) ) {
        $item_data[] = array(
            'key'     => __( 'Product description', 'woocommerce' ),
            'value'   => $post_excerpt,
            'display' => $post_excerpt,
        );
    }
    
    return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );

OR if you really want to remove the colon/name (key). In addition to using the filter hook with code from my answer, you will also have to overwrite the cart/cart-item-data.php template file

  • This template can be overridden by copying it to yourtheme/woocommerce/cart/cart-item-data.php.

Replace

<dl class="variation">
    <?php foreach ( $item_data as $data ) : ?>
        <dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( $data['key'] ); ?>:</dt>
        <dd class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( wpautop( $data['display'] ) ); ?></dd>
    <?php endforeach; ?>
</dl>

With

<dl class="variation">
    <?php foreach ( $item_data as $data ) : ?>
        <dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( wpautop( $data['display'] ) ); ?></dt>
    <?php endforeach; ?>
</dl>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement