php Woocommerce结帐与编辑地址布局

qyzbxkaa  于 2022-12-02  发布在  PHP
关注(0)|答案(3)|浏览(125)

所以,当你去结帐,你看到所有的计费字段输入,但我想删除所有他们,并显示一个卡一样,我的帐户/编辑地址与编辑按钮。这可能吗?
基本上我需要弹出卡在计费字段的地方。有什么建议?
我已经在使用 “在结账页面隐藏账单地址,但保留信息” 答案代码。

t3irkdon

t3irkdon1#

所以,
我已经使用了我放在我的问题上的代码,并在我的functions.php文件中添加了以下代码:

function action_woocommerce_after_checkout_billing_form( $wccs_custom_checkout_field_pro ) {

    if (is_user_logged_in()){
        $customer_id = get_current_user_id();
        $address = get_user_meta( $customer_id, 'billing_address_1', true );
        if (!empty($address)){          
            echo '<style>.woocommerce-billing-fields__field-wrapper {display: none;}</style>';
            echo '<div class="checkoutaddress">
                    <a href="#" class="edit">'. __( 'Edit', 'woocommerce' ).'</a>
                    <h3>'. __( 'Billing address', 'woocommerce' ) .'</h3>
                <address>'.
            get_user_meta( $customer_id, 'billing_first_name', true ).' '.
            get_user_meta( $customer_id, 'billing_last_name', true )    .'<br>'.
            get_user_meta( $customer_id, 'billing_phone', true ).'<br>'.
            $address.'<br>'.
            get_user_meta( $customer_id, 'billing_postcode', true ).' '.
            get_user_meta( $customer_id, 'billing_city', true ).'<br>'.
            get_user_meta( $customer_id, 'billing_state', true )
                .'</address>
            </div>';
        }
    }

}; 
add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );

它将检查用户是否已登录(否则显示字段),并在检查后检查帐单地址是否已填写(否则也显示字段)
希望这能帮助一些人。这不是一个很好的解决方案,但它的工作!

gab6jxml

gab6jxml2#

这里是一个稍微改进的版本,包括送货地址,jQuery显示编辑表单和一些样式,使它看起来像一张卡片。谢谢你的代码。
如果用户可以点击编辑,修改一些信息,点击保存来关闭表单并重新加载卡片内容,那就太好了。有什么想法吗?

