'WooCommerce Additional Information - if empty, hide
I'm working on an eCommerce project at the moment. I'm using WooCommerce in WordPress.
I've got ridden of the product data tabs and displayed product descriptions and additional information within the summary section.
I would like to know how to hide the additional information section if it's empty? Or even, generate a text saying 'None'?
Any help would be appreciated. Mille merci!
Solution 1:[1]
add_filter( 'woocommerce_product_tabs', 'am_ninja_remove_product_tabs', 98 );
function am_ninja_remove_product_tabs( $tabs ) {
global $product;
$id = $product->get_id(); // change this to $product->id fro WC less than 2.7
$my_custom_data = get_post_meta($id, 'am_ninja', true );
if(empty($my_custom_data)) {
unset( $tabs['additional_information'] ); // Remove the additional information tab
}
return $tabs;
}
Check this code snippet
Solution 2:[2]
Thank you to the poster above.
I've been fiddling around and with reference to [Woocommerce Docs][1], I've found a solution to my problem.
Here goes:
First, I've removed the product data tabs completely.
//Removing product data tabs
add_action('init', 'tws_remove_product_tabs');
function tws_remove_product_tabs() {
remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10);
}
Then:
//Adding product additional info in summary - if it's empty hide it
add_action('woocommerce_single_product_summary', 'woocommerce_product_additional_information_tab', 25);
function woocommerce_product_additional_information_tab() {
global $product;
if ($product - > has_attributes() || $product - > has_dimensions() || $product - > has_weight()) { // Check if product has attributes, dimensions or weight
return $tabs;
wc_get_template('single-product/tabs/additional-information.php');
}
}
It's working now.
[1]: https://docs.woocommerce.com
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mujuonly |
Solution 2 | alev |