在WooCommerce中隐藏自定义字段

时间:2018-07-05 10:05:21

标签: wordpress advanced-custom-fields

我在woocommerce的“添加到购物车”按钮旁边添加了一个自定义按钮。

// Enquire Now Button
function wc_shop_enquire_button() {
echo '<a class="button enquire-button" href="'.get_field( 
"enquire_button_link" ).'">'.get_field( "enquire_button_text" 
).'</a>';
}
add_action( 'woocommerce_after_add_to_cart_button', 
'wc_shop_enquire_button', 20 );

但是当字段为空时,我需要隐藏按钮。

我很感谢您能提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

    function wc_shop_enquire_button() {
        if(get_field( "enquire_button_text")!='')
        {
        echo '<a class="button enquire-button" href="'.get_field( 
        "enquire_button_link" ).'">'.get_field( "enquire_button_text" 
        ).'</a>';
        }
      }
 add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_enquire_button', 20 );

答案 1 :(得分:0)

您可以检查变量是否为空,如果不为空,则添加按钮

// Enquire Now Button
function wc_shop_enquire_button() {
    if(!empty(get_field( "enquire_button_text"))){
        echo '<a class="button enquire-button" href="'.get_field( 
        "enquire_button_link" ).'">'.get_field( "enquire_button_text" 
       ).'</a>';
    }
}
add_action( 'woocommerce_after_add_to_cart_button','wc_shop_enquire_button', 20 );
相关问题