add_action( 'woocommerce_before_checkout_billing_form', 'example_billing_card', 10, 1 ); 
function example_billing_card() {

if (is_user_logged_in()){
    $customer_id = get_current_user_id();
    $address = get_user_meta( $customer_id, 'billing_address_1', true );
    if (!empty($address)){          
        echo '<style>.woocommerce-billing-fields__field-wrapper {display: none !important;}</style>';
        echo '<a href="#" id="example-billing-edit" class="edit">'. __( 'Edit', 'woocommerce' ).'</a>
        <div class="example-checkout-billing">
            <address>'.
        get_user_meta( $customer_id, 'billing_first_name', true ).' '.
        get_user_meta( $customer_id, 'billing_last_name', true ).' '.
        get_user_meta( $customer_id, 'billing_phone', true ).'<br>'.
        get_user_meta( $customer_id, 'billing_address_1', true ).' '.
        get_user_meta( $customer_id, 'billing_address_2', true ).'<br>'.
        get_user_meta( $customer_id, 'billing_postcode', true ).' '.
        get_user_meta( $customer_id, 'billing_city', true ).' '.
        get_user_meta( $customer_id, 'billing_state', true ).' '.
        get_user_meta( $customer_id, 'billing_country', true )
            .'</address>
        </div>';
    }
}}; 
add_action( 'woocommerce_before_checkout_shipping_form', 'example_shipping_card', 10, 1 );
function example_shipping_card() {

if (is_user_logged_in()){
    $customer_id = get_current_user_id();
    $address = get_user_meta( $customer_id, 'shipping_address_1', true );
    if (!empty($address)){          
        echo '<style>.woocommerce-shipping-fields__field-wrapper {display: none !important;}</style>';
        echo '<h3>Shipping details</h3><a href="#" id="example-shipping-edit" class="edit">'. __( 'Edit', 'woocommerce' ).'</a>
        <div class="example-checkout-shipping">
            <address>'.
        get_user_meta( $customer_id, 'shipping_first_name', true ).' '.
        get_user_meta( $customer_id, 'shipping_last_name', true ).'<br>'.
        get_user_meta( $customer_id, 'shipping_address_1', true ).' '.
        get_user_meta( $customer_id, 'shipping_address_2', true ).'<br>'.
        get_user_meta( $customer_id, 'shipping_postcode', true ).' '.
        get_user_meta( $customer_id, 'shipping_city', true ).' '.
        get_user_meta( $customer_id, 'shipping_state', true ).' '.
        get_user_meta( $customer_id, 'shipping_country', true )
            .'</address>
        </div>';
    }
}};
add_action( 'wp_head', 'checkout_example_js_css', 900 );
function checkout_example_js_css(){
if ( is_checkout() ){
    ?>
<script>
jQuery(document).ready(function(){
jQuery('#example-billing-edit').on('click', function(event) {        
    jQuery('.woocommerce-billing-fields__field-wrapper').toggleClass('show');
    jQuery('.example-checkout-billing').toggle('show');
    jQuery('#example-billing-edit').toggle('show');
});
});
jQuery(document).ready(function(){
jQuery('#example-shipping-edit').on('click', function(event) {        
    jQuery('.woocommerce-shipping-fields__field-wrapper').toggleClass('show');
    jQuery('.example-checkout-shipping').toggle('show');
    jQuery('#example-shipping-edit').toggle('hide');
});
});
</script>
<style> .example-checkout-billing, .example-checkout-shipping {
        background: var(--ast-global-color-6);
        padding: 16px;
        border-radius: 8px;
        display: inline-block;
    }
    #example-billing-edit,
    #example-shipping-edit {
        display: block;
    }
    .woocommerce-billing-fields__field-wrapper.show,
    .woocommerce-shipping-fields__field-wrapper.show {
        display: flex !important;
    }
    h3#ship-to-different-address {
        display:none;
    }
    address {
        margin: 0;
    }

</style>
    <?php
}
}
1bqhqjot

1bqhqjot3#

我更喜欢在模板myaccount/my-address.php中进行如下更改:

<address>
        <?php if($address) : ?>
            <?php
                echo ($name === 'billing') ? (
                    get_user_meta( $customer_id, 'billing_first_name', true ).' '. get_user_meta( $customer_id, 'billing_last_name', true ) .'<br>'.
                    get_user_meta( $customer_id, 'billing_postcode', true ).'<br>'.
                    get_user_meta( $customer_id, 'billing_state', true ).'<br>'.
                    get_user_meta( $customer_id, 'billing_city', true ).'<br>'.
                    get_user_meta( $customer_id, 'billing_address_1', true ).'<br>'.
                    get_user_meta( $customer_id, 'billing_address_2', true )
                ) : (
                    get_user_meta( $customer_id, 'shipping_first_name', true ).' '. get_user_meta( $customer_id, 'shipping_last_name', true ) .'<br>'.
                    get_user_meta( $customer_id, 'shipping_postcode', true ).'<br>'.
                    get_user_meta( $customer_id, 'shipping_state', true ).'<br>'.
                    get_user_meta( $customer_id, 'shipping_city', true ).'<br>'.
                    get_user_meta( $customer_id, 'shipping_address_1', true ).'<br>'.
                    get_user_meta( $customer_id, 'shipping_address_2', true )
                )
                ;
                // echo $address ? wp_kses_post( $address ) : esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' );
            endif;
        ?>
    </address>

相关问题