gpt4 book ai didi

php - 将 WordPress 用户电子邮件而不是账单电子邮件设置为 WooCommerce 结帐字段的默认值

转载 作者:行者123 更新时间:2023-12-04 07:15:59 32 4
gpt4 key购买 nike

我想要做的是更改结帐字段,这样如果用户登录,他们就无法编辑他们的帐单电子邮件字段。
下面的代码很好用,但理想情况下,我想消除用户能够拥有 2 个电子邮件地址并设置 $address_fields['billing']['billing_email'] 的值的情况。成为其帐户中的用户电子邮件地址。
我试过添加

'value' => $current_user->user_email
'default' => $current_user->user_email
到数组但是这似乎什么都不做。
所以:
  • 首先,我怎样才能为这个表单设置一个值并防止它被改变。
  • 其次是$current_user->user_email获取用户帐户(联系人)电子邮件而不是来自其帐户的帐单电子邮件的正确方法。

  • function custom_checkout_fields( $address_fields ) {


    $current_user = wp_get_current_user();

    if ( is_user_logged_in() ) {
    unset( $address_fields['billing']['billing_email'] );
    unset( $address_fields['billing']['billing_em_ver'] );

    $address_fields['billing']['billing_email'] = [
    'label' => 'Email address',
    'required' => false,
    'description' => '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>',
    'custom_attributes' => [
    'disabled' => 'disabled',
    ]
    ];



    return $address_fields;
    } else{
    return $address_fields;
    }
    }
    add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' ,20, 1 );

    最佳答案

    首先,转到 /plugins/woocommerce/templates/checkout ,复制该文件夹中找到的 form-billing.php 模板文件并将其粘贴到 yourTheme/woocommerce/checkout/ 中。
    其次,通过编辑复制的 form-billing.php 模板文件并更改以下代码段来防止 WooCommerce 自动填充电子邮件地址字段:

        <div class="woocommerce-billing-fields__field-wrapper">
    <?php
    $fields = $checkout->get_checkout_fields( 'billing' );

    foreach ( $fields as $key => $field ) {
    woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    }
    ?>
    </div>
    对此:
        <div class="woocommerce-billing-fields__field-wrapper">
    <?php
    $fields = $checkout->get_checkout_fields( 'billing' );

    foreach ( $fields as $key => $field ) {
    if ( 'billing_email' != $key ) {
    woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    } else {
    woocommerce_form_field( $key, $field );
    }
    }
    ?>
    </div>
    第三,将 WordPress 电子邮件注入(inject)该字段,使其不可变,并通过将以下内容添加到主题的 functions.php 文件中来添加您的自定义描述:

    function shillongtitude_billing_email_field($fields) {

    if ( is_user_logged_in() ) {

    $current_user = wp_get_current_user();

    //set the user email as the email field value
    $fields['billing']['billing_email']['default'] = $current_user->user_email;

    //set the description
    $fields['billing']['billing_email']['description'] = '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>';

    // set attributes
    $fields['billing']['billing_email']['custom_attributes'] = array( 'readonly'=>'readonly', 'label' => 'Email address', 'required' => false );

    return $fields;
    } else {
    return $fields;
    }
    }
    add_filter('woocommerce_checkout_fields', 'shillongtitude_billing_email_field');

    关于php - 将 WordPress 用户电子邮件而不是账单电子邮件设置为 WooCommerce 结帐字段的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68763291/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com