如何在结帐页面上添加自定义字段并将其显示在woo Commerce中的用户帐户页面中

时间:2018-07-17 12:55:30

标签: woocommerce checkout

我必须在结帐时添加字段,例如id并上传两个文件,如何保存该文件以及如何在用户帐户页面中显示该文件。 让我进一步澄清。 我必须添加PAN ID,PAN PHOTO,结帐页面上的另一张照片,将该字段保存在woo Commerce帐户页面中。当用户完成订单并转到其“我的帐户”页面订单时,我要显示这3个字段。请让我知道如何执行此操作。 1个PAN_ID 2张PAN_PHOTO 3 OTHER_PHOTO

            // Add a new checkout field
            function custom_filter_checkout_fields($fields){
                $fields['extra_fields'] = array(
                        'pal_id' => array(
                            'type' => 'text',
                            'required'      => true,
                            'label' => __( 'PAL ID' )
                            ),
                        'photo_id' => array(
                            'type' => 'file',
                            /*'options' => array( 'a' => __( 'apple' ), 'b' => __( 'bacon' ), 'c' => __( 'chocolate' ) ),*/
                            'required'      => false,
                            'label' => __( 'PHOTO PAL ID' )
                            )
                        );

                return $fields;
            }
            add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields' );

            // display the extra field on the checkout form
            function custom_extra_checkout_fields(){ 

                $checkout = WC()->checkout(); ?>

                <div class="extra-fields">
                <h3><?php _e( 'If you are purchasing firearms, or ammunition the information below is required before we can ship to
            your order' ); ?></h3>

                <?php 
                // because of this foreach, everything added to the array in the previous function will display automagically
                foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

                        <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

                    <?php endforeach; ?>
                </div>

            <?php }
            add_action( 'woocommerce_checkout_after_customer_details' ,'custom_extra_checkout_fields' );

            // save the extra field when checkout is processed
            function custom_save_extra_checkout_fields( $order, $data ){

                // don't forget appropriate sanitization if you are using a different field type
                if( isset( $data['pal_id'] ) ) {
                    $order->update_meta_data( '_pal_id', sanitize_text_field( $data['pal_id'] ) );
                }
                if( isset( $data['photo_id'] ) && in_array( $data['photo_id'], array( 'a', 'b', 'c' ) ) ) {
                    $order->update_meta_data( '_photo_id', $data['photo_id'] );
                } 
            }
            add_action( 'woocommerce_checkout_create_order', 'custom_save_extra_checkout_fields', 10, 2 );


            // display the extra data on order recieved page and my-account order review
            function custom_display_order_data( $order_id ){  
                $order = wc_get_order( $order_id ); ?>
                <h2><?php _e( 'Additional Info' ); ?></h2>
                <table class="shop_table shop_table_responsive additional_info">
                    <tbody>
                        <tr>
                            <th><?php _e( 'PAL ID:' ); ?></th>
                            <td><?php echo $order->get_meta( '_pal_id' ); ?></td>
                        </tr>
                        <tr>
                            <th><?php _e( 'PHOTO PAL ID:' ); ?></th>
                            <td><?php echo $order->get_meta( '_photo_id' ); ?></td>
                        </tr>
                    </tbody>
                </table>
            <?php }
            add_action( 'woocommerce_thankyou', 'custom_display_order_data', 20 );
            add_action( 'woocommerce_view_order', 'custom_display_order_data', 20 );


            // display the extra data in the order admin panel
            function custom_display_order_data_in_admin( $order ){  ?>
                <div class="order_data_column">
                    <h4><?php _e( 'Other Details', 'woocommerce' ); ?></h4>
                    <?php 
                        echo '<p><strong>' . __( 'PAL ID' ) . ':</strong>' . $order->get_meta( '_pal_id' ) . '</p>';
                        echo '<p><strong>' . __( 'PHOTO PAL ' ) . ':</strong>' . $order->get_meta( '_photo_id' ) . '</p>'; ?>
                </div>
            <?php }
            add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_display_order_data_in_admin' );

这是我在本地系统上的粗略代码。我不知道如何上传和保存文件以及如何在我的帐户页面中显示该字段

0 个答案:

没有答案