admin管理员组

文章数量:1122826

I have a custom field created for WooCommerce products.

I am using the below code to create

// ADD CUSTOM WOO DATA
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');


function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(array(
        'id' => '_sales_email',
        'placeholder' => '',
        'label' => __('Sales Email Link', 'woocommerce'),
        'desc_tip' => 'true'
    ));

    echo '</div>';
}


function woocommerce_product_custom_fields_save($post_id)
{
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_sales_email'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_sales_email', esc_attr($woocommerce_custom_product_text_field));
}

The Code works well, and displays the data on the product page.

I am displaying the data via a shortcode.

<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>

The problem I am having is hiding the data if the field is empty..

I have tried

if( get_field('_sales_email') )
{
    //echo the_field('_sales_email');
}
else
{
    echo "<style>.dbtn_sales{display:none !important;}</style>";
}

but I'm getting nowhere... Can anyone point me in the right direction here?

I have a custom field created for WooCommerce products.

I am using the below code to create

// ADD CUSTOM WOO DATA
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');


function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(array(
        'id' => '_sales_email',
        'placeholder' => '',
        'label' => __('Sales Email Link', 'woocommerce'),
        'desc_tip' => 'true'
    ));

    echo '</div>';
}


function woocommerce_product_custom_fields_save($post_id)
{
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_sales_email'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_sales_email', esc_attr($woocommerce_custom_product_text_field));
}

The Code works well, and displays the data on the product page.

I am displaying the data via a shortcode.

<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>

The problem I am having is hiding the data if the field is empty..

I have tried

if( get_field('_sales_email') )
{
    //echo the_field('_sales_email');
}
else
{
    echo "<style>.dbtn_sales{display:none !important;}</style>";
}

but I'm getting nowhere... Can anyone point me in the right direction here?

Share Improve this question edited Jan 14, 2019 at 13:50 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jan 14, 2019 at 13:38 SoutherndavisSoutherndavis 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

AFAIR get_field() function comes with ACF and there is no such function built in WP.

It isn't obvious, based on your code, that you use ACF plugin on your site, so there is a chance it might not work.

But, since you already use native WP function for setting the meta data, then it would be good idea to use native WP function to get that value as well.

Also, I wouldn't hide that button using CSS. I would change your code (part that is responsible for printing the button) to this:

<?php if ( get_post_meta(get_the_ID(), '_sales_email', true) ) : ?>
<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>
<?php endif; ?>

Also... If that value comes from user input, then it would be a good idea to assure it is properly escaped:

<?php if ( get_post_meta(get_the_ID(), '_sales_email', true) ) : ?>
<div class="dbtn_sales"><a href="mailto:<?php esc_attr( get_post_meta(get_the_ID(), '_sales_email', true) ); ?>">Email Sales</a></div>
<?php endif; ?>

本文标签: Hide custom meta data if empty