1

I create an attribute like this enter image description here

how to check in list.phtml

if(attribute == "yes"){
show block
}else{
don\'t show any thing
}

Update: I get it like this:

$_product->getAttributeText("stock_display");

but it always returns "no" Although I have set some product "yes" enter image description here

Any help? Thank in advance.

6
  • what is the code of your attribute ?
    – Pawan
    Feb 22, 2020 at 4:41
  • what do you mean? please explain
    – MichaelHa
    Feb 22, 2020 at 4:51
  • I update answer
    – MichaelHa
    Feb 22, 2020 at 5:01
  • if you check value of $_product->getAttributeText("stock_display"); it will return 0 or 1
    – Pawan
    Feb 22, 2020 at 5:01
  • you need to check 0 or 1 in your condition
    – Pawan
    Feb 22, 2020 at 5:02

2 Answers 2

1

You can use:

$_product->getStockDisplay();

<?php $attr = $_product->getStockDisplay(); ?>
               OR
<?php $attr = $_product->getData('stock_display'); ?>
               OR
<?php $attr = $_product->getAttributeText('stock_display'); ?>

if($attr == 1){
show block
}else{
don\'t show any thing
}
2
  • It works, Thank you.
    – MichaelHa
    Feb 24, 2020 at 9:10
  • glad to know :) and welcome..
    – Pawan
    Feb 24, 2020 at 10:31
0

Try to use below condition

if($_product->getResource()->getAttribute('stock_display')->getFrontend()->getValue($_product)){
    //Display Block code here
}else{
    //Don't show anything
}

Hope this will help you!

1
  • it not working. still always return "no"
    – MichaelHa
    Feb 22, 2020 at 7:09

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.