在归档页面上显示产品的自定义字段值

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

我想显示在自定义字段中设置的值以显示在shop循环项目中。它正在为商店工作,并显示基于产品的自定义字段。但是产品页面上的项目本身会显示产品页面的所有价值,而不是单个产品的价值。我必须如何修改代码才能使其正常工作?

add_action('woocommerce_after_shop_loop_item', 'grundpreis');
function grundpreis () {
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'grund-preis', true);
    wp_reset_query();
}

它在这里工作:https://keimster.de/shop/但不在这里:https://keimster.de/produkt/vier-koerner-keimster

enter image description here

php woocommerce custom-fields
1个回答
0
投票

您可以尝试以下代码吗?我相信问题出在$wp_query

function grundpreis () {
    global $post;

    $product = wc_get_product( $post->ID );

    // Check for the custom field value
    $my_field = $product->get_meta( 'grund-preis' );

    if ( $my_field ) {
        echo $my_field;
    }
}
add_action('woocommerce_after_shop_loop_item', 'grundpreis', 10, 0 );
© www.soinside.com 2019 - 2024. All rights reserved.