Aggiungere un nuovo campo note ordine nel checkout di woocommerce

Pubblicato da: Roberto Commenti: 0

Questa alternativa è valida anche per aggiungere un campo personalizzato nel proprio checkout di woocommerce. Nell’ esempio qui di seguito, stiamo ricreando per interno la funzione di un nuovo campo chiamato “Campo Note”, che già esiste nativamente in woocommerce, ma che in realtà ne vorremmo un secondo.

 


/** * @snippet Creazione nuovo campo note in checkout */
add_action( 'woocommerce_before_order_notes', 'aggiungi_nuovo_campo_note_checkout' );

function aggiungi_nuovo_campo_note_checkout( $checkout ) {
$current_user = wp_get_current_user();
$saved_new_note_checkout = $current_user->new_note_checkout;
woocommerce_form_field( 'new_note_checkout', array(
'type' => 'textarea',
'class' => array( 'form-row-wide' ),
'label' => __('Note Ordine 2', 'woocommerce'),
'placeholder' => '',
'required' => true,
'default' => $saved_new_note_checkout,
), $checkout->get_value( 'new_note_checkout' ) );
}

/**
* @snippet Salva nuovo campo note in checkout
*/

add_action( 'woocommerce_checkout_update_order_meta', 'salva_nuovo_campo_note_in_checkout' );

function salva_nuovo_campo_note_in_checkout( $order_id ) {
if ( $_POST['new_note_checkout'] ) update_post_meta( $order_id, '_new_note_checkout', esc_attr( $_POST['new_note_checkout'] ) );
}

/** * @snippet Visualizza testo dopo conferma pagamento */
add_action('woocommerce_order_details_after_order_table','visualizza_testo_nuovo_campo_dopo_checkout_se_compilato',20);
function visualizza_testo_nuovo_campo_dopo_checkout_se_compilato( $order ){

if ( get_post_meta( $order->get_id(), '_new_note_checkout', true ) ) echo '<p><strong>Note:</strong> ' . get_post_meta( $order->get_id(), '_new_note_checkout', true ) . '</p>';
}

/** * @snippet Visualizza testo in admin nell'ordine sotto i campi fatturazione del cliente */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'visualizza_nuova_nota_checkout_in_ordine_admin', 10, 1 );

function visualizza_nuova_nota_checkout_in_ordine_admin( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_new_note_checkout', true ) ) echo '<p><strong>Note:</strong> ' . get_post_meta( $order_id, '_new_note_checkout', true ) . '</p>';
}

/** * @snippet Visualizza testo nell'email  */
add_action( 'woocommerce_email_after_order_table', 'visualizza_nuova_nota_checkout_in_email', 20, 4 );

function visualizza_nuova_nota_checkout_in_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_new_note_checkout', true ) ) echo '<p><strong>Note:</strong> ' . get_post_meta( $order->get_id(), '_new_note_checkout', true ) . '</p>';
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.