How to add gift message to checkout form and admin order detail?


I’m trying to add a gift message to my orders.

So I created a textarea & a custom field that I hooked before paiement selection :

// texte cadeau 
// on commence par créer la zone de texte
add_action('woocommerce_review_order_before_payment', 'add_gift_message');

function add_gift_message($checkout) {
    echo '<div id="gift-area"><h5>' . __('C'est un cadeau ?','lilamand') . '</h5>';
    woocommerce_form_field('gift_message', array(
                                            'type' => 'textarea',
                                            'class' => array(
                                            ) ,
                            'placeholder' => __('Laissez-nous un message personnalisé que nous glisserons dans votre colis (optionnel et gratuit).','lilamand') ,
                            ) ,
    echo '</div>';

Then I saved the content to my new custom field :

// on sauvegarde le message
add_action('woocommerce_checkout_update_order_meta', 'add_gift_message_meta_update');
function add_gift_message_meta_update($order_id) {
    if (!empty($_POST['gift_message'])) {
        update_post_meta($order_id, 'gift_message',sanitize_text_field($_POST['gift_message']));

add_action( 'woocommerce_admin_order_data_after_order_details', 'add_gift_message_display_in_order', 20 );

And then i’m displaying it in the order detail :

// on l'affiche dans la commande 
function add_gift_message_display_in_order( $order ){ 
    $message = get_post_meta( $order->get_id() , '_gift_message', true );
    //if($message && !empty($message)) {
        echo '<div style="clear:both;"></div>';
        echo '<div style="margin:10px;">';
        echo '<h2>'. _e( 'Message a mettre dans le colis','lilamand' ) .'</h2>';
        echo '<p>'. $message .'</p></div>';

Perhaps, I don’t know what is wrong, but on order side, there is no message displayed.
Wich step is wrong ?

thanks in advance for your help

Gregory 2 months 0 Answers 15 views 0

Leave an answer