0

I'm fairly new to Magento and looking at PHP code is just daunting to me so I'm trying to learn by doing. I've been looking into making my own "Free Gift" label for items on my store but no extension I've found is able to give me the specific functionality I require. Basically if the product has an option with the label "Free Gift" then echo out my .

I managed to get some code that does this perfectly except it displays the Free Gift label on any product that has an option available (size for example).

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

If I place this code in my theme files (media.phmtl, list.phtml) then it works like I want it to except with the problem above (as the code only looks for if the product has options)

Now below I have some different code that works PERFECTLY on the product pages.

<?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; ?> 

But it doesn't work when I place it in list.phtml, no labels show up at all. I'm not sure why this is but I think it's because it can't read if the products had options or not.

I'm not sure if I'm going the wrong way about this or if there's a different solution to this but any help would be massively appreciated as I'm not very experienced in PHP, Thank you.

  • Can you confirm that $_product is product object in list.phtml too? – Adarsh Khatri Jun 25 '15 at 10:53
  • Yes, I see it referenced in other places in list.phtml – joseph drury Jun 25 '15 at 10:56
  • Your code must be inside the foreach loop where $_product is called (ex. foreach ($_productCollection as $_product). – Adarsh Khatri Jun 25 '15 at 10:59
  • From where you get $opval? you need to retrieve option value for product on listing page. – Mufaddal Jun 25 '15 at 11:00
  • I'm only trying to get the option label, not the actual value – joseph drury Jun 25 '15 at 11:07
1

On listing page you cant get product custom option directly,,for that you have to load $_product object

write below code in your list.phtml

<?php foreach ($_productCollection as $_product): ?>

<?php $product = Mage::getModel("catalog/product")->load($_product->getId()); ?>

and then write your logic

    <?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; ?>   
}
?>

Note::It may down your web speed perfomance on listing page because it every time load product object

  • That's giving me a syntax error 'Parse error: syntax error, unexpected 'endif' (T_ENDIF)' about 100 lines down – joseph drury Jun 25 '15 at 11:02
  • 1
    Please do not perform a load on the catalog/product model in a foreach. This will cause huge issues on busy production shops. Especially when the feature requested is purely cosmetic. There will be an other solution by, probably, adding some data to the collection – Sander Mangel Jun 25 '15 at 11:02
  • @josephdrury post your list.phtml – Keyur Shah Jun 25 '15 at 11:10
  • @SanderMangel i know that it down web speed performance that why i have added notice – Keyur Shah Jun 25 '15 at 11:11
  • 1
    @josephdrury see codepad.org/JsE6lPaU – Keyur Shah Jun 25 '15 at 11:28
1

You can use Mage::getSingleton('catalog/product_option') collection method to retrieve options collections of product

Use Mage::getSingleton('catalog/product_option')->getProductOptionCollection($_product) method to retrieve option collection of product.

You don't need to load product every time in foreach.

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.