显示自定义字段打击产品标题Woocommerce产品单页

问题描述

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

现在,我有了“自定义字段框”,可以在后端编写文本(请参见屏幕截图),但是我不知道如何在前端显示文本。有人可以帮我这个代码吗?

我关注了此页面,但这仅适用于存档页面... Add a custom field value below product title in WooCommerce archives pages

提前谢谢!

杰瑞

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',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 action hooks and overriding templates

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...