1

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.

8
  • @DarkBee sorry my bad. I am using 2 parts of code in one example. I changed the code I added to reflect what I did (but now properly) Commented Aug 20, 2024 at 12:32
  • I'm guessing $product->get_attribute( 'Breedte (mm)') returns an empty string which isset doesn't account for. Related: isset() and empty() - what to use Commented Aug 20, 2024 at 12:46
  • 1
    Just assemble the desired output in a string variable first, then you can use if conditions to add parts to it for non-empty attributes. Return that string value at the very end. Commented Aug 20, 2024 at 12:47
  • Breedte does have a value set and is displayed and is only used for an example. I got this [productpage] (shop.r-go-tools.com/alle-producten/…) and if you scroll down to the product tab "Specificaties" and the nested tab called "Algemeen" you will see 2 empty values: 'Layout toetsenbord' and 'Multimediatoetsen'. I want to hide those 2 lines for example. The table is generated exactly the way as the code above. Do I need to rewrite the code? Or can I easy adjust this one to do what I want it to do. Can you give me an example please? Commented Aug 20, 2024 at 13:00
  • As already pointed out an empty string does not mean a variable isn't set. Please read the linked question/answers - demo. Furthermore, please verify the actual value of $breedte - var_dump($breedte) Commented Aug 20, 2024 at 13:06

1 Answer 1

1

Try the following revised shortcode that will display only the defined product attributes:

// Display grouped attributes Gewicht en Omvang
add_shortcode( 'display-product-attributes', 'shortcode_display_product_attributes' );
function shortcode_display_product_attributes( $atts ) {
    extract( shortcode_atts( array(
        'id'    => get_the_ID(),
    ), $atts, 'display-product-attributes' ) );

    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)');

        if ( $gewicht || $hoogte || $lengte || $breedte ) {
            $output = '<div class="divTableAtt LichtBlauweRegels">
            <div class="divTableAttBody">';

            if ( $gewicht ) {
                $output .= '<div class="divTableAttRow">
                <div class="divTableAttCell">Gewicht (gram)</div>
                <div class="divTableAttCell">' . $gewicht . '</div>
                </div>';
            }

            if ( $hoogte ) {
                $output .= '<div class="divTableAttRow">
                <div class="divTableAttCell">Hoogte (mm)</div>
                <div class="divTableAttCell">' . $hoogte . '</div>
                </div>';
            }

            if ( $lengte ) {
                $output .= '<div class="divTableAttRow">
                <div class="divTableAttCell">Lengte (mm)</div>
                <div class="divTableAttCell">' . $lengte . '</div>
                </div>';
            }

            if ( $breedte ) {
                $output .= '<div class="divTableAttRow">
                <div class="divTableAttCell">Breedte (mm)</div>
                <div class="divTableAttCell">' . $breedte . '</div>
                </div>';
            }

            return $output . '</div></div>';
        }
    }
}

Code goes in functions.php file of your child theme (or in a plugin). It should work.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the code and it works exactly how I want. I forgot about the .= to combine it. Thanks for the help and explanations everyone. I upvoted the answer, but am not high enough on reputation yet.
thanks, didn't know that was also needed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.