0

This works perfectly, displaying a dropdown of products to offer as a free gift, the label for each option takes the name of the product.

<select id="select_28" class="product-custom-option" title="" name="freeGiftId">
    <option value=""><?php echo $this->__('-- Please Select --')?></option>
    <?php foreach($freeGifts as $giftId): ?>
    <?php $productGift = Mage::getModel('catalog/product')->load($giftId); ?>
    <option value="<?php echo $productGift->getId()?>">
        <?php echo $productGift->getName()?> /*THIS IS THE LINE IN QUESTION */
    </option>
    <?php endforeach; ?>
</select>

I've created a custom attribute however that will be outputted instead of the name but only if it exists.

I'll use <?php echo $productGift->getXyz(); ?> instead of <?php echo $productGift->getName()?>. But I need to add an if statement so that it is only shown if it exists.

I'm trying this and many other variations:-

<select id="select_28" class="product-custom-option" title="" name="freeGiftId">
    <option value=""><?php echo $this->__('-- Please Select --')?></option>
    <?php foreach($freeGifts as $giftId): ?>
    <?php $productGift = Mage::getModel('catalog/product')->load($giftId); ?>
    <option value="<?php echo $productGift->getId()?>">
        <?php if ($productGift->getXyz()): ?> /*THIS IS THE LINE IN QUESTION */
            <?php echo $productGift->getXyz(); ?>
        <?php else: ?>
            <?php echo $productGift->getName()?>
    </option>
    <?php endforeach; ?>
</select>

This isn't working though. Please could someone advise on the correct syntax to use in order to show custom attribute 'Xyz' if it exists, otherwise show the product 'name'?

  • What do you mean by "this isn't working"? What is the result of that? – Marius Dec 18 '13 at 15:29
  • It breaks the page (only loads up to that point in the code and then everything else after that doesn't load). Not that I can identify an error in the log for this. Thanks. – zigojacko Dec 18 '13 at 15:35
  • 1
    You forgot to close the if statement. <?php endif;?> before </option> – Marius Dec 18 '13 at 15:39
  • Yes, that was it. I completely didn't even notice. :/ Thanks very much Marius. – zigojacko Dec 18 '13 at 15:41
1

You could use hasXyz to determine if the object has a value for that attribute, however assuming the value is truthy, what you have should work (it would fail if you expect it to be 0 or '').

EDIT: I can't remember for sure, but I think the closing endif is required in alternate syntax.

  <?php if ($productGift->getXyz()): ?> /*THIS IS THE LINE IN QUESTION */
      <?php echo $productGift->getXyz(); ?>
  <?php else: ?>
      <?php echo $productGift->getName()?>
  <?php endif; ?>
| improve this answer | |
  • Oh for goodness sake, the endif escaped. I think this is it. Just testing it now. Thanks. – zigojacko Dec 18 '13 at 15:37
  • Thanks very much for spotting this, the runaway endif I didn't even notice. Cheers. – zigojacko Dec 18 '13 at 15:45
  • Lost count of the amount of hours I've wasted chasing down such issues. I didn't even notice until after I'd replied :). – Peter O'Callaghan Dec 18 '13 at 15:47

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.