Search code examples
phpwordpresswoocommerceproductadmin

Show or hide WooCommerce admin product custom field depending on the product type


I added an custom field to the products

add_action( 'woocommerce_product_options_sku', 'prodcode_fields');

function prodcode_fields() {
    global $post, $thepostid, $product_object;

    echo '<div class="options_prodcode_fields">';
   
    woocommerce_wp_text_input(
        array(
            'id' => '_prodcode_good_number',
            'value' => $product_object->get_meta('_prodcode_number', true),
        'type' => 'number',
            'label' => 'Product Code',
            'desc_tip' => true,
        'description' => __('Product Code', 'sv-prodcode'),
        )  );

echo '</div>';
}

All is well, but it is necessary not to display it in variable goods. Can you tell me how to do it using woocommerce tools?

I tried to add 'class' => 'show_if_simple', but I get nonsense - only input is hidden, but labels and so on remain.

enter image description here


Solution

  • To hide an additional product custom field, you can use 2 approaches:

    Using the wrapper_class attribute with "show_if_simple" or "hide_if_variable" like:

    • 'wrapper_class' => 'show_if_simple',
    • 'wrapper_class' => 'hide_if_variable',

    Or you can directly use "show_if_simple" or "hide_if_variable" in the starting <div> like:

    echo '<div class="options_prodcode_fields show_if_simple">';
    

    or

    echo '<div class="options_prodcode_fields hide_if_variable">';
    

    So in for your code:

    add_action( 'woocommerce_product_options_sku', 'display_admin_product_custom_setting_fields');
    function display_admin_product_custom_setting_fields() {
        global $product_object;
    
        // Only for simple product type
        echo '<div class="options_prodcode_fields show_if_simple">';
       
        woocommerce_wp_text_input(
            array(
                'id'            => '_prodcode_good_number',
                'type'          => 'number',
                'label'         => __('Product Code', 'sv-prodcode'),
                'description'   => __('Product code description', 'sv-prodcode'),
                'desc_tip'      => true,
                'value'         => $product_object->get_meta('_prodcode_number'),
            )  );
    
        echo '</div>';
    }
    

    Addition

    The different product type classes to show or hide a custom product field
    By Default in WooCommerce:
    • Product type "simple": show_if_simple or hide_if_simple
    • Product type "variable": show_if_variable or hide_if_variable
    • Product type "external": show_if_external or hide_if_external
    • Product type "grouped": show_if_grouped or hide_if_grouped
    • Virtual product: show_if_virtual or hide_if_virtual
    • Downloadable product: show_if_downloadable or hide_if_downloadable
    Some other foreign products:
    • Product type "subscription": show_if_subscription or hide_if_subscription
    • Product type "booking": show_if_booking or hide_if_booking
    • Product type "bundle": show_if_bundle or hide_if_bundle