Jump to content

Display Stock Number in Options?


Recommended Posts

Is there a way to display the available stock in the options dropdown/area rather than in the normal stock area?


On the front end display it just says "Stock Level: 4 - 15", giving the lowest to the highest of the stock levels.


It would be nice if it would show next to the option itself, like this:


Large, Blue (22)


This would let the customer know how many of each item is available, rather than guessing.  If one of stock options is at 0, then it says  Stock Level: 0-15.


Thanks for the help!


Using Version 5.2.1

Link to comment
Share on other sites

I may not be recalling this right, but my options are presented as:

Size: S M L (with price differentials if necessary)

Color: R B G Y W B

in selectors on the View Product front page, but not as a list of all combinations (such as: RS RM RL BS BM BL etc). Only until you try to add it to the cart do you find out if that combo is in stock.


Are you saying your option combinations are listed as one long list?


If so, and if this is how a stock Cubecart skin shows them, then yes, getting this piece of data from the matrix and making it available to the skin would be somewhat easy.


If this is not a stock skin, then you may have to entreat the skin designer to assist in using the data being made available.

Link to comment
Share on other sites

Very sorry - I didn't communicate this right.


I do not have combinations. I was trying to make my post easier, and I complicated it.  I simply have single options.


They are date options for classes, so it would be good to see how many "seats" are left in each class date


I am using a modified stock Cubecart Skin


So what I want to do is add the number available of the option next to the option in the selector itself, not above it. I want them to see it before trying to add it to the cart.


Thanks again.

Link to comment
Share on other sites

Alright, using the templates only, I got the option selectors to look like this:


Blue (5)

Red (5)

Green (5)


However, once the stock reduces on an option, then it shows a range display, rather than reducing that particular one:


Blue (4-5)

Red (4-5)

Green (4-5)


Anyone have a clue where to modify this?


My code on the content.product template:

	  {if $PRODUCT.stock_level && is_array($OPTIONS)}
	  <p>{$LANG.catalogue.stock_level}: {$PRODUCT.stock_level}</p>


	  {if is_array($OPTIONS)}
	  <div class="options">
		{foreach from=$OPTIONS item=option}
	  	  {if $option.type == '0'}
		  <label for="option_{$option.option_id}" class="return">{$option.option_name}{if $option.price} ({$option.symbol}{$option.price}){/if}{if $option.required} *{/if}</label>
		  <span><select name="productOptions[{$option.option_id}]" id="option_{$option.option_id}" class="textbox {if $option.required}required{/if}" style="width:150px;">
			<option value="">{$LANG.form.please_select}</option>
			{foreach from=$option.values item=value}
			<option value="{$value.assign_id}">{$value.value_name}{if $value.price} ({$value.symbol}{$value.price}){/if} ({$PRODUCT.stock_level})</option>
Link to comment
Share on other sites

Let's start with the concept of a 'options_identifier'. This is the key into the options matrix. The options matrix is the solution to having distinct stock levels for each option combination (in your case, the option is a date, but we will continue to use color).


The options matrix is stored in the CubeCart_option_matrix table. The {$PRODUCT.stock_level} you found comes from a query that asks only for the lowest stock level and highest stock level among every combination of options for this product: (catalogue.class.php line 290)

['stock_level'] =  (['min_stock'] == ['max_stock']) ? ['max_stock'] : ['min_stock'] - ['max_stock'];

The above is abbreviated, but says if min equals max, use max, but if not, express the stock level as a range from the lowest stock level found among all the option combinations to the highest stock level found among all the option combinations for this product. Again, this one expression informs for every combination for this product.


Thus, the (5) is the min and max from looking at every option combination. Once 'blue' was sold, the blue option combination has a stock level of 4 and is now the min. Since min does not equal max, the expression becomes '(4-5)'.


So, we can't use the stock_level given to the $PRODUCT array. We need something that can derive the 'options_identifier' and fetch the stock_level for each specific option combination.


One place that can get the stock_level for each option combination from the CubeCart_option_matrix table is the getProductStock function (catalogue.class.php line 750). But we need the option_identifier.

Link to comment
Share on other sites

Specifically, we will let the getProductOptions functions (catalogue.class.php line 512) fetch the $OPTIONS array (i.e., 'Color'), and within that array is another array of the option values array. So, we have $OPTIONS[i]['values'][k][key], where i is the option Color, and values are arrays, k, that have the value_name, Black, Blue, etc., and other data.


This, {foreach from=$OPTIONS item=option}, allows us to iterate over i, so we can work with $option.values.


This, {foreach from=$option.values item=value}, allows us to iterate over k, so we can work with the values.


Ultimately, we want $value.stock_level. We just need to figure out where to get it and add it to the array structure.


That happens in the _displayProductOptions function (catalogue.class.php line 1251):

$option_list[$value['option_id']]['values'][] = array(
    'assign_id'        => $value['assign_id'],
    'price'            => (isset($value['option_price']) && $value['option_price']>0) ? $GLOBALS['tax']->priceFormat($value['option_price'], true) : false,
    'symbol'        => (isset($value['option_price']) && $value['option_negative'] == 0) ? '+' : '-',
    'value_id'        => $value['value_id'],
    'value_name'    => $value['value_name'],
    'stock_level'    => $value['stock_level'],

I have already added the stock_level array element.


Now we just have to convince getProductOptions to derive the options_identifier and query the matrix table.

Link to comment
Share on other sites

In the file catalogue.class.php, at around line 573, find: (and the new lines have already been added)

                    if ($category['option_type'] == 0) {
                        // Get Option Values
                        if (($values    = $GLOBALS['db']->select('CubeCart_option_value', false, array('option_id' => $category['option_id'], 'value_id' => $mid), array('priority' => 'DESC', 'value_name' => 'ASC'))) !== false) {
                            foreach ($values as $value) {
                                foreach ($option[$value['option_id']] as $opt) {
$options_identifier_string = '';
                                    if ($opt['value_id'] == $value['value_id']) {
$options_identifier_string .= $value['option_id'].$value['value_id'];
$opt['options_identifier']    = md5($options_identifier_string);
$products_matrix_data = $GLOBALS['db']->select('CubeCart_option_matrix', array('stock_level' ,'product_code', 'upc', 'jan', 'isbn', 'image'), array('product_id' => (int)$product_id, 'options_identifier' => $opt['options_identifier'],));
$opt['stock_level'] = $products_matrix_data[0]['stock_level'];
                                        $option_array[$category['option_type']][$category['option_id']][]    = array_merge($category, $value, $opt);
                    } else {


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...