在单个产品页面的价格后添加自定义元字段值

Add a custom meta field value after the price on single products pages

我在我的产品页面上创建了一个名为 "Info" 的自定义 Metabox。
我想在产品价格后面的模板 content-single-product.php 中显示相应的元字段值。但是它没有输出相应的元字段值,这是行不通的。

我的问题是我不知道如何调用相应的元字段值。是在 function.php php 文件中还是在 content-single-product.php 模板中。

这是我用来创建 Metabox 的代码 (并且有效):

// Create metabox
<?php
function meta_box1()
{
  add_meta_box('new_meta', 'info','new_meta_output','product');
}
add_action ('add_meta_boxes','meta_box1');


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

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

?>

此代码在我的主题文件夹的 function.php 文件中 无效:

// add metabox to behind price product

function meta_product() {
    // don't show in product --problem
    $new_meta2 = get_post_meta($post->ID,'_new_meta',true);
    echo $new_meta2;

    // show in product
    echo "123456";
}
add_action('woocommerce_single_product_summary', 'meta_product',25);

我做错了什么,我怎样才能让它发挥作用?

谢谢。

UPDATE

1) You are NOT getting the Product ID in your last function. For that you need to use WordPress get_the_ID() and the the data value will be outputted as desired.
2) Your function new_meta_save() has not the same name that in the corresponding add_action (hook).
3) All your code goes on function.php file located in your active theme.

这是您重新访问的功能代码:

// Creating a custom metabow in Admin Individual product pages
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"> info meta box</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 $new_meta2;
}
add_action('woocommerce_single_product_summary', 'meta_product',15);

所有代码都在您的活动 child 主题(或主题或任何插件文件)的 function.php 文件中。

此代码已经过测试并且有效


要在价格后显示此元字段值,您需要 1020 之间的优先级, (但是如果要显示在价格之前,标题之后,优先级会在510之间.

woocommerce_single_product_summary 的相关模板上,您有:

    <?php
        /**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         */
        do_action( 'woocommerce_single_product_summary' );
    ?>