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

标签 php wordpress woocommerce product meta-boxes

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

screenshot

我的问题是我不知道如何调用相应的元字段值。它是在 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);

所有代码都在您的事件子主题(或主题或任何插件文件)的 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' );
    ?>

关于php - 在单个产品页面的价格后添加自定义元字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39842989/

相关文章:

php - 同一天两次之间以 15 分钟为增量打印时间

php - PayPal 付款 - HTTP 响应代码 400

php - 在通过 IP 地址访问我的网站的用户面前打开对话框

php - 如果 php ://input receives simultaneous webhooks? 会发生什么

php - 连接失败 : Connection refused mysql (LAMP)

php - 从 xml 文件中选择全部,其中 person ='Bob'

php - 如何使用$wpdb在wordpress上显示mysql表数据?

php - WordPress HTML代码,帮忙找找

woocommerce - 如果产品已添加到 WooCommerce 中的购物车,如何重命名“添加到购物车”按钮

php - Woocommerce 产品类别覆盖