【发布时间】:2017-09-08 12:29:51
【问题描述】:
我有一个看似简单的任务,但我终其一生都想不通。
我的 Woocommerce 产品的管理页面上有一个自定义字段。我只需要在此处设置的值显示在 Woocommerce 结帐页面和电子邮件中。
我已经设法使用以下代码使该字段显示在产品页面上:
add_action( 'woocommerce_product_options_general_product_data', 'create_shipdate_custom_field' );
function create_shipdate_custom_field() {
woocommerce_wp_text_input( array(
'id' => '_shipdate',
'type' => 'text',
'label' => __('Shipping Date', 'woocommerce' ),
'description' => '',
'desc_tip' => 'true',
'placeholder' => __('i.e. 16 December 2017', 'woocommerce' ),
) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_shipdate_custom_field' );
function save_shipdate_custom_field( $post_id ) {
$wc_text_field = $_POST['_shipdate'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_shipdate', esc_attr( $wc_text_field ) );
}
}
function product_date() {
echo get_post_meta( get_the_ID(), '_shipdate', true );
}
add_action('woocommerce_single_product_summary', 'product_date', 30);
我可以使用以下代码在结帐页面上显示静态文本,但它不适用于我的自定义字段:
function emaildate() { echo "This will display fine"; }
add_action('woocommerce_order_item_meta_start', 'emaildate', 10, 1);
提前感谢您的帮助!
【问题讨论】:
-
我能够在结帐页面上显示静态文本 - 可以展示你是如何尝试这个的,即
emaildate方法? -
所以,例如:
function emaildate() { echo "This will display fine"; }add_action('woocommerce_order_item_meta_start', 'emaildate', 10, 1); -
请在您的问题中添加任何其他相关信息。 cmets 中的信息很容易被遗漏。
-
您应该可以使用
echo get_post_meta( get_the_ID(), '_shipdate', true );来显示_shipdate值。 -
感谢您到目前为止的帮助。我已经用附加信息编辑了我的问题。 @Und3rTow,该代码适用于在单个产品页面中显示该字段。但是,结帐页面没有显示它。
标签: php wordpress woocommerce