programing

Woocommerce에서 체크아웃 필드를 생성하는 제품 수량에 따른 ACF 리피터

luckcodes 2023. 2. 11. 23:58

Woocommerce에서 체크아웃 필드를 생성하는 제품 수량에 따른 ACF 리피터

woocommerce-advanced-checkout-fields 플러그인을 사용하고 있으며 다음과 같이 청구 섹션에 리피터 필드를 추가했습니다.

여기에 이미지 설명 입력

위 이미지에서 볼 수 있듯이 리피터 필드 "이름/이메일"은 제품 "벨트"에 적용됩니다.

지금 매장에서 제품을 구입하여 아래와 같은 수량 3을 만들면 리피터 필드가 3번 표시되며 모든 것이 정상입니다.

여기에 이미지 설명 입력

이제 주문을 하면 랜딩 페이지에 다음과 같이 입력한 값이 표시되지 않습니다.

여기에 이미지 설명 입력

또한 Order admin 섹션에는 다음과 같이 값이 표시되지 않습니다.

여기에 이미지 설명 입력

나는 내가 그 문제를 분명히 상세하게 설명했다고 생각한다.이 문제를 해결하려면 당신의 제안이 필요합니다.

당신이 현상금을 지급한 이후로 아무런 대답도 하지 않았으니까.사용하고 있는 설정이 무엇인지, 주문에 관한 추가 필드에 관한 데이터베이스 관련 포스트 메타 데이터(등록 메타 키)가 무엇인지 모르는 한 누구도 문제를 해결할 수 없습니다.

플러그인이 없는 특정 제품의 아이템 수량에 따라 특정 커스텀 체크아웃 청구 필드에 대한 리피터를 가져오려면:

1) 제품 추가 설정(이 기능을 활성화하기 위한 체크박스):

// Display a custom setting option on product edit pages
add_action('woocommerce_product_options_general_product_data', 'add_product_repeater_checkbox_option');
function add_product_repeater_checkbox_option(){
    echo '<div class="product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'          => '_qty_repeater',
        'label'       => __('Qty repeater', 'woocommerce'),
        'description' => __('Enable quantity repeater for additional "Name" and "Email" billing checkout fields', 'woocommerce'),
        'desc_tip'    => 'true'
    ));

    echo '</div>';
}

// Save the custom setting option value from product edit pages
add_action( 'woocommerce_admin_process_product_object', 'save_product_repeater_checkbox_option', 100, 1 );
function save_product_repeater_checkbox_option( $product ) {
    $qty_repeater = isset( $_POST['_qty_repeater'] ) ? 'yes' : 'no';
    $product->update_meta_data( '_qty_repeater', $qty_repeater );
}

여기에 이미지 설명 입력

2) 체크아웃 반복되는 추가 필드 추가/저장(주문 표시) :

add_filter('woocommerce_billing_fields', 'additional_billing_checkout_fields', 50, 1 );
function additional_billing_checkout_fields( $billing_fields ) {
    foreach(WC()->cart->get_cart() as $cart_item ){
        // Check if the "Quanty repeater option is set for the current item
        if( $cart_item['data']->get_meta('_qty_repeater') === 'yes' && is_checkout() && $cart_item['quantity'] > 1 ) {

            // Quantity repeater
            for( $i = 1, $j = 2; $i < $cart_item['quantity']; $i++, $j++ ){

                // Name fields
                $billing_fields['billing_name_person'.$j] = array(
                    'type'        => 'text',
                    'label'       => __("Name", "woocommerce") . ' ' . $j,
                    'class'       => array('form-row-first'),
                    'required'    => true,
                    'clear'       => false,
                );

                // Email fields
                $billing_fields['billing_email_person'.$j] = array(
                    'type'        => 'email',
                    'label'       => __("Email", "woocommerce") . ' ' . $j,
                    'class'       => array('form-row-last'),
                    'required'    => true,
                    'clear'       => true,
                );
            }
            break; // Only for one item
        }
    }
    return $billing_fields;
}

