简体   繁体   English

从注册到 Woocommerce Checkout 获取自定义字段

[英]Get custom Fields from registration to Woocommerce Checkout

Hi i am using a plugin to allow employers to register and post jobs.嗨,我正在使用一个插件来允许雇主注册和发布工作。 In the registration I have created custom fields they are also in the admin and in the Profile (VAT IDs they I need for billing).在注册中,我创建了自定义字段,它们也在管理员和配置文件中(我需要他们用于计费的增值税 ID)。 I can automaticaly fill and save them also update in the Profile or admin back-end.我可以自动填充并保存它们,也可以在配置文件或管理后端更新。 But I have to include them in the billing fields (not so important where maybe after company name) where they are generated into the invoice.但是我必须将它们包含在帐单字段中(可能在公司名称之后并不那么重要),它们会在其中生成到发票中。 I am not a programmer I only know html and css little bit.我不是程序员,我只知道一点点 html 和 css。 I am trying now for 12 hours and no chance to get it there.我现在正在尝试 12 个小时,但没有机会到达那里。 I would also like to make them not-editable from user profile.我还想让它们无法从用户个人资料中编辑。 This are my fields saved in the child-theme functions.php I have done this with the help from Plugin author but he will not help me more because of no support.这是我保存在子主题functions.php中的字段,我是在插件作者的帮助下完成的,但由于没有支持,他不会帮助我更多。 Plugin guide 插件指南

add_action('iwj_employer_form_after_general',function ($job){
$post_id = $job ? $job->get_id() : '';
?>
<?php
   iwj_field_text( '_ico_company', 'IČO spoločnosti*', true, $post_id, null, 'true', '', __( '' ) );
   ?>
<?php
});
add_action('iwj_admin_employer_form_after_general',function ($post_id){

?>
<?php
   iwj_field_text( '_ico_company', 'IČO spoločnosti*', true, $post_id, null, 'true', '', __( '' ) );
   ?>
<?php
});

add_action('save_post', function($post_id){
    if($post_id && get_post_type($post_id) == 'iwj_employer'){
        $custom_field_value = sanitize_text_field($_POST['_ico_company']);
        update_post_meta($post_id, '_ico_company', $custom_field_value);
    }
}, 99);

add_action('iwj_register_process', function($uid){
    $user = IWJ_User::get_user($uid);
    $post_id = 0;
    if($user->is_employer()){
        $emp = $user->get_employer();
        $post_id = $emp->get_id();
    }    
    if($post_id){
        //Add you custom field name process here
        update_post_meta($post_id, '_ico_company', sanitize_text_field($_POST['_ico_company']));

    }
});

I would be mega happy if someone could help me out with this :)如果有人能帮我解决这个问题,我会非常高兴:)

This will help you to have the fields in the checkout.这将帮助您在结帐中拥有字段。 I added a mock function get_vat_field, because I don't know yet how you are going to get the VAT field in the checkout.我添加了一个模拟函数 get_vat_field,因为我还不知道您将如何在结帐中获取增值税字段。

Generally, it adds an additional field to the checkout, and when the checkout is completed, it adds it to the order.通常,它会在结帐中添加一个附加字段,当结帐完成时,它会将其添加到订单中。 Which then is displayed in the order meta of the order.然后显示在订单的订单元中。 You can find that in the order itself in the admin panel (order edit page).您可以在管理面板(订单编辑页面)的订单中找到它。

Simply add this to your functions.php of your child theme.只需将此添加到您的子主题的functions.php 中即可。

// Our hooked in function – $fields is passed via the filter!
function add_vat_to_checkout_fields( $fields ) {
    $fields['billing']['vat'] = array(
        'type' => 'text',
        'label'     => __('VAT', 'woocommerce'),
        'placeholder'   => _x('VAT', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide hidden'),
        'clear'     => true,
        'value' => get_vat_field('') // this is the function that gets the field from the user account or job post.
    );

    return $fields;
}

/**
 * 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', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('VAT').':</strong> ' . get_post_meta( $order->get_id(), 'vat', true ) . '</p>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM