显示自定义领域吹产品标题Wooccommerce产品单页



我想在后端Wooccommerce产品页面中添加一个文本字段,并在前端的产品标题下方显示/回显文本。

现在我有了在后端写文本的"自定义字段框"(见屏幕截图(,但我不知道如何在前端显示文本。有人能帮我处理这个代码吗?

我关注了这个页面,但它只适用于存档页面。。。在WooCommerce档案页面中的产品标题下方添加自定义字段值

提前谢谢!

杰瑞

Functions.php

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}

您的解决方案是正确的钩子,当您使用add_action((时,您需要选择正确的钩子将代码插入正确的位置。

很明显,你想要的位置是";woocommerce_before_add_to_cart_form";

add_action('woocommerce_before_add_to_cart_form', 'woocommerce_product_custom_fields');

我不确定确切的位置,但你可以更改";woocommerce_before_add_to_cart_form"然后放置你想要的右勾拳。这是一篇很好的文章,展示了每个位置的位置和所需的钩子。让我知道你得到了什么!

添加以下内容以在产品标题下的单个产品页面中显示该产品自定义字段:

add_action( 'woocommerce_single_product_summary', 'custom_field_display_below_title', 7 );

代码位于活动子主题(或活动主题(的functions.php文件中。它应该起作用。

然后它将调用您已经在使用的函数,在产品档案页面上显示自定义字段:

add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}

相关:WooCommerce动作挂钩和覆盖模板

最新更新