如何解决在“我的帐户”页面上的“编辑帐单和送货地址”部分中显示自定义字段数据的预览
我的目的是在woocommerce帐户页面上的“编辑帐单和收货地址”部分中显示自定义字段数据的预览。我对 my-address.php 模板的代码进行了调整,在该模板中,我可以正确显示2个文本的预览,一个文本用于计费,一个文本用于运输(但是当我添加代码时)我用来显示来自自定义字段的元数据时,它会产生调试错误,从而扭曲页面布局)。这是模板的代码:
<?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',$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',$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','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文件中显示自定义字段元数据的代码:
<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',esc_html($custom_order_meta) );?>
</p> <?php
}
?></span>
我非常感谢您的帮助,不知道是否有过滤器或挂钩可以实现。
解决方法
我能够通过删除“ woocommerce”来解决该问题,并在朋友@LoicTheAztec的评论帮助下非常感谢。代码下方:
<?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
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。