Edvin Uddfalk
Edvin Uddfalk

Reputation: 401

Check if product has specific attributes set to it, then show additional div if true

I am trying to create a function to show additional product information if requirements are met dependent on whether some attributes are set or not on specific products. For this I am using two attributes called 'pa_storlek' and 'pa_djurart'. Seems to me everything should be working but no matter what I do I just keep getting the else value (Inte X-Small).

I need to check the two attributes together, meaning that if I get both attributes values at the same time the statement should be valid, and - if not, it should show the else output.

Anyone has an idea? Been looking around alot but can't seem to find another question like mine..

// X-Small dogs list
add_action( 'woocommerce_single_product_summary', 'x_small_dogs', 90 );
function x_small_dogs() {
    global $product;

    $product_attributes = $product->get_attributes();

    // Get the product attribute value
    $size = $product->get_attribute('pa_storlek');
    $animal = $product->get_attribute('pa_djurart');

    // If product has attribute 'size = x-small' and 'animal = hund'
    if( strpos($size, 'x-small') && strpos($animal, 'hund') ) {
        echo '<div class="">X-Small</div>';
    } else {
        echo '<div class="">Inte X-small</div>';
    }
}

Upvotes: 0

Views: 151

Answers (1)

Stefino76
Stefino76

Reputation: 369

If strpos find the string starting at the first character, return 0 (index position) and it is evaluated like false.

So try

if( strpos($size, 'x-small')!==false && strpos($animal, 'hund')!==false) {

Upvotes: 1

Related Questions