LemonStand Wiki

Displaying product extra options

Extra options are free or paid options which could be added to a product. For example, gift wrap is a paid extra option.

You can output product extra options as a list of checkboxes below the product price. We recommend you create a separated partial for displaying extra options, instead of placing the corresponding code directly into the product page code, because partials help you to keep your product page code clean and easy to read.

Start with creating a new partial and assign it some meaningful name, for example shop:product_extra_options. The following code outputs a list of product extra options. Each extra option is presented with a checkbox and label.

<? foreach ($product->extra_options as $option):
        $control_name = 'product_extra_options['.$option->option_key.']';
        $posted_options = post('product_extra_options', array());
        $is_checked = isset($posted_options[$option->option_key]);
?>
        <input name="<?= $control_name ?>" 
                <?= checkbox_state($is_checked) ?> 
                id="extra_option_<?= $option->id ?>" value="1" type="checkbox"/>
                
        <label for="extra_option_<?= $option->id ?>"><?= h($option->description) ?>:</label>
        <? if ($option->price > 0): ?>
                + <?= format_currency($option->price) ?>
        <? else: ?>
                free
        <? endif ?>
<? endforeach ?> 

A product's extra options are contained in the $extra_options field of the $product object. Each element of the $extra_options field is an object having the $option_key field used for identifying an extra option by LemonStand, the $description field and the $price field.

For each extra option the example code outputs a checkbox. Please note the value of the NAME attribute of the INPUT element corresponding the checkbox. The NAME attribute value has the following format: product_extra_options[option_key]. You should not change this format. The checkbox_state function, used in the code example, keeps the checkboxes checked between page reloads. The product page could be reloaded if a visitor selects another grouped product (in the drop-down menu), or adds the product to the cart.

Depending on the option price the code outputs a price value or the word “free”, in case if an option has price equal to 0. For displaying currency values the format_currency function is used.

Once you finish developing the partial, you can output the list of product options on the product page, below the product price or in any other place, but inside the FORM element:

<? $this->render_partial('shop:product_extra_options') ?>  

See also:

Next: Displaying related products
Previous: Displaying product options
Return to Product page

Comments

No comments posted so far.

Add your comment

Loading form...