Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Change the Qty Available from Numerical to Text

Change the Qty Available from Numerical to Text

Locked

Views: 2,304

Results 1 to 20 of 31
This thread is locked. New replies are disabled.
4 Mar 2010, 4:26 AM
#1
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Change the Qty Available from Numerical to Text

Hi,

I'm trying to find out how to change the Qty Available from numerical to numerical and or text

Some of our suppliers list the qty available in numerical, and others in text such as ;

Available
Back to Back

5

Etc etc...
Any suggestions, or assistance would be appreciated.

Thanks,
Mike

4 Mar 2010, 12:38 PM
#2
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

Hi,

I'm trying to find out how to change the Qty Available from numerical to numerical and or text

Some of our suppliers list the qty available in numerical, and others in text such as ;

Available
Back to Back

5

Etc etc...
Any suggestions, or assistance would be appreciated.

Thanks,
Mike

php has the following functions that you can use:

strval() --> num 2 str
intval() --> str 2 num

I have a gut feeling that you have asked the wrong question though.
Exactly what is it that you are trying to do?

Cheers
Rod

4 Mar 2010, 3:43 PM
#3
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Change the Qty Available from Numerical to Text

Are you getting automated feeds that you import with easy populate? Or do you manually enter such information?

The product quantity field is designed to be handled as a number, and even though it might (or might not) hold text if you gave it that, the processing functions would choke on it. There are several ways you could deal with the situation, depending on how you receive and enter the quantity, and how you want to display it.

4 Mar 2010, 8:06 PM
#4
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

RodG:

Exactly what is it that you are trying to do?

Cheers
Rod

A number of products have the qty as 0 with "Back-to-Back" as the availability status, others have >5. This one I can handled, just run a simple formula in excel to convert >5 to 5

Rather than simply display 0 stock, I was hoping to have those with text inserted into the Qty field.

I realize with the Qty as text I cannot then use the reducing stock option when an order is placed, but as we rely predominantly on the suppliers feed, I can turn that option off.

5 Mar 2010, 1:44 AM
#5
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

A number of products have the qty as 0 with "Back-to-Back" as the availability status, others have >5. This one I can handled, just run a simple formula in excel to convert >5 to 5

Rather than simply display 0 stock, I was hoping to have those with text inserted into the Qty field.

I realize with the Qty as text I cannot then use the reducing stock option when an order is placed, but as we rely predominantly on the suppliers feed, I can turn that option off.

If this were my problem, the first thing I'd do would be to modify the database itself and change the field type for products.products_quantity from the current "float" into "varchar".

This will allow the quantities to be entered as text strings.

If you are lucky, this field will then be output to screen "as is", and your main requirement will be solved.

I would however expect a few repercussions due to the fact that zencart also uses this field for stock control/availability, and will therefore resolve to "0", suggesting there is nothing in stock.

Depending on the extent and effects of these repercussions I'd either decide to take care of them one at a time, or revert the database back to its original state and try a different approach.

My alternate approach would be to add another field to the products table, and call it something like "products_quantity_text". It is then a matter of modifying the relevant parts of the zencart code so that any output uses this field rather than the original "products_quantity" field. As with my first approach, the amount of work required to locate and make all the modifications won't be really known until you try it.

Cheers
Rod

5 Mar 2010, 2:22 AM
#6
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

RodG:

If this were my problem, the first thing I'd do would be to modify the database itself and change the field type for products.products_quantity from the current "float" into "varchar".

This will allow the quantities to be entered as text strings.

If you are lucky, this field will then be output to screen "as is", and your main requirement will be solved.

I would however expect a few repercussions due to the fact that zencart also uses this field for stock control/availability, and will therefore resolve to "0", suggesting there is nothing in stock.

Depending on the extent and effects of these repercussions I'd either decide to take care of them one at a time, or revert the database back to its original state and try a different approach.

My alternate approach would be to add another field to the products table, and call it something like "products_quantity_text". It is then a matter of modifying the relevant parts of the zencart code so that any output uses this field rather than the original "products_quantity" field. As with my first approach, the amount of work required to locate and make all the modifications won't be really known until you try it.

Cheers
Rod

Hey Rod,

Very much appreciate you taking the time to document this.

I tried the first approach, and as you suspected, Zen Cart just reverts it back to 0.. regardless of what I set the field to in the database.

I tried a few of the options via admin eg turning off stock control, but to no avail, straight back to 0

The second option seems feasible, (if I had the confidence to attack it.!)

I think in the interim I may have to resort to some data manipulation in the spread sheet before I upload it

5 Mar 2010, 5:45 AM
#7
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Change the Qty Available from Numerical to Text

A reasonable workaround may be to have the spreadsheet take all text that doesn't easily translate into a number and set it to something like 9999 (or a quantity you would never see in reality). Then a small bit of code at output locations could test for the quantity being 9999 and change an output variable's value from a number to a word like"Available" or "In Stock".

This would not give the precision of your various suppliers' feeds, but would be basically functional.

5 Mar 2010, 7:32 AM
#8
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

gjh42:

