2

I'm trying to display a "Free Gift" label on the product picture for any product that has the custom options label "Free Gift".

The code I have below works on the actual product page but if I try it on the product list page (list.phmtl) the "Free Gift" label isn't showing up.

I've been told to load my collection but I'm not sure how to do that, won't that also slow my site down if I'm loading my entire collection each time somebody goes on a product list page?

Maybe there's a different solution to my issue?

<?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; ?> 
8
  • In case you haven't noticed, Grid mode and list mode uses different foreach loops. Make sure you put your label inside of both loops Commented Jun 24, 2015 at 15:45
  • Sorry but I'm new to Magento and PHP. Could you explain what I need to do please? Commented Jun 24, 2015 at 15:51
  • I'm sorry, your question is too broad for me to answer. What exactly are you trying to do? Commented Jun 24, 2015 at 15:57
  • My products have a custom option called "Free Gift" and I want my code to read that the product has a free gift option and then echo out some text saying "Free Gift" over the image. The code I posted above works on the actual product page, but I also want it to work on the product listing thumbnails. This old code I have works on the product listing pages but it displays on all products with ANY custom option, which is not what I want. I'll post it below so you can see. Commented Jun 24, 2015 at 16:06
  • <?php if ( $_product->getData('has_options') && ($_product->getTypeID() == 'simple') ): ?> <!-- do something --> <div class="free_gift_text"><?php echo "Free Gift" ?></div>; <?php endif; ?> Commented Jun 24, 2015 at 16:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.