0

First of all I want to say I have little coding experience. I want to show available sizes in the productbox on the shop page. I got this working after some searching around here. Now I want to only display the sizes when sizes are available. If there are no size attributes available I would like the text: No sizes available or no text at all.

Example

I got the following code now:

add_action('woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5);


function display_size_attribute() {

    global $product;

    if ($product->is_type('external')) {

        $taxonomy = 'pa_maat';

        echo "Maten:";
        echo '<span class="attribute-size">' 
                . $product->get_attribute($taxonomy) 
        . '</span>';

    }

}
0

to test is the text is not empty, try that :

function display_size_attribute() {

    global $product;

    if ($product->is_type('external')) {

        $taxonomy = 'pa_maat';

        $text = $product->get_attribute($taxonomy);

        if (!empty($text)) {
            echo "Maten:";
            echo '<span class="attribute-size">' 
                    . htmlspecialchars($text) 
            . '</span>';
        }

    } // END if ($product->is_type('external')) {


} // END function display_size_attribute() {
  • Doesnt work :( Uncaught Error: Call to undefined function is_empty() in wp-content/themes/flatsome-child/functions.php:27 – Salespotters Nov 13 at 11:53
  • oh yes, I edited the code, try it now. – Kaperto Nov 13 at 11:56
  • It works! Thanks you are a lifesaver! – Salespotters Nov 13 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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