I have some two options added to products via the admin panel. What I want to do is insert some custom html between these options. Is it possible to do this programmatically ?

Something similar to:

        'type' => 'html',
        'value' => '<!--html to go here-->',
        'position' => 1
    ) );

where the type would require a new view script that would just echo the option value.

Alternatively, is there a way to echo out the options independently? e.g.:

echo $_product->getOption(0)->toHtml(), // echo first option
     $myHtmlHere,                       // echo my html
     $_product->getOption(1)->toHtml(); // echo second option

The easiest way to do it is to loop through custom options of your product and then depending on option ID insert your custom HTML:

if($_product->hasOptions()) {
    foreach ($_product->getOptions() as $option) {
        echo $option->getOptionHtml();

        if ($option->getId() == [your-option-id]) {
            // output your custom HTML here.
| improve this answer | |

Thanks to @Tim for pointing me in the right direction:

$_options = $_product->getOptions();
foreach($_options as $option)
    //radio, checkbox, select and multi-select all run off this class
    $o = new Mage_Catalog_Block_Product_View_Options_Type_Select();

    //Each product will save the option with its own ID. Every option has the same
    //name, so will check for that. If the right option, set it as a radio element
    if($option->getTitle() == 'Colour') {

    //This class requires the option, product and layout instance to
    //work correctly.

    //echo the element
    echo $o->getValuesHtml();

This does not echo the title of the option or wrap it in interesting HTML (no DDs).

I have altered the foreach loop to suit my needs but this is the code needed to echo the options I want.

| improve this answer | |
  • Which file did you place this code in? – Michael Yaeger Jul 29 '16 at 17:44

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.