Display the text only if attribute value = "yes" in magneto site

Display the text only if attribute value = "yes" in magneto site

September 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display the text only if attribute value = "yes" in magneto site

Displaying Text Conditionally Based on Attribute Value in Magento

In Magento, you can easily display text conditionally based on the value of a custom attribute. This is particularly useful when dealing with attributes that have binary values like “YES” or “NO”. In this guide, we’ll show you how to achieve this for an attribute named name_order.

Understanding the Problem

You have created a custom dropdown attribute called name_order in your Magento backend. The possible values for this attribute are “YES” or “NO”.

In your frontend view file (e.g., catalog_product_view.xml), you want to display a specific text only when the value of name_order is set to “YES”. Currently, your code looks like this:

<?php echo "display text"; ?>

The Solution

To solve this problem, you need to fetch the attribute value using Magento’s core functionality and then use conditional logic to display the text. Here’s a step-by-step solution:

  1. Fetching the Attribute Value

First, you need to get the value of the name_order attribute for the current product. You can do this in your custom block or helper class like so:

use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Api\ProductRepositoryInterface;

class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    private $productRepository;

    public function __construct(
        TemplateContext $context,
        array $data = [],
        ProductRepositoryInterface $productRepository
    ) {
        parent::__construct($context, $data);
        $this->productRepository = $productRepository;
    }

    public function getProduct()
    {
        $productId = 1; // Replace with your product ID
        return $this->productRepository->getById($productId);
    }
}
  1. Using Conditional Logic

Next, you can use PHP’s ternary operator or the if statement to check if the attribute value is “YES”. Here’s how you can display the text conditionally in your view file:

<?php $product = $block->getProduct(); ?>
<?php $nameOrderValue = $product->getData('name_order'); ?>

<!-- If name_order is set to 'YES', display the text -->
<?php if ($nameOrderValue === 'YES'): ?>
    <div class="your-custom-class">
        <?php echo "display text"; ?>
    </div>
<?php endif; ?>

Best Practices and Further Reading

  • It’s a good practice to use the getData() method provided by Magento to fetch attribute values. This ensures compatibility with different e-commerce platforms.

  • For more complex conditional logic or if you want to display different texts for each possible value, consider using an array with keys as the attribute values and values as the corresponding text:

<?php $nameOrderTexts = [
    'YES' => "Display this text when name_order is YES",
    'NO'  => "Display this text when name_order is NO"
]; ?>

<!-- If name_order is set to a known value, display the corresponding text -->
<?php if (isset($nameOrderTexts[$nameOrderValue])): ?>
    <div class="your-custom-class">
        <?php echo $nameOrderTexts[$nameOrderValue]; ?>
    </div>
<?php endif; ?>

Conclusion

Displaying text conditionally based on an attribute value in Magento is a common requirement for customizing the frontend view. By following this guide, you’ve learned how to fetch the attribute value and use conditional logic to display your desired text.

Remember, when dealing with attributes, always use Magento’s core functions like getData() to ensure compatibility and optimal performance.

For more advanced customizations in Magento, consider exploring our blog at LaravelCompany. We cover a wide range of topics from theme development to extension creation.

Catalog API Themes Admin

Our network