简体   繁体   English

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

[英]Display custom field blow product title Woocommerce product single pages

I would like to add a text field in the back-end Woocommerce product page and displaying/echo the text on the front-end below the Product title.我想在后端 Woocommerce 产品页面中添加一个文本字段,并在产品标题下方的前端显示/回显文本。

Now I have the 'custom field box' to write a text in the back-end (see screenshot), but I don't know how I can showing the text on the front-end.现在我有“自定义字段框”在后端编写文本(见截图),但我不知道如何在前端显示文本。 Can someone help me with this code?有人可以帮我处理这段代码吗?

I followed this page, but it is only for archive pages... Add a custom field value below product title in WooCommerce archives pages我关注了此页面,但仅适用于存档页面... 在 WooCommerce 存档页面的产品标题下方添加自定义字段值

Thank you in advance!先感谢您!

jerry杰瑞

Functions.php功能.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));
}

Your solution is the right hook, when you use add_action() you need to choose the right hook to insert your code in the right location.您的解决方案是正确的钩子,当您使用 add_action() 时,您需要选择正确的钩子以将代码插入正确的位置。

Probebly the location you want is "woocommerce_before_add_to_cart_form";可能您想要的位置是“woocommerce_before_add_to_cart_form”;

add_action('woocommerce_before_add_to_cart_form', 'woocommerce_product_custom_fields');

I'm not sure about the exact location but you can just change "woocommerce_before_add_to_cart_form" and place the right hook you want.我不确定确切的位置,但您可以更改“woocommerce_before_add_to_cart_form”并放置您想要的正确钩子。 This is a great article that shows the location and the required hook for every location.这是一篇很棒的文章,它显示了每个位置的位置和所需的钩子。 Let me know what you get!让我知道你得到了什么!

Add the following to display that product custom field in single product pages below product title:添加以下内容以在产品标题下方的单个产品页面中显示该产品自定义字段:

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

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 It should work.它应该工作。

Then it will call the function, that you are already using, to display the custom field on product archive pages:然后它将调用您已经在使用的 function 以在产品存档页面上显示自定义字段:

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>';
    }
}

Related: WooCommerce action hooks and overriding templates相关: WooCommerce 动作挂钩和覆盖模板

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 在 WooCommerce 单个产品标题下显示自定义字段 - Display a custom field under WooCommerce single product title 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 在 WooCommerce 存档页面中的产品标题下方显示 ACF 字段/图像 - Display ACF field/image below the product title in WooCommerce archive pages 在 WooCommerce 档案页面中的产品标题下方添加自定义字段值 - Add a custom field value below product title in WooCommerce archives pages 在WooCommerce单一产品和购物车页面中输出自定义字段值 - Output a custom field value in WooCommerce single product and cart pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM