Limit one per order

General ShopSite user discussion

Limit one per order

Postby JCGrafted » Mon Nov 07, 2011 9:56 am

We have recently added an e-book to our store. We had a customer accidentally order 9 of them instead of 1.

I use the "Order Anywhere" option and build my own pages. I changed the input type to check box instead of text, thus allowing only 1 to be submitted. This does not completely solve the problem. The customer can still change the quantity in the shopping cart of can choose the item to submit a 2nd time. I want to limit the product to 1 per order.

I realize that you cannot program out all user errors (you can't fix stupid), but I try to get as close as I can.

Any suggestions?
JCGrafted
 
Posts: 29
Joined: Wed Sep 21, 2011 2:41 pm
Location: Houston, TX

Re: Limit one per order

Postby robm » Mon Nov 07, 2011 11:12 am

If you have ShopSite Pro, we have a tutorial that addresses this specific issue: limiting a product to a maximum quantity:

http://support.lexiconn.com/news/viewtopic.php?t=415

Rob
robm
 
Posts: 463
Joined: Fri Aug 04, 2006 5:46 pm
Location: Connecticut

Re: Limit one per order

Postby JCGrafted » Tue Nov 08, 2011 11:56 am

I am running ShopSite Pro version 10 sp1. I tried the code in the example and it did not work. My code that I added was:
Code: Select all
ns_count = 0;

for(ns=0; ns<ss_sku.length; ns++)
{
   if(ss_sku[ns]=='SAAB009a')
   {
        ns_count+= ss_quantity[ns];
   }
}

if (button == "8")
{
  if(ns_count > 1)
  {
    alert("You may only select one e-book per order");
    return false;
  }
}


It did nothing. What am I doing wrong?
JCGrafted
 
Posts: 29
Joined: Wed Sep 21, 2011 2:41 pm
Location: Houston, TX

Re: Limit one per order

Postby robm » Tue Nov 08, 2011 12:11 pm

I went to your site, added the e-book, changed the quantity to 2, pressed update, and when I clicked "Continue Checkout" it did not let me proceed, giving me a JavaScript warning "You may only select one e-book per order". However, if they change the quantity and then immediately click checkout, it bypasses this JS check.

It will work if they add multiple items of the same restricted product.

You can disable the ability to change the quantity in the cart to stop this workaround:
Commerce Setup -> Order System -> Shopping Cart -> "Quantity can not be changed"

Rob
robm
 
Posts: 463
Joined: Fri Aug 04, 2006 5:46 pm
Location: Connecticut

Re: Limit one per order

Postby JCGrafted » Tue Nov 08, 2011 4:59 pm

Thanks! I did not think to press the "Continue Checkout" button. I thought the code would not allow the extra e-book to be added to the cart at all. I am perfectly happy with how this works.

I did not know you could add JavaScript to the cart. This is very good news. Is it a new feature or have I just missed it. I have been looking for JavaScript for the cart for some time.

This could passably solve another problem I have been having for quite awhile. I will need to ask for help with it, but will do so in another post so it is more searchable by others who may have the same problem.

My one suggestion is that your development team might consider an option to include a .js file for those who need to add a lot of JavaScript.

Thanks again.
JCGrafted
 
Posts: 29
Joined: Wed Sep 21, 2011 2:41 pm
Location: Houston, TX

Re: Limit one per order

Postby IrongateE » Tue Aug 06, 2013 2:06 pm

Rob, I would suggest a different work-around.

I utilized javascript to force the cart to do an update of the contents every time a quantity was changed or the shipping option was changed. This way it's impossible to change the quantity and bypass the check, but users are still able to change the quantity in their cart. Just add the following javascript into the bottom of your shopping cart HTML template.

Code: Select all
<script type='text/javascript'>
   /**
   *   triggers a javascript HTML DOM event programatically
   *   @param   HTMLElement   ele   the element on which to trigger an event
   *   @param   string   type   the event type to be triggered
   *   @author   Glen
   */
   function triggerEvent(ele, type){
      if ((ele[type] || false) && typeof ele[type] == 'function'){
         ele[type](ele);
      }
   }
   var update_button = document.getElementsByClassName('button7')[0];
   if(typeof update_button != 'undefined'){
      var update_inputs = [];
      var cart_qtys = document.getElementsByClassName('cart_quantity');
      for(var i=0, max=cart_qtys.length; i<max; i++){
         var input = cart_qtys[i].getElementsByTagName('input')[0];
         update_inputs.push(input);
      }
      input = document.forms['order'].elements['shipping'];
      update_inputs.push(input);
      for(var i=0, max=update_inputs.length; i<max; i++){
         if(typeof update_inputs[i] != 'undefined'){
            if(update_inputs[i].addEventListener){
               update_inputs[i].addEventListener('change', function(){triggerEvent(update_button, 'click');} );
            }else if(update_inputs[i].attachEvent){
               update_inputs[i].attachEvent('change', function(){triggerEvent(update_button, 'click');} );
            }
         }
      }
   }
</script>


Hope that helps,

Glen
Last edited by IrongateE on Tue Aug 06, 2013 2:43 pm, edited 1 time in total.
IrongateE
 
Posts: 4
Joined: Tue May 14, 2013 2:41 pm

Re: Limit one per order

Postby robm » Tue Aug 06, 2013 2:35 pm

Glen,

Very nice! I'll check it out. :)

Rob
robm
 
Posts: 463
Joined: Fri Aug 04, 2006 5:46 pm
Location: Connecticut

Re: Limit one per order

Postby paultim » Wed Aug 14, 2013 9:47 pm

Dear if they change the quantity and then immediately click checkout, it bypasses this JS check.
Smile plzZZzz....(Usman Malik)…!!!
paultim
 
Posts: 1
Joined: Wed Aug 14, 2013 9:41 pm

Re: Limit one per order

Postby IrongateE » Thu Aug 15, 2013 9:21 am

Paultim,

There must be some other factor that is different between my production environment and yours that is causing the check to not function correctly. Perhaps you are using a different version of ShopSite? (I am using Pro 11 sp2 r4). When I change the cart quantity in my installation and then immediately press the 'checkout' button the check will occur and the user will stay on the shopping cart page until they press the 'checkout' button again without changing any quantities.

I could take a guess and say that your version may not be running the check if the input box does not lose focus (user clicks out of the input field). If you wanted to try an alternative approach, you could change the javascript code to trigger an update whenever the contents of the input field changed by replacing the addEventListener / attachEvent functions with the code below:

Code: Select all
if(typeof update_inputs[i] != 'undefined'){
   if(update_inputs[i].addEventListener){
       update_inputs[i].addEventListener('input', function(){triggerEvent(update_button, 'click');} );
   }else if(update_inputs[i].attachEvent){
       update_inputs[i].attachEvent('input', function(){triggerEvent(update_button, 'click');} );
   }
}


Or you could try replacing 'input' above with 'keyup'. However, I cannot recommend either of these tactics, as this code will cause the check to occur every time a customer changes the text content of the input box. This might give you some very frustrated customers. For example, hitting the backspace key will cause a check to occur and reset the quantity. Because of that, the only way you can change quantities in the shopping cart page is to highlight the quantity number and then press the number you want. Also, quantities can't be set above 9, as a check will occur after the customer presses the '1' key if they are trying to set the quantity to '10'.

I can recommend installing Firebug in Firefox and taking a look at the 'console' tab while you are on your webpage. If something is breaking the javascript code, an error message will show up that will help you troubleshoot the issue as a javascript error or possibly a difference between ShopSite versions.

Hope that helps,

Glen
IrongateE
 
Posts: 4
Joined: Tue May 14, 2013 2:41 pm


Return to User Forum

Who is online

Users browsing this forum: No registered users and 77 guests

cron