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
This is an example in the template:
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:
Now we need, first to display it on the checkout page (as required):
Then save that "Item description" custom field as order item metadata, allowing to display it on all orders and email notifications:
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.