0

I'm trying to display a "Free Gift" text box over any products that have options. This works well but now I'm just trying to refine it so it doesn't appear on products that have size options which is also a custom options.

Is it possible to get the options label and display my text box depending on if it says "Free Gift"?

Here is the code I've used to display my text box on any product that has options.

<?php if ( $_product->getData('has_options') && ($_product->getTypeID() == 'simple')): ?>
<div class="free_gift_text"><?php echo "Free Gift" ?></div>; <?php endif; ?>

And this is the code I've tried but doesn't work.

<?php if ( $_product->getData('has_options') && ($_product->getTypeID() == 'simple') && ($_product->getOptionsLabel('Free Gift') == "Free Gift") ): ?><div class="free_gift_text"><?php echo "Free Gift" ?></div>; <?php endif; ?>
2

You can get the custom option label like this :

<?php

$theoptions = $_product->getOptions();
foreach($theoptions as $opkey=> $opval)
{
    if(( $_product->getData('has_options')) && ($_product->getTypeID() == 'simple') && ($opval->getTitle() == 'Free Gift')): ?>
       <div class="free_gift_text"><?php echo "Free Gift" ?></div>;
    <?php endif; ?>   
}
  • I was getting a syntax error using this code, so I removed the { } and it works...but only at the actual product page (media.phtml). This doesn't work when I want the "Free Gift" to display on list.phtml – joseph drury Jun 24 '15 at 13:22
  • load the product collection on list.phtml and then try the code – Manashvi Birla Jun 24 '15 at 13:25
  • Ok I'm not sure how to do that, do I need to add it in the code you provided? – joseph drury Jun 24 '15 at 14:21

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.