PHP Magento 1.6 – How to Get Custom Options Label

custom-optionsmagento-1.6PHP

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

Best Answer

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