WooCommerce自定义域:某些值不在订单页面中

我不是一名开发人员,但不知何故设法添加了WooCommerce自定义字段来结账和订购编辑页面。有类似的问题,但我找不到正确的解决方案。

某些自定义域在管理订单编辑页中可见,但它们不会显示值,也不会添加到订单电子邮件中。

我错过了什么?

请参见结尾处的屏幕截图。

<?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 
 */


解决方案

首先,无需为每个附加字段反复使用相同的挂钩

这里和那里还有打字错误、大写,否则使用小写字母或不同的名称


/**
 * 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 );


附加:为避免每次都添加if语句,您可以改用foreach循环

因此

/**
 * 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 );

因此也可以通过这种方式

/**
 * 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 );

相关文章