2

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:

$_product->addOption(
    array(
        '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
1

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.
        }
    }
}
1

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') {
        $option->setType(Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO);
    }

    //This class requires the option, product and layout instance to
    //work correctly.
    $o->setOption($option)
        ->setProduct($_product)
        ->setLayout($this->getLayout());

    //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.

  • 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.