Ad

Display Product Variation Description Or Product Short Description Based On Product Type In WooCommerce Checkout

This code snippet displays product short description at WooCommerce checkout:

// 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 );

The issue is that a variable product can only have 1 product short description, so all of the product variations have the same exact description.

Is it possible to modify this code snippet to display product variation description instead of product short description for variable products?

Ad

Answer

To display product variation description instead of product short description for variable products, you can use:

// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {   
    // Compare
    if ( $cart_item['data']->get_type() == 'variation' ) {
        // Get the variable product description
        $description = $cart_item['data']->get_description();
    } else {    
        // Get product excerpt
        $description = get_the_excerpt( $cart_item['product_id'] );
    }       
        
    // Isset & NOT empty
    if ( isset ( $description ) && ! empty( $description ) ) {
        $item_data[] = array(
            'key'     => __( 'Description', 'woocommerce' ),
            'value'   => $description,
            'display' => $description,
        );
    }
    
    return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );
Ad
source: stackoverflow.com
Ad