0

I have this to display the attribute on my product page:

<?php echo "Bottle size: ", $_product->getAttributeText('bottle_size'); ?>

However, not all of my products will have this attribute so I don't want it to display anything if the "bottle_size" attribute has no value in it.

How can I achieve this?


UPDATE:::::

Tried this but that still doesn't work:

<?php
$attribute_value = $product->getResource()->getAttribute("bottle_size")->getFrontend()->getValue($product);
if ($attribute_value) {
echo $attribute_value
} 
?>
1

You can add IF condition to display if your Attribute contains value

<?php $attributeValue = $_product->getAttributeText('bottle_size'); ?>
<?php if(isset($attributeValue)): ?>
    <?php echo "Bottle size: ".$attributeValue; ?>
<?php endif; ?>
  • That's something I am looking for, unfortunately it's not working for me :/ Hi Abhishek btw ;) you again! – Greg Nov 28 '17 at 16:05
  • Do you get attribute value in $_product->getAttributeText('bottle_size') .. like 500ML – Abhishek Panchal Nov 28 '17 at 16:06
  • Yes, <?php echo "Bottle size: ", $_product->getAttributeText('bottle_size'); ?> that works just fine. – Greg Nov 28 '17 at 16:07
  • Try my updated answer – Abhishek Panchal Nov 28 '17 at 16:08
  • Still nothing :/ – Greg Nov 28 '17 at 16:12
1

Please try below code this

$attribute_value = $product->getResource()->getAttribute("attribute_code")->getFrontend()->getValue($product);
if ($attribute_value != "") {
    echo $attribute_value;
} 
  • don't seem to work... Look my updated question – Greg Nov 28 '17 at 15:50
  • Please try my updated answer. If it will not work then please let me know in detail what issue you are getting. – V. Vish Nov 28 '17 at 16:05
  • That didn't work either :/ – Greg Nov 28 '17 at 16:12
  • What value getting in this variable $attribute_value? – V. Vish Nov 28 '17 at 16:14
  • This is my current code: <?php $attribute_value = $product->getResource()->getAttribute("bottle_size")->getFrontend()->getValue($product); if ($attribute_value != "") { echo $attribute_value; } ?> The value of "bottle_size" should be 50ml – Greg Nov 28 '17 at 16:15

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.