I have basic e-commerce website selling clothing, but this image:
When someone select an 'out of stock' item like shown i want to update the 'add to bag' button to another button and link to send an email or something.
Here's the bit of js I have to check stock and colours etc:
<script type="text/javascript">
$(function(){
$('.colors').on('change', function(e){
var cat_id = e.target.value;
var current = $(this).data("index");
var select = $("select[data-index='"+current+"'][class='sizes']");
$.get('/related-sizes?info=' + cat_id, function(data){
select.empty();
$.each(data, function(index, subcatObj){
select.append('<option value="'+subcatObj.size+'">'+subcatObj.size+'</option>');
});
});
});
var a = new Array();
$(".colors").children("option").each(function(x){
test = false;
b = a[x] = $(this).val();
for (i=0;i<a.length-1;i++){
if (b ==a[i] && b != '') test =true;
}
if (test) $(this).remove();
});
});
</script>
So how can I do a check to see if it states "out of stock" then change the "add to bag" button to "email us" and send that to an email address?
Hi,
you can catch click on "Out of stock" option in $.colors.change. In this function you can add condition, if selected value == Out of stock. And if yes, $(a button-element).text('send email ...').attr('href', 'change url').
e.g:
<script type="text/javascript">
$(function(){
$('.colors').on('change', function(e){
var cat_id = e.target.value;
var current = $(this).data("index");
var select = $("select[data-index='"+current+"'][class='sizes']");
if(select == xxx) {
$('.butt').text('Send email').attr('href', '/');
} else {
// default button
}
$.get('/related-sizes?info=' + cat_id, function(data){
select.empty();
$.each(data, function(index, subcatObj){
select.append('<option value="'+subcatObj.size+'">'+subcatObj.size+'</option>');
});
});
});
var a = new Array();
$(".colors").children("option").each(function(x){
test = false;
b = a[x] = $(this).val();
for (i=0;i<a.length-1;i++){
if (b ==a[i] && b != '') test =true;
}
if (test) $(this).remove();
});
});
</script>
Sorry also should have stated that this button is within a from so want the add to bag to do the form submit, otherwise if out ot stock show button but with a link to mailto.
if($(this).find('option:selected').text() == 'Out of Stock') {
$('button.atb').text('Send an email').onClick(function(e) {
e.preventDefaut();
windows.location.replace('/url-to-send-email');
});
} else {
$('button.atb').text('Add to Bag').onClick(function(e) {
$(form).submit(); // You can submit only specific form (e.g. by class)
});
}
and HTML:
<button class="btn btn-success atb">Add to Bag</button></p>
if($(this).find('option:selected').text() == 'Out of Stock') {
$('button.oos').text('Send an email').onClick(function(e) {
e.preventDefaut();
windows.location.replace('/url-to-send-email');
});
} else {
$('button.oos').text('Add to Bag').onClick(function(e) {
$('.addToCart').submit(); // Added class to form
});
}
Button <button class="btn btn-success oos">Add to Bag</button>
No error but after playing around I put this console.log($(this).find('option:selected').text()); which does show in console as Out of Stock, so not sure whats going on here.
Seems to be something with this check if($(this).find('option:selected').text() == 'Out of Stock') {
As I put some logs in there to test
if($(this).find('option:selected').text() == 'Out of Stock') {
console.log('Yeah! its out of stock');
$(".oos").hide();
$(".se").show();
} else {
console.log('Not out of stock...continue');
$(".se").hide();
$(".oos").show();
}
and when selecting out of stock the console shows the following:
$(function(){
$(".se").hide();
$('.colors').on('change', function(e){
var cat_id = e.target.value;
var current = $(this).data("index");
var select = $("select[data-index='"+current+"'][class='sizes']");
$.get('/related-sizes?info=' + cat_id, function(data){
select.empty();
$.each(data, function(index, subcatObj){
select.append('<option value="'+subcatObj.size+'">'+subcatObj.size+'</option>');
});
});
//console.log($(this).find('option:selected').val());
if($(this).find('option:selected').text() == 'Out of Stock') {
console.log('Yeah! its out of stock');
$(".oos").hide();
$(".se").show();
} else {
console.log('Not out of stock...continue');
$(".se").hide();
$(".oos").show();
}
});
var a = new Array();
$(".colors").children("option").each(function(x){
test = false;
b = a[x] = $(this).val();
for (i=0;i<a.length-1;i++){
if (b ==a[i] && b != '') test =true;
}
if (test) $(this).remove();
});
});
if($(this).find('option:selected').val() == '612-Black') {
console.log('Yeah! its out of stock');
$(".oos").hide();
$(".se").show();
} else {
console.log('Not out of stock...continue');
$(".se").hide();
$(".oos").show();
}
}