Hide custom meta data if empty

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 im getting nowhere… Can anyone point me in the right direction here?

Many Thanks
K

Read more here:: Hide custom meta data if empty

Leave a Reply

Your email address will not be published. Required fields are marked *