繁体   English   中英

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

[英]Display a custom field above product price in Woocommerce single product pages

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

任何帮助表示赞赏

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

显示图像问题

更新(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文件中。 经过测试和工作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM