在“我的帐户"页面上的“编辑帐单和送货地址"部分中显示自定义字段数据的预览 [英] Display a preview of the custom field data in the Edit Billing and Shipping Address section on the my account page

查看:62
本文介绍了在“我的帐户"页面上的“编辑帐单和送货地址"部分中显示自定义字段数据的预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是在woocommerce帐户页面上的编辑帐单和收货地址"部分中显示自定义字段数据的预览.我对 my-address.php 模板的代码进行了调整,在该模板中,我可以正确显示2种文本的预览,一种用于计费,一种用于运输,(但是当我添加代码时我用来显示来自自定义字段的元数据时,它会产生调试错误,从而使页面布局失真.这是模板的代码:

My intention is to show a preview of the custom field data in the Edit Billing and Shipping Address section on the my woocommerce account page. I made an adjustment to the code of the my-address.php template, in which I can correctly show the preview of 2 texts, one for billing and one for shipping, (but when I add the code that I use to display meta data from custom fields it generates a debugging error distorting the page layout). here is the code of template:

<?php
/**
 * My Addresses
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-address.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 2.6.0
 */

defined( 'ABSPATH' ) || exit;


// My Billing Addresses

$customer_id = get_current_user_id();

if ( ! wc_ship_to_billing_address_only()  ) {
    $get_addresses = apply_filters(
        'woocommerce_my_account_get_addresses',
        array(
            'billing'  => __( 'Billing address', 'woocommerce' ),
        ),
        $customer_id
    );
} else {
    $get_addresses = apply_filters(
        'woocommerce_my_account_get_addresses',
        array(
            'billing' => __( 'Billing address', 'woocommerce' ),
        ),
        $customer_id
    );
}

$oldcol = 1;
$col    = 1;
?>

<p>
    <?php echo apply_filters( 'woocommerce_my_account_my_address_description', esc_html__( 'The following addresses will be used on the checkout page by default.', 'woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</p>

<?php if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) : ?>
    <div class="u-columns woocommerce-Addresses col2-set addresses">
<?php endif; ?>

<?php foreach ( $get_addresses as $name => $address_title ) : ?>
    <?php
        $address = wc_get_account_formatted_address( $name );
        $col     = $col * -1;
        $oldcol  = $oldcol * -1;
    ?>

    <div class="u-column<?php echo $col < 0 ? 1 : 2; ?> col-<?php echo $oldcol < 0 ? 1 : 2; ?> woocommerce-Address">
        <header class="woocommerce-Address-title title">
            <h3><?php echo esc_html( $address_title ); ?></h3>
            <a href="<?php echo esc_url( wc_get_endpoint_url( 'edit-address', $name ) ); ?>" class="edit"><?php echo $address ? esc_html__( 'Edit', 'woocommerce' ) : esc_html__( 'Add', 'woocommerce' ); ?></a>
        </header>
        
        <address>
Display preview of custom field order meta data BILLING.
        </address>
        
    </div>

<?php endforeach; ?>

<?php if ( ! wc_ship_to_billing_address_only() ) : ?>

    <?php
endif;


// My Shipping Addresses

$customer_id = get_current_user_id();

if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) {
    $get_addresses = apply_filters(
        'woocommerce_my_account_get_addresses',
        array(
            'shipping' => __( 'Shipping address', 'woocommerce' ),
        ),
        $customer_id
    );
} else {
    $get_addresses = apply_filters(
        'woocommerce_my_account_get_addresses',
        array(
            'billing' => __( 'Billing address', 'woocommerce' ),
        ),
        $customer_id
    );
}

$oldcol = 1;
$col    = 1;
?>



<?php if ( ! wc_shipping_enabled() ) : ?>
    <div class="u-columns woocommerce-Addresses col2-set addresses">
<?php endif; ?>

<?php foreach ( $get_addresses as $name => $address_title ) : ?>
    <?php
        $address = wc_get_account_formatted_address( $name );
        $col     = $col * -1;
        $oldcol  = $oldcol * -1;
    ?>

    <div class="u-column<?php echo $col < 0 ? 1 : 2; ?> col-<?php echo $oldcol < 0 ? 1 : 2; ?> woocommerce-Address">
        <header class="woocommerce-Address-title title">
            <h3><?php echo esc_html( $address_title ); ?></h3>
            <a href="<?php echo esc_url( wc_get_endpoint_url( 'edit-address', $name ) ); ?>" class="edit"><?php echo $address ? esc_html__( 'Edit', 'woocommerce' ) : esc_html__( 'Add', 'woocommerce' ); ?></a>
        </header>
        
        <address>
Display preview of custom field order meta data SHIPPING.
        </address>
    
    </div>

<?php endforeach; ?>

<?php if ( ! wc_shipping_enabled() ) : ?>
    </div>
    <?php
endif;

这是我用来在某些php文件中显示自定义字段元数据的代码:

this is the code i use to display custom field meta data in some php files:

<span><?php
    $custom_order_meta = get_post_meta($order->get_order_number(), 'meta_data_order_field_key', true);

    if( ! empty($custom_order_meta) )
    { ?>
<p> <?php
printf( '<strong>Title Field:</strong> ' . esc_html( '%s', 'woocommerce' ), esc_html($custom_order_meta)  );?> 
</p> <?php 
    }
    ?></span>

附加的图像,以便更好地理解.

Attached image for better understanding.

我非常感谢您的帮助,不知道是否有过滤器或钩子可以做到这一点.

I appreciate any help, I don't know if there is a filter or hook to do it.

推荐答案

我能够通过删除"woocommerce"并在朋友@LoicTheAztec的评论帮助下非常感谢您.代码下方:

I was able to solve it by removing "woocommerce" and with the help of the comment of the friend @LoicTheAztec thank you very much. Below the code:

    <?php
    $custom_user_meta = get_user_meta(get_current_user_id(), 'custom_field_key_meta', true);

    if( ! empty($custom_user_meta) )
    { ?>
<p> <?php
printf( '<strong>Title Custom Field:</strong> ' . esc_html( '%s' ), esc_html($custom_user_meta)  );?> 
</p> <?php 
    }
    ?>

这篇关于在“我的帐户"页面上的“编辑帐单和送货地址"部分中显示自定义字段数据的预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