0

i would like to call an image if the drop-down attribute is marked as "yes" from drop-down - if its "No" then i dont want to call an image. i am using the below code but this seems to pull the image for yes and no.

<div>

<?php //echo $_product->getAttributeText('discount'); 
if($_product->getAttributeText('discount')){ ?>
<img src='<?php echo $this->getViewFileUrl('images/footer-logo.png'); ?>' alt="Demo">

<?php } ?>
</div>

Thank you

1 Answer 1

0

The problem is with $_product->getAttributeText('discount'); this will return either "Yes" or "No". Both will be validated to "true" by PHP, because they are strings with length > 0.

You can test this easily by doing:

<?php

var_dump((bool)"No"); // true
var_dump((bool)"Yes"); // also true

More info can be found here: http://php.net/manual/de/language.types.boolean.php Check the section "Converting to booleans".

The correct solution on your case would be:

<?php
if($_product->getDiscount()) { // either "1" or "0"

Your Answer

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

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