Last active
September 29, 2017 08:54
-
-
Save bluvertigo/2689754ecb96f3ee38f9 to your computer and use it in GitHub Desktop.
Add Costum Field to Checkout / Aggiungere un campo personalizzato al checkout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add the field to the checkout | |
*/ | |
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' ); | |
function my_custom_checkout_field( $checkout ) { | |
echo '<div id="my_custom_checkout_field"><h2>' . __('Codice Fiscale / Partita Iva') . '</h2>'; | |
woocommerce_form_field( 'p_iva', array( | |
'type' => 'text', | |
'class' => array('my-field-class form-row-wide'), | |
'label' => __('Partita Iva o Codice Fiscale *'), | |
'placeholder' => __('Partita Iva o Codice Fiscale'), | |
), $checkout->get_value( 'my_field_name' )); | |
echo '</div>'; | |
} | |
/** | |
* Process the checkout | |
*/ | |
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); | |
function my_custom_checkout_field_process() { | |
// Check if set, if its not set add an error. | |
if ( ! $_POST['p_iva'] ) | |
wc_add_notice( 'P.IVA ' . __( 'is a required field.', 'woocommerce' ), 'error' ); | |
} | |
/** | |
* Update the order meta with field value | |
*/ | |
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' ); | |
function my_custom_checkout_field_update_order_meta( $order_id ) { | |
if ( ! empty( $_POST['p_iva'] ) ) { | |
update_post_meta( $order_id, 'P.IVA', sanitize_text_field( $_POST['p_iva'] ) ); | |
} | |
} | |
/** | |
* Display field value on the order edit page | |
*/ | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); | |
function my_custom_checkout_field_display_admin_order_meta($order){ | |
echo '<p><strong>'.__('P.IVA').':</strong> ' . get_post_meta( $order->id, 'P.IVA', true ) . '</p>'; | |
} | |
/** * Add the field to order emails **/ | |
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys'); | |
function my_custom_checkout_field_order_meta_keys( $keys ) { | |
$keys[] = 'P.IVA'; | |
return $keys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment