Adding a shopping cart limitation with a warning message


#1


Adding gift card support on squarespace, rather than coupons
#2

Hey Cameron,

Glad to talk to you again! smile

A lot have changed since our last contact, such as now I'm using this open and public forum instead of Zendesk, for support tickets. I'm also starting to use GitHub for most of the code produced here. And prices gone up.

What have not changed, however, is how I do my work in general.

We never talked about sale products indeed, but my script should have worked with them no matter what and I take fault in that. In fact, while digging this up, I found it had a bug unrelated to sale items. If you set the preSelectedItems to anything and didn't add them, it would bring the alert. I'm very ashamed to admit it was all my fault again.

( by the way, that link asks for a password, so I couldn't really test on your end )

So, no matter if it was a quick fix or if it was just what you thought, I feel obligated to fix it. Basically, I just replaced this line:

  if (total > parseFloat(minTotal)) {

for this:

  if (total == 0 || total > parseFloat(minTotal)) {

Now I'll be testing this again see if I find anything else before you do. After all, I've got 2 strikes already! expressionless

And here's the full code:

<script name="limiting checkout" author="cregox.com">
  // if this list is empty, it will limit checkout for minTotal on the whole subtotal
  // values must be the precise HREF under the item on checkout
  var preSelectedItems = ['/shop/fourty-two', '/shop/ninety-one'];
  
  // if minTotal is 0 or less, there will be no warning
  var minTotal = '200.00';
  
  // customize the message at your will
  var warningMessage = 'For the selected items, you need to add a subtotal of at least $'+ minTotal +' and you only got $%total%';
  
  // code
  $(document).ready(function(){
    var checkout = $('div.checkout .checkout-button');
    checkout.css({'display': 'none'});
    $('<div class="checkout-button">Checkout</div>')
     .appendTo(checkout.parent()).click(function(){
      var total = 0;
      if (preSelectedItems && preSelectedItems.length > 0) {
        $.each(preSelectedItems, function(index, value){
          var addToTotal = $('div.cart-container .item-desc a[href="'+value+'"]').parents('tr').find('span.sqs-money-native').text();
          if ( addToTotal && !isNaN(parseFloat(addToTotal)) && isFinite(addToTotal) ) {
            total += parseFloat(addToTotal);
          }
        });
      } else {
        total = parseFloat($('div.subtotal span.sqs-money-native').text());
      }
      if (total == 0 || total > parseFloat(minTotal)) {
        checkout.trigger('click');
      } else {
        var warning = $('<div class="sqs-widgets-confirmation-content clear"><div class="title">Unable to Checkout</div><div class="message">'+ warningMessage.replace('%total%', total.toFixed(2)) +'</div><div class="buttons"><div class="confirmation-button no-frame confirm" tabindex="3">Okay</div></div></div>').appendTo(checkout.parent().parent());
        warning.css({'position':'absolute','right':'0px','width':'300px'});
        warning.ready(function(){
          warning.fadeIn('slow');
        });
        warning.find('.confirm').click(function(){
          warning.fadeOut('slow', function(){
            warning.remove();
          });
        });
      }
    });
  });
</script>

#3

Thanks I'll give this a try.


#4

This worked. Thanks. I've, noted your prices changed. I'll let you know when I have more projects in the future!


#5

I'm so glad it worked!

Since then I've been trying to come up with an automated way for testing this out and this is the best cost benefit option I could find so far: www.bugsnag.com

Still back in March I've applied it to my own site now and it is great, but it wouldn't have helped finding bugs such as those last 2 ones you've found, as they were highly unpredictable and, being more of a logic error, they don't really trigger any script error.

In theory, I should generate unit tests trying to picture every scenario possible and you may see the paradox there... They only existed in the first place because they came out of scenarios I haven't pictured!

In any case, just want to let you know I'm still looking for ways to improve this and if I ever find any, I'll make sure to come back and apply 'em to you.