How can I apply a piece of Conditional Logic to a Custom Field’s Checkbox?

I am currently working on a WordPress powered website, with WooCommerce functionality.

I have created a custom field, to appear in the Back End of a Product Page, using the following code:

<?php
function theme_name_woocommerce_custom_fields() {
    // Custom Text Box
    woocommerce_wp_checkbox(
        array(
            'id'          => '_custom_text_box',
            'label'       => 'Show Custom Text Box',
            'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
            'desc_tip'    => 'true',
        ) 
    );
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );

function save_theme_name_woocommerce_custom_field( $post_id ) {
    if ( ! empty( $_POST['_custom_text_field'] ) ) {
        update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
    }
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>

I would now like to create a Conditional Logic, to achieve the following:

If Checkbox is ticked, show Custom Text Box on Product Page, Else do not
show Custom Text Box.

I am aware of conditional logic within WordPress but not aware of how to apply such logic to WooCommerce and how to link up with the Checkbox.

Any help on this matter, would be greatly appreciated.

Read more here: How can I apply a piece of Conditional Logic to a Custom Field’s Checkbox?

Leave a Reply

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