Magento2 – Check if Product Attribute Value is Null

magento2product-attribute

I want to show the below code only if product attribute value is not null

The attribute to check is price_range

CURRENT CODE

<p>The price is not available for this product, it is approximately <?php echo $_helper->productAttribute($block->getProduct(), $block->getProduct()->getPriceRange(), 'price_range') ?>*. <i>*Price shown is for guidance purposes only</i>.</p>

Can anyone please suggest an update with an if statement to get this working?

Best Answer

I assume you just want to see if the data is set against the product before echoing it?

This will depend on if you are getting a collection of products, adding the attribute to select and then looping through them / if you are just on a product page, but the following should work:

<?php if ($block->getProduct()->getPriceRange()): ?>
 //Do things here
<?php endif; ?>