1

I have a problem I can't find out myself so any help appreciated.

I have a dynamic cart which is filled with some JSON data. In the cart the shipping costs are also filled from the same json data. The script itself works when a method is available. When a shipping method isn't available for that country the script should say "not available" or something like that. Instead it returns an undefined error which causes the cart not to work. After some searching I found out that when an user comes from country A (which is supported by the cart) the variable "methods" is present in the Json string. When a user comes from country B (which isn't supported) the variable "methods" is not present in the json string. Obviously that causes the error. So what I try to achieve is to check if the variable "methods" is present or not. I found some answers like using typeof, HasOwnProperty, lenght() etc but I can't figure out how I should implement that in the script below.

My script

 <script type="text/javascript">

        function getPriceDynamicCart(price){
          price = parseFloat(price).toFixed(2);
            price = '€ ' + price.replace('.', ',');
          return price;
        }
        function loadDynamicCart(){
          var url = "{{ '/cart/' | url }}?format=json";

          $.getJSON(url,function(data){

            var cartHtml = '';
            var priceTotal = 0;
            var foundShipment = false;

             $.each(data.cart.products, function(index, product){                  

              priceTotal = priceTotal + parseFloat(product.price.price_incl);
              productTitleLimit = jQuery.trim(product.fulltitle).substring(0, 19);

              cartHtml = cartHtml +
                '<div class="cart-product"><div class="cart-quantity">' +product.quantity+'&nbsp;x&nbsp;</div><div class="cart-title"><a href="'+ product.url +'" alt="'+ product.fulltitle + '" title="'+ product.fulltitle + '">' + productTitleLimit + '</a></div><div class="cart-price">' + getPriceDynamicCart(product.price.price_incl) + '</div></div>';
            });

            $.each(data.cart.shipping.methods, function(index, method){

              if(!foundShipment && !method.cod && !method.pickup) {
                priceTotal = priceTotal + parseFloat(method.price.price_incl);
                cartHtml = cartHtml +
                  '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price"> ' + getPriceDynamicCart(method.price.price_incl) + '</div></div>';

                foundShipment = true;                
              }
            });

            cartHtml = cartHtml +
              '<div class="cart-total"><div class="total">Totaal(incl. btw): <span>' + getPriceDynamicCart(priceTotal) + '</span></div>';

            $('#dynamic_cart').html(cartHtml);

          });
        }

        $().ready(function(){
          loadDynamicCart();
        });
      </script>
1
  • I don't want to mark as an exact duplicate... But here is an answer that displays how to test a value against undefined. Javascript isset() equivalent Commented Nov 6, 2012 at 17:23

2 Answers 2

3
if(data.cart.shipping.methods !== undefined){

    $.each(data.cart.shipping.methods, function(index, method){

          if(!foundShipment && !method.cod && !method.pickup) {
            priceTotal = priceTotal + parseFloat(method.price.price_incl);
            cartHtml = cartHtml +
              '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price"> ' + getPriceDynamicCart(method.price.price_incl) + '</div></div>';

            foundShipment = true;                
          }
        });
}else{
  cartHtml = cartHtml +
              '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price">**Anything you care to write**</div></div>';
}

That should work.

Edit, I've added an else statement for setting the cartHtml variable, so you have more flexibility for future use.

Sign up to request clarification or add additional context in comments.

3 Comments

This simple?! It seems to work... you know how long I've been searching for a solution... Thx you made my day!
Sorry one last question. This says "if not undefined"? How do I add code that "replaces" +getPriceDynamicCart+ with something like "not available"?
Edited my answer, I've used an else statement, so you can be a lot more flexible with what you might do in future. You might want to use a function when the property is undefined, for example.
0

You can check the data.cart.shipping.methods property isn't null / undefined before you call each:

if (data.cart.shipping.methods) {
    $.each(data.cart.shipping.methods, function(index, method){
    //...
    }
}

1 Comment

Shouldn't you check for undefined and not null? Functionally identical in this case since they're both falsy, but still.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.