tobiasg
tobiasg

Reputation: 1073

Add custom field to "Additional Information" tab (WooCommerce)

I'm trying to use ACF to add a custom attribute to the "Additional Information" tab for the products in WooCommerce. I want the admin to be able to upload a PDF that then should be set and linked in the table found in the "Additional Information" tab.

I've found the default template for the attributes in the plugin directory for WooCommerce, more precisely woocommerce/templates/single-product/product-attributes.php. This is how the template looks. I can easily just put the the_field("pdf") there to display the custom field, but the problem I'm running into is that the Additional Information tab will only show if attributes has been added through WooCommerce.

Is there some way to add additional conditional tags for when to display the Additional Information tab? If I somehow could add if (get_field("pdf")) to that code, I think this would be solved.

Upvotes: 3

Views: 7214

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253867

Updated: In Woocommerce if any dimensions, weight or product attributes (set to be displayed on product) exist, "Additional Information" tab will be displayed…

So if the tab is hidden and you have added on single-product/product-attributes.php template a custom field get_field("pdf") to be displayed that has a value, you can force "Additional Information" tab to appear using:

add_filter( 'woocommerce_product_tabs', 'woo_customize_tabs', 100, 1 );
function woo_customize_tabs( $tabs ) {
    if( ! isset($tabs['additional_information']) && null !== get_field("pdf") ){
        $tabs['reviews']['priority'] = 30;
        $reviews = $tabs['reviews'];
        unset($tabs['reviews']);
        $tabs['additional_information'] = array(
            'title'     => __( 'Additional information', 'woocommerce' ),
            'priority'  => '20',
            'callback'  => 'woocommerce_product_additional_information_tab',
        );
        $tabs['reviews'] = $reviews;
    }
    return $tabs;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 3

Related Questions