简体   繁体   English

在 Woocommerce 单个产品页面的简短描述下显示自定义字段

[英]Display a custom field under short description in Woocommerce single product pages

In woocommerce I am using some code to add a Metabox with a custom field in product edit pages.在 woocommerce 中,我使用一些代码在产品编辑页面中添加一个带有自定义字段的 Metabox。

How can I display the value of this custom field under short description in single product pages?如何在单个产品页面的简短描述下显示此自定义字段的值?

Here is my code:这是我的代码:

add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
    add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}


function info_meta_fields_output($post)
{
    $new_meta = get_post_meta($post->ID,'_new_meta',true);
    echo ('<label for="new_meta"> Custom Text </label>');
    echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}

add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
    $new_meta=sanitize_text_field($_POST['new_meta']);
    update_post_meta ($post_id,'_new_meta',$new_meta);
}


// Displaying the value on single product pages
function meta_product($product_id) {

    $new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
    echo ('<p id="value-on-single-product">' . $new_meta2 . '</p>');
}
add_action('woocommerce_single_product_summary', 'meta_product',30);

But it doesn't display the custom field value.但它不显示自定义字段值。

Updated (added a 2nd custom field as asked in comments)更新(在评论中添加了第二个自定义字段)

You should try the following, that will set your custom field in product general tab Metabox and will display this custom field value under product short description:您应该尝试以下操作,这将在产品常规选项卡 Metabox 中设置您的自定义字段,并将在产品简短描述下显示此自定义字段值:

// Add the custom field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_to_general_product_metabox' );
function add_custom_field_to_general_product_metabox() {
    global $post;

    // Get the selected value
    $value = get_post_meta( $post->ID, '_new_meta', true );
    if( empty( $value ) ) $value = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value, // Displaying the selected value
    ) );


    // Get the selected value
    $value2 = get_post_meta( $post->ID, '_new_meta2', true );
    if( empty( $value2 ) ) $value2 = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta2',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value2, // Displaying the selected value
    ) );
}

// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_to_general_product_metabox' );
function save_custom_field_to_general_product_metabox( $post_id ){

    if( isset( $_POST['new_meta'] ) )
        update_post_meta( $post_id, '_new_meta', esc_attr( $_POST['new_meta'] ) );

    if( isset( $_POST['new_meta2'] ) )
        update_post_meta( $post_id, '_new_meta2', esc_attr( $_POST['new_meta2'] ) );
}


// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_single_product_summary', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
    global $product;

    $custom_field = get_post_meta( $product->get_id(),'_new_meta', true );
    if( ! empty( $custom_field ) )
        echo  '<p id="value-on-single-product">' . $custom_field . '</p>';

    $custom_field2 = get_post_meta( $product->get_id(),'_new_meta2', true );
    if( ! empty( $custom_field2 ) )
        echo '<p id="value-on-single-product">' . $custom_field2 . '</p>';
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works.测试和工作。

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

相关问题 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在 Woocommerce 中的单个产品简短描述下添加文本 - Add Text under Single Product Short Description in Woocommerce 将简短描述移动到 Woocommerce 单个产品页面中的选项卡中 - Move short description into tabs 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 after product description in Woocommerce 显示 现货 WooCommerce 单品简短描述 - Display In stock available variations in WooCommerce single product short description 在Woocommerce中的产品简短描述后显示自定义字段值 - Displaying custom field values after product short description in Woocommerce 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM