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

吃可爱长大的小学妹 提交于 2020-01-23 11:40:11

问题


I have created a custom Metabox named "Info" on my product pages.
I would like to show the corresponding meta field value in the template content-single-product.php behind the product price. But it doesn't output the corresponding meta field value and this is not working.

My problem is that I don't know how I can call the corresponding meta field value. Is it in function.php php file or in content-single-product.php template.

This is the code that I use to create the Metabox (and it works):

// 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');

?>

This code is in function.php file of my theme folder is not working:

// 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);

What I am doing wrong and how can I make it work?

Thanks.


回答1:


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.

Here is your revisited functional code:

// 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);

ALL the code goes in function.php file of your active child theme (or theme or in any plugin file).

This code is tested and works


To show this meta field value after the price you need a priority between 10 and 20, (but if you want to show it before the price, after the title, the priority is going to be between 5 and 10.

On the concerned template for woocommerce_single_product_summary you have:

    <?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' );
    ?>


来源:https://stackoverflow.com/questions/39842989/add-a-custom-meta-field-value-after-the-price-on-single-products-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!