// Mark Order as having this additional fields data values
add_action('woocommerce_checkout_create_order', 'save_additional_billing_checkout_fields', 20, 2);
function save_additional_billing_checkout_fields( $order, $data ) {
    foreach( $order->get_items() as $item ){
        $product = $item->get_product();
        // Mark the order as containing additional fields
        if( $product->get_meta('_qty_repeater') === 'yes' && $item->get_quantity() > 1 ) {
            $item->update_meta_data( '_qty_repeater', '1' );
            break; // Stop the loop
        }
    }
}

여기에 이미지 설명 입력

3) 추가 청구 필드 관련 데이터(관리 주문, 주문 보기, 이메일):

// Display additional billing fields values
add_action('woocommerce_order_details_after_order_table', 'display_additional_billing_fields_values' ); // Order received and view
add_action( 'woocommerce_email_after_order_table', 'display_additional_billing_fields_values' ); // Email notifications
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_additional_billing_fields_values' ); // Admin edit Order
function display_additional_billing_fields_values( $order ) {

    if( $order->get_meta('_qty_repeater') ) {
        // Only for email notifications
        if( ! ( is_wc_endpoint_url() || is_checkout() || is_admin() ) ){
            echo '<style>
            table.customer-details {width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373; border: 1px solid #e4e4e4; margin-bottom:40px;}
            table.customer-details td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
                padding: 12px; padding-bottom: 4px;}
            </style>';
        }
        // Others
        else {
            echo '<style> table.customer-details, table.customer-details td { border: none; } </style>';
        }

        echo '<h2>' . __( 'Customer details', 'woocommerce' ) . '</h2>';
        echo '<div><table class="customer-details" cellspacing="0">';

        // Loop through order items
        foreach( $order->get_items() as $item ){
            $product = $item->get_product();
            if( $product->get_meta('_qty_repeater') === 'yes' ) {
                // Loop through item quantity
                for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
                    // Name
                    echo '<tr><td><strong>' . __("Name", "woocommerce") . ' ' . $j;
                    echo ': </strong>' . $order->get_meta('_billing_name_person'.$j) . '</td>';
                    // Email
                    echo '<td><strong>' . __("Email", "woocommerce") . ' ' . $j;
                    echo ': </strong>' . $order->get_meta('_billing_email_person'.$j) . '</td></tr>';
                }
                break;
            }
        }
        echo '</table></div>';
    }
}

여기에 이미지 설명 입력

4) 추가 과금 필드 편집 가능하게 합니다(관리자):

add_filter( 'woocommerce_admin_billing_fields' , 'additional_admin_editable_billing_fields' );
function additional_admin_editable_billing_fields( $fields ) {
    global $pagenow, $post;

    if( $pagenow != 'post.php' ) return $fields;

    $order = wc_get_order($post->ID);

    if( $order->get_meta('_qty_repeater') ) {
        // Loop through order items
        foreach( $order->get_items() as $item ){
            $product = $item->get_product();
            if( $product->get_meta('_qty_repeater') === 'yes' ) {
                // Loop through item quantity
                for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
                    $fields['name_person'.$j] = array(
                        'label'         => __("Name", "woocommerce") . ' ' . $j,
                        'show'          => false,
                        'wrapper_class' => 'first',
                    );
                    $fields['email_person'.$j] = array(
                        'label'         => __("Email", "woocommerce") . ' ' . $j,
                        'show'          => false,
                        'wrapper_class' => 'last',
                    );
                }
                break;
            }
        }
    }
    return $fields;
}

여기에 이미지 설명 입력

코드가 기능합니다.php 파일에는 액티브한 아이 테마(또는 활성 테마).테스트 및 동작.

언급URL : https://stackoverflow.com/questions/52731705/acf-repeater-based-on-product-quantity-generating-checkout-fields-in-woocommerce