在Woocommerce单个产品页面中显示高于产品价格的自定义字段

问题描述 投票:3回答:1

在Woocommerce中,我已经以正确的方式在支持的编辑产品页面中设置了一个自定义字段...现在我正在尝试将产品这个自定义字段值显示在产品价格之前的单个产品页面中。

但出于某种原因,使用下面的代码我可以显示它(并且产品价格也没有):

function bd_rrp_price_html( $price, $product ) {

    echo get_post_meta( $post->ID, '_text_field', true );

}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );

任何帮助表示赞赏

这是我想要在视觉上显示的内容:

showing the issue with image

php wordpress woocommerce custom-fields price
1个回答
2
投票

更新(2个替代方案)

请尝试以下重新访问的代码:

add_filter( 'woocommerce_get_price_html', 'custom_single_price_html', 100, 2 );
function custom_single_price_html( $price, $product ) {
    $custom_field = get_post_meta( $product->get_id(), '_text_field', true );
    if ( is_product() && ! empty($custom_field) )
        $price = '<span class="custom-textfield">' . $custom_field . '</span><br>' . $price;
    return $price;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。


或者您也可以使用以下内容:

add_filter( 'woocommerce_single_product_summary', 'single_product_text_field', 8 );
function single_product_text_field() {
    global $product;

    $custom_field = get_post_meta( $product->get_id(), '_text_field', true );
    if ( ! empty($custom_field) )
        echo '<p class="custom-textfield">' . $custom_field . '</p>';
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.