Displaying Advanced Custom Field (ACF) value in a WooCommerce hook

前端 未结 2 753
春和景丽
春和景丽 2021-01-15 02:38

I want to display the value from an Advanced Custom Field (ACF) in Woocommerce and I am using this code in this function hooked:

add_action( \'woocommerce_si         


        
2条回答
  •  春和景丽
    2021-01-15 03:14

    Your problem is that you can't use echo with ACF the_field('my_field') , because when using the_field('my_field') is just like using echo get_field('my_field'), so you are trying to echo an echo. Instead use get_field('my_field') this way in your code:

    add_action( 'woocommerce_single_product_summary', 'charting', 20 );
    
    function charting() {
        if( !empty( get_field('size_chart') ) ) { // if your custom field is not empty…
            echo '

    size guide

    '; } return; }

    After, I have add empty() function in your condition…

    You can also try to return it instead of echo:

        return '

    size guide

    ';

    Reference:

    • ACF the_field
    • ACF get_field

提交回复
热议问题