【问题标题】:Hide billing address from checkout page but keep the information从结帐页面隐藏帐单地址,但保留信息
【发布时间】:2016-09-27 09:35:12
【问题描述】:

我想在结帐页面隐藏帐单地址(而不是删除它)并在注册时首次设置帐单地址?

允许在帐单上显示.pdf

我添加了这段代码:

<?php

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_email']);
    unset($fields['billing']['billing_city']);
    return $fields;
}

通过添加此代码,帐单地址不会添加到 pdf 帐单中

那么我应该添加或编辑哪些代码?

提前谢谢你。

【问题讨论】:

    标签: php wordpress woocommerce account user-registration


    【解决方案1】:

    第一步:注册时首次设置帐单地址

    1) 您需要更改 WooCommerce 设置以进行注册。
    WooCommerce 设置 > 帐户(标签)

    2) 在用户注册我的帐户页面中添加结算字段:

    基地:How to add custom fields in user registration on the "My Account" page

    自定义代码(将该代码复制到您的活动子主题或主题的 function.php 文件中)

    if( !is_admin() )
    {
        // Function to check starting char of a string
        function startsWith($haystack, $needle)
        { 
            return $needle === '' || strpos($haystack, $needle) === 0;
        }
    
        // Custom function to display the Billing Address form to registration page
        function my_custom_function()
        {
            global $woocommerce;
            $checkout = $woocommerce->checkout();
    
            ?>
                <h3><?php _e( 'Billing Address', 'woocommerce' ); ?></h3>
            <?php
    
            foreach ($checkout->checkout_fields['billing'] as $key => $field) :
                woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
            endforeach;
        }
        add_action('register_form','my_custom_function');
    
    
        // Custom function to save Usermeta or Billing Address of registered user
        function save_address($user_id)
        {
            global $woocommerce;
            $address = $_POST;
    
            foreach ($address as $key => $field) :
                if(startsWith($key,'billing_'))
                {
                    // Condition to add firstname and last name to user meta table
                    if($key == 'billing_first_name' || $key == 'billing_last_name')
                    {
                        $new_key = explode('billing_',$key);
                        update_user_meta( $user_id, $new_key[1], $_POST[$key] );
                    }
                    update_user_meta( $user_id, $key, $_POST[$key] );
                }
            endforeach;
        }
        add_action('woocommerce_created_customer','save_address');
    
    
        // Registration page billing address form Validation
        function custom_validation()
        {
            global $woocommerce;
            $address = $_POST;
    
            foreach ($address as $key => $field) :
    
                // Validation: Required fields
                if(startsWith($key,'billing_'))
                {
    
                    if($key == 'billing_country' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please select a country.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_first_name' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter first name.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_last_name' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter last name.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_address_1' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter address.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_city' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter city.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_state' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter state.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_postcode' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter a postcode.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_email' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter billing email address.', 'woocommerce' ) );
                    }
    
                    if($key == 'billing_phone' && $field == '')
                    {
                        $woocommerce->add_error( '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Please enter phone number.', 'woocommerce' ) );
                    }
                }
    
            endforeach;
        }
        add_action('register_post','custom_validation');
    
    }
    

    第二步:将帐单地址隐藏在结帐空白处删除

    您需要使用 CSS 来定位具有 display: none !important; 规则的帐单冻结字段。

    【讨论】:

    • 非常感谢这个明确的答案!但我想要的是不要在结帐页面中再次询问帐单地址我在function.php文件中添加了一些代码以将其删除,但是注册时它没有得到地址设置它是空的!
    • @AbdelazizMirad 因此,您可能需要禁用 在“结帐”页面上显示回头客登录提醒的 woocommerce 设置复选框。你能提供一个链接到你的网站吗?如我所说,在结帐时隐藏帐单字段最好使用 CSS 规则。
    • 我尝试了 Checkout 和 My Account 选项卡中的所有内容,是的,我可以隐藏 html/css 我需要保留信息!
    • 我用的是wordpress主题,这个主题在注册时不要求提供账单/送货地址!我为地址添加了一些字段,但似乎没有将其视为帐单地址! (哈哈,不!我不是法国人:p)
    • @AbdelazizMirad 因为您需要使用现有的计费字段名称和 slug(就像在我的代码示例中一样)。您可以添加自定义字段,但您需要保留原始计费字段条款(slugs)。当然,您可以更改显示的标题。此外,您应该在结帐时添加自定义结算字段,即使您隐藏它们。
    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2015-09-17
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多