3

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.

1 Answer 1

3

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.

2
  • Is there any way to use the get_field("pdf") code and get the additional information tab to show if either the dimensions attributes are set OR the "pdf" ACF field is set for a specific product?
    – tobiasg
    Apr 2, 2018 at 21:03
  • 1
    It worked! I had to change isset(get_field("pdf")) to null !== get_field("pdf") though in order to get rid of an error telling me that isset could not be used on get_field(). So maybe that should be changed in your answer if that's true. Thank you so much for your help!
    – tobiasg
    Apr 2, 2018 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.