Zen Cart Logo
If this is your first visit, check our documentation. To post, register or login.
Forums / Setting Up Your Zen Cart Products / Setting Up Categories, Products, Attributes / My hack to make attributes appear 'out of stock' instead of deleting them

My hack to make attributes appear 'out of stock' instead of deleting them

8 Feb 2012, 10:36 AM
#1
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Post #1 by dgent

I, like many people have wanted attributes to be able to be 'switched off' instead of having to delete them, and re-install them when a product in a certain size or spec is out of stock, or have it labelled 'out of stock' so customers know that options may be available in the future.

Well, this was never possible, but I've put a hack on my own sites which allows this is happen in a roundabout way.

  1. Add this following script to your html_header.php file.
<script language="javascript" type="text/javascript"> function replaceWord(control, oldWord, newWord) { var pageElement = document.getElementById(control); oldWord = new RegExp(oldWord, "g"); if (pageElement.value) pageElement.value = pageElement.value.replace(oldWord, newWord); else pageElement.innerHTML = pageElement.innerHTML.replace(oldWord, newWord); } </script> <body onload="replaceWord('productAttributes', '£999.00', 'Out Of Stock')" />

(Note above : Make the currency above match the one on your site, mine is UK hence the £'

  1. Mark your attributes you want to appear out of stock, as 'display only' (so they cannot be added to basket)

  2. Make the add on price of that attribute '999'

Thats it. What this javascript does is string replace text on the screen, so when it detects £999.00 it swaps the text for 'Out of stock'

So now when the customer views the drop down menu, it lists the size/spec with 'Out of stock' next to it, and it cannot be added to basket. To make it in stock again, simply mark as not display only and remove the price markup.

8 Feb 2012, 5:40 PM
#3
gjh42 avatar

gjh42

Black Belt

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

Post #3 by gjh42

Looks good!
The next step is to put the javascript in a separate file like /includes/templates/your_template/jscript/jscript_attrib_stock.js, so it will be loaded automatically without editing html_header.php.

There is an automatic method for getting the <body onload=, too. I would have to go look that up, possibly in the wiki.

9 Feb 2012, 9:49 AM
#4
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Post #4 by dgent

Now Ive just noticed an issue...of course if you have multiple currencies it won't work because the £ will change to a $ and won't match obviously..

Is there anyway of having multiple versions of a string to check and replace?

(EDIT) You can just have the string as 999.00 instead of £999.99 but then it will say '£Out of Stock'

9 Feb 2012, 10:18 AM
#5
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Post #5 by dgent

Hmm, this will only work if you only use one currency, as the '999' will change to an alternate amount when you change currency.

Again, if you can do multiple 'or' strings, and work out what 999 would convert into (if you are using fixed currency conversions) it wouldnt be an issue.

9 Feb 2012, 6:30 PM
#6
gjh42 avatar

gjh42

Black Belt

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

Post #6 by gjh42

I'm sure javascript has a way to do alternate substitutions, though I don't know how.
If you make the saved filename like jscript_attrib_stock.php, you can do PHP processing to look up the active currency and language, and set the search and replacement strings accordingly before the js runs to do its work on the page output.