Reputation: 345
I am not a developer but somehow managed to add Woocommerce custom fields to checkout and order edit pages. There are similar questions, but I can't find the correct solution.
Some custom fields are visible in the admin order edit page but they don't display the values and are not added to order emails.
What am I missing?
Please see screenshot at the end.
<?php
/**
* DOMAINE
*/
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_domaine' );
function my_custom_checkout_field_domaine( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>' . __('Votre question') . '</h3>';
woocommerce_form_field( 'domaine', array(
'type' => 'select',
'class' => array('domaine form-row-wide'),
'label' => __('Dans quel domaine se situe votre question ?'),
'required' => true,
'options' => array(
'famille' => __('Famille'),
'immobilier' => __('Immobilier'),
'vie_pro' => __('Vie professionnelle'),
'fiscalité' => __('Fiscalité'),
'droit_des_societes' => __('Droit des sociétés'),
'autre' => __('Autre')
),
'default' => 'famille'),
$checkout->get_value( 'domaine' ));
echo '</div>';
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process_domaine');
function my_custom_checkout_field_process_domaine() {
// Check if set, if its not set add an error.
if ( ! $_POST['domaine'] )
wc_add_notice( __( 'Merci de choisir le domaine de votre question.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_domaine' );
function my_custom_checkout_field_update_order_meta_domaine( $order_id ) {
if ( ! empty( $_POST['questidomaineon'] ) ) {
update_post_meta( $order_id, 'domaine', sanitize_text_field( $_POST['domaine'] ) );
}
}
/**
* 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_domaine', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta_domaine($order){
echo '<p><strong>'.__('domaine').':</strong> ' . get_post_meta( $order->id, 'domaine', true ) . '</p>';
}
/**
* END DOMAINE
*/
/**
* QUESTION
*/
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_question' );
function my_custom_checkout_field_question( $checkout ) {
woocommerce_form_field( 'question', array(
'type' => 'textarea',
'class' => array('my-field-class form-row-wide'),
'required' => true,
'label' => __('Merci de décrire le plus précisément possible votre demande et vos questions'),
'placeholder' => __('...'),
), $checkout->get_value( 'question' ));
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process_question');
function my_custom_checkout_field_process_question() {
// Check if set, if its not set add an error.
if ( ! $_POST['question'] )
wc_add_notice( __( 'Merci de poser votre question.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_question' );
function my_custom_checkout_field_update_order_meta_question( $order_id ) {
if ( ! empty( $_POST['question'] ) ) {
update_post_meta( $order_id, 'Question', sanitize_text_field( $_POST['question'] ) );
}
}
/**
* 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_question', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta_question($order){
echo '<p><strong>'.__('question').':</strong> ' . get_post_meta( $order->id, 'question', true ) . '</p>';
}
/**
* END QUESTION
*/
/**
* RECHERCHES
*/
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_recherches' );
function my_custom_checkout_field_recherches( $checkout ) {
woocommerce_form_field( 'recherches', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Avez-vous déjà effectué des recherches ?'),
'required' => true,
'options' => array(
'oui' => __('Oui'),
'non' => __('Non'),
),
), $checkout->get_value( 'recherches' ));
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process_recherches');
function my_custom_checkout_field_process_recherches() {
// Check if set, if its not set add an error.
if ( ! $_POST['recherches'] )
wc_add_notice( __( 'Merci de renseigner si vous avez déjà effectué des recherches.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_recherches' );
function my_custom_checkout_field_update_order_meta_recherches( $order_id ) {
if ( ! empty( $_POST['recherches'] ) ) {
update_post_meta( $order_id, 'recherches', sanitize_text_field( $_POST['recherches'] ) );
}
}
/**
* 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_recherches', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta_recherches($order){
echo '<p><strong>'.__('recherches').':</strong> ' . get_post_meta( $order->id, 'recherches', true ) . '</p>';
}
/**
* END RECHERCHES
*/
/**
* PROFESSIONNEL
*/
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_professionnel' );
function my_custom_checkout_field_professionnel( $checkout ) {
woocommerce_form_field( 'professionnel', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Avez-vous déjà consulté un professionnel du droit ou de la réglementation ?'),
'required' => true,
'options' => array(
'oui' => __('Oui'),
'non' => __('Non'),
),
), $checkout->get_value( 'professionnel' ));
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process_professionnel');
function my_custom_checkout_field_process_professionnel() {
// Check if set, if its not set add an error.
if ( ! $_POST['professionnel'] )
wc_add_notice( __( 'Merci de renseigner si vous avez déjà contacté un professionnel.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_professionnel' );
function my_custom_checkout_field_update_order_meta_professionnel( $order_id ) {
if ( ! empty( $_POST['professionnel'] ) ) {
update_post_meta( $order_id, 'professionnel', sanitize_text_field( $_POST['professionnel'] ) );
}
}
/**
* 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_professionnel', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta_professionnel($order){
echo '<p><strong>'.__('Contact avec un professionnel').':</strong> ' . get_post_meta( $order->id, 'professionnel', true ) . '</p>';
}
/**
* END PROFESSIONNEL
*/
/**
* STATUT
*/
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_statut' );
function my_custom_checkout_field_statut( $checkout ) {
woocommerce_form_field( 'statut', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Vous êtes ?'),
'required' => true,
'options' => array(
'particulier' => __('Particulier'),
'entreprise' => __('Entreprise'),
'collectivite' => __('Collectivité')
),
), $checkout->get_value( 'statut' ));
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process_statut');
function my_custom_checkout_field_process_statut() {
// Check if set, if its not set add an error.
if ( ! $_POST['statut'] )
wc_add_notice( __( 'Merci de renseigner votre statut.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_statut' );
function my_custom_checkout_field_update_order_meta_statut( $order_id ) {
if ( ! empty( $_POST['statut'] ) ) {
update_post_meta( $order_id, 'statut', sanitize_text_field( $_POST['statut'] ) );
}
}
/**
* 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_statut', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta_statut($order){
echo '<p><strong>'.__('statut').':</strong> ' . get_post_meta( $order->id, 'statut', true ) . '</p>';
}
/**
* END STATUT
*/
Upvotes: 2
Views: 308
Reputation: 29624
First and foremost, there is no need to use the same hook over and over for each additional field
Here and there you also have typos, uppercase where you otherwise use a lowercase letter or a different name
/**
* Add fields to the checkout
*/
function my_custom_checkout_fields( $checkout ) {
// domaine
woocommerce_form_field( 'domaine', array(
'type' => 'select',
'class' => array('domaine form-row-wide'),
'label' => __('Dans quel domaine se situe votre question ?'),
'required' => true,
'options' => array(
'famille' => __('Famille'),
'immobilier' => __('Immobilier'),
'vie_pro' => __('Vie professionnelle'),
'fiscalité' => __('Fiscalité'),
'droit_des_societes' => __('Droit des sociétés'),
'autre' => __('Autre')
),
'default' => 'famille'
), $checkout->get_value( 'domaine' ));
// question
woocommerce_form_field( 'question', array(
'type' => 'textarea',
'class' => array('my-field-class form-row-wide'),
'required' => true,
'label' => __('Merci de décrire le plus précisément possible votre demande et vos questions'),
'placeholder' => __('...'),
), $checkout->get_value( 'question' ));
// recherches
woocommerce_form_field( 'recherches', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Avez-vous déjà effectué des recherches ?'),
'required' => true,
'options' => array(
'oui' => __('Oui'),
'non' => __('Non'),
),
), $checkout->get_value( 'recherches' ));
// professionnel
woocommerce_form_field( 'professionnel', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Avez-vous déjà consulté un professionnel du droit ou de la réglementation ?'),
'required' => true,
'options' => array(
'oui' => __('Oui'),
'non' => __('Non'),
),
), $checkout->get_value( 'professionnel' ));
// statut
woocommerce_form_field( 'statut', array(
'type' => 'radio',
'class' => array('recherche-field form-row-wide'),
'label' => __('Vous êtes ?'),
'required' => true,
'options' => array(
'particulier' => __('Particulier'),
'entreprise' => __('Entreprise'),
'collectivite' => __('Collectivité')
),
), $checkout->get_value( 'statut' ));
}
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_fields', 10, 1 );
/**
* Process the checkout
*/
function my_custom_checkout_field_process() {
// Validation, 'domaine' is not added because it has a default value
// question
if ( empty( $_POST['question'] ) ) {
wc_add_notice( __( 'Merci de poser votre question.' ), 'error' );
}
// recherches
if ( empty( $_POST['recherches'] ) ) {
wc_add_notice( __( 'Merci de renseigner si vous avez déjà effectué des recherches.' ), 'error' );
}
// professionnel
if ( empty( $_POST['professionnel'] ) ) {
wc_add_notice( __( 'Merci de renseigner si vous avez déjà contacté un professionnel.' ), 'error' );
}
// statut
if ( empty ( $_POST['statut'] ) ) {
wc_add_notice( __( 'Merci de renseigner votre statut.' ), 'error' );
}
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process' );
/**
* Update the order meta with field value
*/
function my_custom_checkout_field_update_order_meta_domaine( $order_id ) {
if ( ! empty( $_POST['domaine'] ) ) {
update_post_meta( $order_id, 'domaine', sanitize_text_field( $_POST['domaine'] ) );
}
if ( ! empty( $_POST['question'] ) ) {
update_post_meta( $order_id, 'question', sanitize_text_field( $_POST['question'] ) );
}
if ( ! empty( $_POST['recherches'] ) ) {
update_post_meta( $order_id, 'recherches', sanitize_text_field( $_POST['recherches'] ) );
}
if ( ! empty( $_POST['professionnel'] ) ) {
update_post_meta( $order_id, 'professionnel', sanitize_text_field( $_POST['professionnel'] ) );
}
if ( ! empty( $_POST['statut'] ) ) {
update_post_meta( $order_id, 'statut', sanitize_text_field( $_POST['statut'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_domaine', 10, 1 );
/**
* Display field value on the order edit page
*/
function my_custom_checkout_field_display_admin_order_meta_domaine( $order ) {
// Get order id
$order_id = $order->get_id();
echo '<p><strong>'.__('domaine').':</strong> ' . get_post_meta( $order_id, 'domaine', true ) . '</p>';
echo '<p><strong>'.__('question').':</strong> ' . get_post_meta( $order_id, 'question', true ) . '</p>';
echo '<p><strong>'.__('recherches').':</strong> ' . get_post_meta( $order_id, 'recherches', true ) . '</p>';
echo '<p><strong>'.__('Contact avec un professionnel').':</strong> ' . get_post_meta( $order_id, 'professionnel', true ) . '</p>';
echo '<p><strong>'.__('statut').':</strong> ' . get_post_meta( $order_id, 'statut', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta_domaine', 10, 1 );
Additional: To avoid adding an
if
statement every time, you can use aforeach
loop instead
So this
/**
* Update the order meta with field value
*/
function my_custom_checkout_field_update_order_meta_domaine( $order_id ) {
if ( ! empty( $_POST['domaine'] ) ) {
update_post_meta( $order_id, 'domaine', sanitize_text_field( $_POST['domaine'] ) );
}
if ( ! empty( $_POST['question'] ) ) {
update_post_meta( $order_id, 'question', sanitize_text_field( $_POST['question'] ) );
}
if ( ! empty( $_POST['recherches'] ) ) {
update_post_meta( $order_id, 'recherches', sanitize_text_field( $_POST['recherches'] ) );
}
if ( ! empty( $_POST['professionnel'] ) ) {
update_post_meta( $order_id, 'professionnel', sanitize_text_field( $_POST['professionnel'] ) );
}
if ( ! empty( $_POST['statut'] ) ) {
update_post_meta( $order_id, 'statut', sanitize_text_field( $_POST['statut'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_domaine', 10, 1 );
Is therefore also possible in this way
/**
* Update the order meta with field value
*/
function my_custom_checkout_field_update_order_meta_domaine( $order_id ) {
// Add fields
$fields = array('domaine', 'question', 'recherches', 'professionnel', 'statut');
// Loop
foreach ( $fields as $field ) {
$field_post = $_POST[$field];
if ( ! empty( $field_post ) ) {
update_post_meta( $order_id, $field, sanitize_text_field( $field_post ) );
}
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_domaine', 10, 1 );
Upvotes: 2