Woocommerce (3.8.0) 관리자 이메일에 체크 아웃 페이지의 사용자 정의 필드 데이터를 포함 시키십시오.

데이비드 리

woocommerce 결제 페이지에 몇 가지 사용자 정의 필드를 만들었습니다. 내 코드가 정확하고 필드가 제대로 표시됨 필드 데이터를 저장하고 관리자 패널에 표시했습니다. 내 코드가 정확하고 필드가 제대로 표시됩니다.

새 제품을 주문할 때마다 관리자 이메일에이 데이터를 포함하는 코드를 작성했습니다. 내 코드가 정확하지 않고 정보가 이메일에 표시되지 않습니다.

이 주제와 관련된 다른 모든 stackoverflow 답변은 더 이상 사용되지 않는 필터에 의존 woocommerce_email_order_meta_keys합니다. woocommerce 3.8.0 woocommerce_email_order_meta_fields필터로 답변하는 stackoverflow는 없습니다 .

woocommerce 3.8.0 및 wordpress 5.3을 실행하고 있습니다. 이 파일을 wp-content / themes / child-theme / functions.php에 저장하고 있습니다.

내 코드에 WTF가 잘못 되었나요? 나는 그것을 몇 번이고 확인했고 무엇이 잘못되었는지 알아낼 수 없습니다. 누군가 내가 뭘 잘못하고 있는지 말해 줄 수 있습니까? 저는 PHP와 워드 프레스를 가르치려는 루비 온 레일스 개발자입니다.

add_filter('woocommerce_email_order_meta_fields','custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['custom_field_1'] = array(
        'label' => __( 'custom_field_1' ),
        'value' => get_post_meta( $order->id, 'custom_field_1', true ),
    );
    $fields['custom_field_2'] = array(
        'label' => __( 'custom_field_2' ),
        'value' => get_post_meta( $order->id, 'custom_field_2', true ),
    );
    $fields['custom_field_3'] = array(
        'label' => __( 'custom_field_3' ),
        'value' => get_post_meta( $order->id, 'custom_field_3', true ),
    );
    $fields['custom_field_4'] = array(
        'label' => __( 'custom_field_4' ),
        'value' => get_post_meta( $order->id, 'custom_field_4', true ),
    );
    return $fields;
}

woocommerce 결제 페이지의 내 사용자 정의 양식 필드는 여기에 있습니다

add_filter( 'woocommerce_checkout_fields', 'isca_custom_checkout_fields' );`
function isca_custom_checkout_fields($fields){
    $fields['isca_extra_fields'] = array(
            'custom_field_1' => array(
                'class' => array(
                    'form-row-first'
                  ),
                'type' => 'text',
                'required'      => true,
                'placeholder' => __( 'Name' )
                ),
            'custom_field_2' => array(
                'class' => array(
                    'form-row-last'
                  ),
                'type' => 'text',
                'required'      => true,
                'placeholder' => __( 'Nickname' )
                ),   
            'custom_field_3' => array(
                'class' => array(
                    'form-row-first'
                  ),
                'type' => 'text',
                'required'      => true,
                'placeholder' => __( 'Favorite Exercise' )
                ),  
            'custom_field_4' => array(
                'class' => array(
                    'form-row-last'
                  ),
                'type' => 'text',
                'required'      => false,
                'placeholder' => __( 'Favorite Stretch' )
                ),   
            );
    return $fields;
}

그런 다음이 코드를 사용하여 woocomerce 결제 페이지에 추가합니다.

add_action( 'woocommerce_after_checkout_billing_form' ,'isca_extra_checkout_fields' );
function isca_extra_checkout_fields(){
    $checkout = WC()->checkout(); ?>
    <br/>
    <div class="extra-fields">
    <h3><?php _e( 'Fitness Information' ); ?></h3>
    <?php
       foreach ( $checkout->checkout_fields['isca_extra_fields'] as $key => $field ) : ?>
            <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
        <?php endforeach; ?>
    </div>
<?php }

멜빈

Wordpress에는 오류와 경고를 식별하고 해결할 수 있도록 디버그 모드를 전환하는 옵션이 있습니다. 코드에서 오류가 발생하는 동안 오류는 wordpress 폴더 debug.logwp-content폴더에 이름이 지정된 파일에 기록됩니다 . 따라서 워드 프레스에서 개발하는 동안 세 가지 옵션을 설정해야합니다. wp-config.php로 이동하여이 세 줄의 코드를 추가하십시오.

define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );

귀하 custom_woocommerce_email_order_meta_field의 기능에 오류가 있습니다. 주문 ID를 잘못 호출했습니다. 오류 로그에이를 표시합니다. 주문 속성을 직접 호출하면 안됩니다. 그래서 당신은 변화가 $order->id$order->get_id(). 따라서 기능을

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    if( !$sent_to_admin ){
        return;
    }
    $fields['custom_field_1'] = array(
        'label' => __( 'custom field 1' ),
        'value' => get_post_meta( $order->get_id(), 'custom_field_1', true ),
    );
    $fields['custom_field_2'] = array(
        'label' => __( 'custom field 2' ),
        'value' => get_post_meta( $order->get_id(), 'custom_field_2', true ),
    );
    $fields['custom_field_3'] = array(
        'label' => __( 'custom field 3' ),
        'value' => get_post_meta( $order->get_id(), 'custom_field_3', true ),
    );
    $fields['custom_field_4'] = array(
        'label' => __( 'custom field 4' ),
        'value' => get_post_meta( $order->get_id(), 'custom_field_4', true ),
    );
    return $fields;
}

또한 체크 아웃 페이지에 추가 한 사용자 정의 필드를 저장하는 코드를 작성하지 않았습니다. 주문 메타에 저장되지 않은 값을 찾으려고합니다. 다음과 같이 메타를 주문하려면 사용자 정의 추가 체크 아웃 필드를 저장하는 코드를 작성해야합니다.

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 ($_POST['custom_field_1']) update_post_meta( $order_id, 'custom_field_1', esc_attr($_POST['custom_field_1']));
    if ($_POST['custom_field_2']) update_post_meta( $order_id, 'custom_field_2', esc_attr($_POST['custom_field_2']));
    if ($_POST['custom_field_3']) update_post_meta( $order_id, 'custom_field_3', esc_attr($_POST['custom_field_3']));
    if ($_POST['custom_field_4']) update_post_meta( $order_id, 'custom_field_4', esc_attr($_POST['custom_field_4']));
}

완료. . . 이제 모든 것이 이메일에 완벽하게 추가됩니다 (테스트 및 확인). 또한이 필드가 관리자 이메일에 표시된다고 말 했으므로 다음을 사용하여 해당 조건을 확인해야합니다.

if( !$sent_to_admin ){
   return;
}

내가 custom_woocommerce_email_order_meta_fields함수에 추가했습니다 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관