A reasonable workaround may be to have the spreadsheet take all text that doesn't easily translate into a number and set it to something like 9999 (or a quantity you would never see in reality). Then a small bit of code at output locations could test for the quantity being 9999 and change an output variable's value from a number to a word like"Available" or "In Stock".

This would not give the precision of your various suppliers' feeds, but would be basically functional.

Thanks,

All sounds great in theory, but you lose me when it comes to creating the code out that output end.. I would not have a clue where to start.. Give me a spread sheet in excel and I'll have it making cups of tea for you, but code.. uurrghhhh...

Having said that,

If I am going to take it from a text base to a number base, and then generate code to take the number back to text, would it not make more sense to have the code accept text in the first place ?

Cheers,
Mike

5 Mar 2010, 8:01 AM
#9
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Change the Qty Available from Numerical to Text

Yes, except that that would require a lot more work (or possibly be unstable) than the workaround I described (which would only take a couple of lines of code in the PHP).

5 Mar 2010, 8:14 AM
#10
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Change the Qty Available from Numerical to Text

A mockup of the code required could look like this:```php
if ($products_qty == 9999) {
$qty_output = 'Available';
}else{
$qty_output = $products_qty;
}

//then use $qty_output where $products_qty would be output

You would substitute whatever variable actually holds product quantity in a particular location for $products_qty.
6 Mar 2010, 1:22 AM
#11
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

gjh42:

A mockup of the code required could look like this:```php
if ($products_qty == 9999) {
$qty_output = 'Available';
}else{
$qty_output = $products_qty;
}

//then use $qty_output where $products_qty would be output

> You would substitute whatever variable actually holds product quantity in a particular location for $products_qty.

Thanks for this Glenn,   

Which file, and where would it be added;.

It looks like it would be this file ?

/includes/templates/norts/templates/tpl_product_info_display.php


and would I have to amend the database to varchar or text as opposed to numerical for product_qty value..


Could I also use multiple instances of the Mod to account for multiple options of text ?  eg 

($products_qty == 9999) {
  $qty_output = 'Available';
}else{
  $qty_output = $products_qty;
}

($products_qty == 8888) {
  $qty_output = 'Back-to-Back';
}else{
  $qty_output = $products_qty;
}

($products_qty == 7777) {
  $qty_output = '7-10 Days';
}else{
  $qty_output = $products_qty;
}



Cheers,
Mike
7 Mar 2010, 9:38 AM
#12
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

Thanks for this Glenn,

Which file, and where would it be added;.

It looks like it would be this file ?

/includes/templates/norts/templates/tpl_product_info_display.php

I couldn't say for sure without looking and/or experimenting, but it looks like a good file to start with.

Mike_Dean:

and would I have to amend the database to varchar or text as opposed to numerical for product_qty value..

No. not with Glenns' solution.

Mike_Dean:

Could I also use multiple instances of the Mod to account for multiple options of text ? eg

($products_qty == 9999) {
$qty_output = 'Available';
}else{
$qty_output = $products_qty;
}

($products_qty == 8888) {
$qty_output = 'Back-to-Back';
}else{
$qty_output = $products_qty;
}

($products_qty == 7777) {
$qty_output = '7-10 Days';
}else{
$qty_output = $products_qty;
}

Yes (but don't forget the 'if' statement -- eg

if ($products_qty == 8888) {
if ($products_qty == 7777) {

etc, etc.

A variant on this would be:

$qty_output = $products_qty;
if ($products_qty == 8888) $qty_output = 'Back-to-Back';
if ($products_qty == 7777) $qty_output = ' 7-10 Days'';

etc, etc.
This way you'll avoide a lot of 'else' statements.

Cheers
Rod

7 Mar 2010, 11:38 AM
#13
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

RodG:

I couldn't say for sure without looking and/or experimenting, but it looks like a good file to start with.

No. not with Glenns' solution.

Yes (but don't forget the 'if' statement -- eg

if ($products_qty == 8888) {
if ($products_qty == 7777) {

etc, etc.

A variant on this would be:

$qty_output = $products_qty;
if ($products_qty == 8888) $qty_output = 'Back-to-Back';
if ($products_qty == 7777) $qty_output = ' 7-10 Days'';

etc, etc.
This way you'll avoide a lot of 'else' statements.

Cheers
Rod

As always, thanks Rod,

I'll have to experiment and see exactly where in the file the code should be placed... Trial and error I guess...

Cheers,
Mike

7 Mar 2010, 12:13 PM
#14
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

I'll have to experiment and see exactly where in the file the code should be placed... Trial and error I guess...

Selecting a good place to trial will help reduce the chances of an error :-)

Look for a piece of code that looks something like the suggested changes ... IOW, look for other places in the code that reads something like
" $qty_output = something " and add your code into a suitable place just after.

There may be more than one place .. and possibly even more than one file, but the basic idea is the same, figure out the places where the $qty_output is set, then add your own code to modify the value before it is actually output.

Cheers
Rod

7 Mar 2010, 12:27 PM
#15
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

RodG:

Selecting a good place to trial will help reduce the chances of an error :-)

Look for a piece of code that looks something like the suggested changes ... IOW, look for other places in the code that reads something like
" $qty_output = something " and add your code into a suitable place just after.

There may be more than one place .. and possibly even more than one file, but the basic idea is the same, figure out the places where the $qty_output is set, then add your own code to modify the value before it is actually output.

Cheers
Rod

I cant find any instance in any file (using the dev toolkit in admin) that contains $qty_output, or $quantity_output ??

I have been playing around with this line in the tpl_product_info_display file

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

But no luck as yet...

7 Mar 2010, 1:20 PM
#16
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

I cant find any instance in any file (using the dev toolkit in admin) that contains $qty_output, or $quantity_output ??

I have been playing around with this line in the tpl_product_info_display file

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

But no luck as yet...

From the code you provided, the variable you are looking to change is
"$products_quantity"

So, somewhere before this piece of code will be a piece that reads (in part)
$products_quantity = ..........

You add your code somewhere between these two pieces.

The final result looking something like:

$products_quantity = .......... (original code)

(added code)
if ($products_quantity == 8888) $products_quantity = 'Back-to-Back';
if ($products_quantity == 7777) $products_quantity = ' 7-10 Days'';

(original code)

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

caveat: If "$products_quantity" is used later in the file it may cause a problem because you have changed from a numeric to a string (it is 'sloppy' programming, but php does allow it).

Hmmmmm..... perhaps a better idea would be this:

Locate the code that reads////
$products_quantity = ..........

Immediately after this (at a convenient point) , add this new code:

$products_out = $products_quantity ;
if ($products_out == 8888) $products_out = 'Back-to-Back';
if ($products_out == 7777) $products_out = ' 7-10 Days'';

Then change:

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

So it reads:

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_out . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

That should do it (and is probably closer to what Glenn was thinking with his mockup).

This method doesn't mess with the $products_quantity field either - it just introduces a new variable ($products_out) based on the original $products_quantity variable, modifies the new variable if required, then outputs (via the 'echo' command) the new variable instead of the original.

Cheers
Rod

7 Mar 2010, 1:38 PM
#17
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

Thanks Rod,

I'll give this a go when the eyes are not so blurry..! :ohmy:

Cheers,
Mike

7 Mar 2010, 1:53 PM
#18
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Change the Qty Available from Numerical to Text

Mike_Dean:

I have been playing around with this line in the tpl_product_info_display file

<?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?>

But no luck as yet...

This is mainly for general interest, but that line of code won't make a lot of sense to a lot of people - It can be deconstructed thus:

<?php // enable php processor // if (($flag_show_product_info_quantity == 1) and ( $products_quantity != "" )) { echo '<li> : ' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>'' ; } else { echo '') . "\n"; } ?> // disable php processor

Clever eh :-)

Cheers
Rod

7 Mar 2010, 1:57 PM
#19
mike_dean avatar

mike_dean

Totally Zenned

Join Date:
Feb 2006
Location:
Central Coast, NSW, Australia
Posts:
531
Plugin Contributions:
0

Re: Change the Qty Available from Numerical to Text

Couldn't hold off..!! :D

So close...

Took a few goes to get the right format and location

Just have the get the else statement right.

For any stock with a defined output, such as the 9999 etc it displays correctly,

*  Model: Verbatim 41839
* Stock Status : **Thanks Rod, you did it**
* Manufactured by: Verbatim

but other standard stock qtys then the whole product qty line disappears....

*  Model: Sony DPR55DLS1
* Manufactured by: Sony

This is what I ended up with

<?php $products_out = $products_quantity ; if ($products_out == 9999) $products_out = 'Thanks Rod, you did it'; if ($products_out == 7777) $products_out = '7-10 Days'; ?> <?php echo (($flag_show_product_info_quantity == 1 and $products_quantity) ? '<li> Stock Status : ' . $products_out . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?> <?php echo (($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name)) ? '<li>' . TEXT_PRODUCT_MANUFACTURER . $manufacturers_name . '</li>' : '') . "\n"; ?> </ul>

Correction....

Well done ol'boy..

Corrected code

<?php $products_out = $products_quantity ; if ($products_out == 9999) $products_out = 'Thanks Rod, you did it'; if ($products_out == 7777) $products_out = '7-10 Days'; ?> <?php echo (($flag_show_product_info_quantity == 1 ) ? '<li> Stock Status : ' . $products_out . TEXT_PRODUCT_QUANTITY . '</li>' : '') . "\n"; ?> <?php echo (($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name)) ? '<li>' . TEXT_PRODUCT_MANUFACTURER . $manufacturers_name . '</li>' : '') . "\n"; ?>

Hats off to you Rod... and of course Glenn for the beginnings of the code..

Your support is very much appreciated....

7 Mar 2010, 3:47 PM
#20
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Change the Qty Available from Numerical to Text

Good working-out of the code! This tweak with tests for different dummy quantity numbers should work very nicely for a finite and predefined set of possible text messages. It wouldn't automatically cover a new manufacturer text availability message, but if that is not a problem, it looks like you've got yourself covered.