[php]
/***********************************************************************
* WooCommerce Checkout custom Fields
* Add Contact person to Checkout Page
***********************************************************************
*/
add_action( ‚woocommerce_after_order_notes‘, ‚fair_contact_person_field‘ );
function fair_contact_person_field( $checkout ) {
$contact_person = get_current_user_contact_person( get_current_user_id() );
echo ‚
‚ . __(‚Contact Person for Fair‘) . ‚
‚;
woocommerce_form_field( ‚fair_contact_person‘, array(
‚type‘ => ‚text‘,
‚class‘ => array(‚my-field-class form-row-wide‘),
‚label‘ => __(‚Fill in this field‘),
‚placeholder‘ => __(‚Please enter contact details of person, responsible for this fair‘),
‚default‘ => $contact_person,
), $checkout->get_value( ‚fair_contact_person‘ ));
echo ‚
‚;
}
function get_current_user_contact_person( $user_id ) {
$keys = array(
‚anrede‘ => ‚aus_kontakt_anrede‘,
‚titel‘ => ‚aus_kontakt_titel‘,
‚vorname‘ => ‚first_name‘,
’nachname‘ => ‚last_name‘,
‚telefon‘ => ‚aus_kontakt_telefon‘,
‚email‘ => ‚aus_kontakt_email‘,
);
$i = 0;
foreach ( $keys as $key ) {
$contact_person_details[$i] = get_user_meta($user_id, $key, true);
$i++;
}
$contact_person = ‚Name: ‚ . $contact_person_details[0] . ‚ ‚ . $contact_person_details[1] . ‚ ‚ . $contact_person_details[2] . ‚ ‚ . $contact_person_details[3] . ‚ Telefon: ‚ . $contact_person_details[4] . ‚ E-Mail: ‚ . $contact_person_details[5];
return $contact_person;
}
/**
* Process the checkout
*/
add_action(‚woocommerce_checkout_process‘, ‚fair_contact_person_field_process‘);
function fair_contact_person_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST[‚fair_contact_person‘] )
wc_add_notice( __( ‚Please provide a contact person for organisational purposes.‘ ), ‚error‘ );
}
/**
* Update the order meta with field value
*/
add_action( ‚woocommerce_checkout_update_order_meta‘, ‚fair_contact_person_field_update_order_meta‘ );
function fair_contact_person_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST[‚fair_contact_person‘] ) ) {
update_post_meta( $order_id, ‚My Field‘, sanitize_text_field( $_POST[‚fair_contact_person‘] ) );
}
}
/**
* 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 ‚
‚.__(‚My Field‘).‘: ‚ . get_post_meta( $order->id, ‚My Field‘, true ) . ‚
‚;
}
[/php]
Related Articles
- WC Attribute Komma durch Linebreak ersetzen
- WC Produkt Kategorie Name vor Bild in Liste anzeigen
- WooCommerce Abgeschlossene Bestellungen bearbeitbar machen
- WooCommerce Button für Anfrage mit Text-Prefill
- WooCommerce Conditional Email Text (User Role, Product Category)
- Order Comment / Anmerkungsfeld um User Role abhängigen Content erweitern
- Fix Elementor fixed Background sizing
- Cronjob Überlappung verhindern – flock()
- WordPress Update Hinweise deaktivieren
- WP Download Manager "attached file is missing/deleted" Fehler