0

Brief

On a Product Page, I want to check if a Product Attribute is equal to a specific value. And then output the appropriate text.

Where I'm at so far

<?php
// Helper for Checking Product Attribute
$_product = $block->getProduct();
// Get Product Attributes
$shape = $_product->getShape();
?>

<!-- Shape Check -->
<?php if ($shape == 'Round'): ?>
    <div>
        <span>I am Round</span>
    </div>
<?php elseif ($shape == 'Square'): ?>
    <div>
        <span>I am Square</span>
    </div>
<?php else: ?>
    <div>
        <span>Else</span>
    </div>
<?php endif; ?>

Issue

Despite checking a product that is either Square or Round, the output only displays Else. Therefore, my code isn't checking the attribute correctly.

1 Answer 1

1

Depends on the attribute type. For example if it's a dropdown you will get the ID.

Take a look at this for example

https://stackoverflow.com/questions/51378438/get-dropdown-attribute-text-magento-2

Try this:

$shape = $_product->getAttributeText('shape');

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.