I used the code below to display a certain amount of attributes within a Custom Tab by using a shortcode. This works fine but not all products carry the same specs. How can I hide the lines that contain no data?
I entered this code:
// Display grouped attributes Gewicht en Omvang
function get_product_attributes_gewicht_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes-gewicht' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$gewicht = $product->get_attribute( 'Gewicht (gram)' );
$hoogte = $product->get_attribute( 'Hoogte (mm)');
$lengte = $product->get_attribute( 'Lengte (mm)');
$breedte = $product->get_attribute( 'Breedte (mm)');
return '<div class="divTableAtt LichtBlauweRegels">' .
'<div class="divTableAttBody">' .
'<div class="divTableAttRow">' .
'<div class="divTableAttCell">Gewicht (gram)</div>' .
'<div class="divTableAttCell">' . $gewicht . '</div>' .
'</div>' .
'<div class="divTableAttRow">' .
'<div class="divTableAttCell">Hoogte (mm)</div>' .
'<div class="divTableAttCell">' . $hoogte . '</div>' .
'</div>' .
'<div class="divTableAttRow">' .
'<div class="divTableAttCell">Lengte (mm)</div>' .
'<div class="divTableAttCell">' . $lengte . '</div>' .
'</div>' .
'<div class="divTableAttRow">' .
'<div class="divTableAttCell">Breedte (mm)</div>' .
'<div class="divTableAttCell">' . $breedte . '</div>' .
'</div>' .
'</div>' .
'</div>';
}
}
add_shortcode( 'display-attributes-gewicht', 'get_product_attributes_gewicht_shortcode' );`
I want to hide the if the attirube in that row is empty. I tried to use and if statement around that row, but it doesn't work.
if (isset($breedte)){
return '<div class="divTableAttRow">' .
'<div class="divTableAttCell">Breedte (mm)</div>' .
'<div class="divTableAttCell">' . $breedte . '</div>' .
'</div>';
}
I did close the line before with a ; and started the line after with a new 'return' statement.
The results are that the entire result of other functions are shown and not only this set as it is suposed to.
I am still learning this and figuring out how to get it to work.
$product->get_attribute( 'Breedte (mm)')returns an empty string whichissetdoesn't account for. Related: isset() and empty() - what to useifconditions to add parts to it for non-empty attributes. Return that string value at the very end.$breedte-var_dump($breedte)