skip to Main Content

I need to modify the product description to be displayed on checkout. So, I have tried the following code:

add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_15127954', 10, 2 );
function wc_checkout_description_so_15127954( $other_data, $cart_item ) {
    $post_data = get_post( $cart_item['product_id'] );
    $custom = get_post_meta( get_the_ID(), 'order_description', true );        
    $other_data[] = array( 'name' =>  $post_data->$custom );
    return $other_data;
}

But this code changes the description everywhere. I only need it changed after the product has been purchased, to display a custom description on order emails and payment receipts etc… without changing the product description or short description

Note : This issue is related to the displayed description on orders, email notifications, PayPal and CC receipts. The description could be generated from the product description OR from the short description, so any solution should filter/modify both of them just for the display in orders, email notifications, PayPal and CC receipts.


Update – Code addition:

add_action( 'woocommerce_product_options_general_product_data', 'custom_description_custom_field' );
function custom_description_custom_field() {

    $args = array(
        'id' => 'order_description',
        'label' => __( 'Order Description', '$text_domain' ),
        'class' => 'product-description-field',
        'desc_tip' => true,
        'description' => __( 'Custom Order Description Shown on PayPal, Credit Card, reciept, checkout & order email.', '$text_domain' ),
    );

    woocommerce_wp_text_input( $args );
}

add_action( 'woocommerce_process_product_meta', 'save_custom_description_custom_field' );
function save_custom_description_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title   = isset( $_POST['order_description'] ) ? $_POST['order_description'] : '';

    $product->update_meta_data( 'order_description', sanitize_text_field( $title ) );
    $product->save();
}

2

Answers


  1. This is an example in the template:

    // Optional: get the WC_product Object instance from the post ID
    $product_id = $post->ID; // or get_the_id()
    $product    = wc_get_product( $product_id );
    
    // The product short description (for testing)
    $new_short_description = "<strong>Here</strong> is is the product short 
    description content."
    
    $product->set_short_description( $new_short_description ); 
    // Add/Set the new short description
    $product->save(); // Store changes in database
    
    Login or Signup to reply.
  2. I have improved the existing code of your addition, changing the text input field with a text area, and changing the hook of your last function:

    add_action( 'woocommerce_product_options_general_product_data', 'custom_description_custom_field' );
    function custom_description_custom_field() {
    
        $args = array(
            'id' => 'item_description',
            'label' => __( 'Custom description', '$text_domain' ),
            'class' => 'product-description-field',
            'desc_tip' => true,
            'description' => __( 'Custom Order Description Shown on PayPal, Credit Card, reciept, checkout & order email.', '$text_domain' ),
    
        );
    
        woocommerce_wp_text_input( $args );
    
    }
    
    add_action( 'woocommerce_admin_process_product_object', 'save_custom_description_custom_field' );
    function save_custom_description_custom_field( $product ) {
        $title = isset( $_POST['order_description'] ) ? $_POST['item_description'] : '';
        $product->update_meta_data( 'item_description', sanitize_text_field( $title ) );
    }
    

    Now we need, first to display it on the checkout page (as required):

    // Checkout: Display "Item description" custom field
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'checkout_custom_field_item_description_display', 10, 2 );
    function checkout_custom_field_item_description_display( $item_qty, $cart_item ) {
        if( $item_description = $cart_item['data']->get_meta('item_description') ) {
            $text_domain = 'woocommerce';
            $item_qty .= sprintf('<br>
            <div class="item-description"><span>%s</span><br> 
            <small>%s</small></div>', __('Description:', $text_domain), $item_description );
        }
        return $item_qty;
    }
    

    Then save that "Item description" custom field as order item metadata, allowing to display it on all orders and email notifications:

    // Save "Item description" custom field value as order item metadata and display on orders / emails
    add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_metadata', 10, 4 );
    function save_custom_fields_as_order_item_metadata( $item, $cart_item_key, $values, $order ) {
        if( $item_description = $values['data']->get_meta('item_description') ) {
            $item->add_meta_data('item_description', $item_description);
        }
    }
    
    // Have a readable "meta key" label name replacement
    add_filter( 'woocommerce_order_item_display_meta_key', 'filter_order_item_display_meta_key', 10, 3 );
    function filter_order_item_display_meta_key( $display_key, $meta, $item ) {
        if ( $display_key === 'item_description' ) {
            $text_domain = 'woocommerce';
            $display_key = __('Description', $text_domain);
        }
        return $display_key;
    }
    

    Tested and work for checkout, orders and email notifications.

    Remaining (untested): You need to test that on PayPal and CC receipts. If it doesn’t work for those, you should ask a new question, specifically for PayPal and CC receipts.

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