As per default, WooCommerce uses tabs (Additional Information, Description and Reviews) and by using the code below, I removed the tabs and placed their content into the product page using the hook woocommerce_after_single_product_summary
.
As you can see in the code, I've chosen NOT to display the Additional Information content since I cannot figure out how to implement this line with the code below, making it not show if there are no attributes, dimensions or weight. If there is any of these attributes, show the Additional Information.
Here is the line I can not get into the code below:
if ($product -> has_attributes() || $product -> has_dimensions() || $product -> has_weight()) {
add_action( 'woocommerce_after_single_product_summary', 'cac_remove_product_tabs', 2 );
function cac_remove_product_tabs(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_after_single_product_summary', 'cac_display_as_one_product_page', 10 );
}
function cac_display_as_one_product_page() {
wc_get_template( 'single-product/tabs/description.php' );
//wc_get_template('single-product/tabs/additional-information.php');
comments_template();
}
Any ideas on how to make this work? Thanks for any improvement ideas or tips.
There is no need for that has explained below…
The template single-product/tabs/additional-information.php
loads the product weigh, dimensions, and product attributes using wc_display_product_attributes()
template function, that is hooked in woocommerce_product_additional_information
action hook.
function wc_display_product_attributes( $product ) {
wc_get_template( 'single-product/product-attributes.php', array(
'product' => $product,
'attributes' => array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' ),
'display_dimensions' => apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ),
) );
}
So as you can see each of them is first filtered before being displayed by the template single-product/product-attributes.php
where the product weight, dimensions and product attributes are checked through some conditions to avoid empty values:
For weight you have (on line 26):
<?php if ( $display_dimensions && $product->has_weight() ) : ?>
For dimensions you have (on line 33):
<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
For product attributes: if there is no product attributes, it will gives an empty array and nothing will be displayed.
So you don't need to need to use :
if ($product -> has_attributes() || $product -> has_dimensions() || $product -> has_weight()) { }
Addition
Fo the title if there is no data to display, you will need use:
add_filter( 'woocommerce_product_additional_information_heading', 'custom_product_additional_information_heading' );
function custom_product_additional_information_heading( $heading ) {
global $product;
$attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' );
return $product->has_weight() && $product->has_dimensions() && sizeof($attributes) > 0 ? $heading : false;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.