JOKKER
JOKKER

Reputation: 542

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?

Upvotes: 1

Views: 1654

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

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

Upvotes: 2

Related Questions