Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

html_options Recommended Change

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
devin
Smarty n00b


Joined: 13 Dec 2010
Posts: 1

PostPosted: Mon Dec 13, 2010 7:07 pm    Post subject: html_options Recommended Change Reply with quote

This recommended code change is directly related to customer service calls where the user was curious as to why we didn't sell a specific size or color of a specific item. It has also raised concern regarding, how many sales we may have missed out on.

Currently the plugin "html_options" does not capture product options that are out of stock. So here are the code changes that will apply a disabled attribute to the option if it is out of stock. In the image below, you will notice that Twin is labeled (Out of Stock) and due to the disabled attribute the user cannot select this item(see image below)


In the revised code below I have added a new case (case 'disabled') to the switch, then referenced the case a few times in the code.

Revised Version: function.html.options.php
Code:
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsFunction
*/

/**
* Smarty {html_options} function plugin
*
* Type:     function<br>
* Name:     html_options<br>
* Purpose:  Prints the list of <option> tags generated from
*            the passed parameters
*
* @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
      (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com>
* @param array $params parameters
* Input:<br>
*            - name       (optional) - string default "select"
*            - values     (required if no options supplied) - array
*            - options    (required if no values supplied) - associative array
*            - selected   (optional) - string default not set
*            - output     (required if not options supplied) - array
* @param object $smarty Smarty object
* @param object $template template object
* @return string
* @uses smarty_function_escape_special_chars()
*/

function smarty_function_html_options($params, $smarty, $template)
{
    require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
    //$smarty->loadPlugin('Smarty_shared_escape_special_chars');

    $name = null;
    $values = null;
    $options = null;
    $selected = $disabled = array();
    $output = null;

    $extra = '';

    foreach($params as $_key => $_val) {
        switch ($_key) {
            case 'name':
                $$_key = (string)$_val;
                break;

            case 'options':
                $$_key = (array)$_val;
                break;

            case 'values':
            case 'output':
                $$_key = array_values((array)$_val);
                break;

            case 'selected':
                $$_key = array_map('strval', array_values((array)$_val));
                break;

            case 'disabled':
                $$_key = array_map('strval', array_values((array)$_val));
                break;

            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (!isset($options) && !isset($values))
        return '';
    /* raise error here? */

    $_html_result = '';

    if (isset($options)) {
        foreach ($options as $_key => $_val)
        $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $disabled);
    } else {
        foreach ($values as $_i => $_key) {
            $_val = isset($output[$_i]) ? $output[$_i] : '';
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $disabled);
        }
    }

    if (!empty($name)) {
        $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
    }

    return $_html_result;
}

function smarty_function_html_options_optoutput($key, $value, $selected, $disabled)
{
    if (!is_array($value)) {
        $_html_result = '<option value="' .
        smarty_function_escape_special_chars($key) . '"';
      if (in_array((string)$key, $disabled)) {
         $_html_result .= ' disabled="disabled"';
      } else {
         if (in_array((string)$key, $selected))
                 $_html_result .= ' selected="selected"';
      }
      $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
    } else {
        $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
    }
    return $_html_result;
}

function smarty_function_html_options_optgroup($key, $values, $selected)
{
    $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
    foreach ($values as $key => $value) {
        $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
    }
    $optgroup_html .= "</optgroup>\n";
    return $optgroup_html;
}


The Template Change Required:
Code:
{html_options options=$option.select selected=$option.default_value disabled=$option.select_disabled}


I hope to see this code added to future versions. It is a must have for anyone with products options that have the potential to be out of stock.
Thanks![/code]
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP