getShippingChoice (JS API v2)

Version 2 API only.

getShippingChoice

Method Signature

ShippingEstimate ultraCart.getShippingChoice();

Description

Retrieves the currently selected shipping choice. This method is commonly used when rendering the subtotal section of a checkout.

Result

a ShippingEstimate object for the currently selected shipping method, or null if no choice has been made

/**
 * updates the summary block showing subtotal, tax, shipping, and total
 */
function renderSummary() {
  var c = ultraCart.getCart();
  if (c.items.length == 0) {
    jQuery('#summaryContainer').hide();
    return;
  } else {
    jQuery('#summaryContainer').show();
  }

  // shipping must be dealt with specially.  to prevent customers from gaming the system, there are several instances
  // where the price of shipping is reset.  So, when rendering, always lookup the shipping method and calculate the cost
  // from that rather than using the cart.shippingHandlingWithDiscount, although the latter would be simpler

  var totalTax = c.tax;
  var shippingTotal = ' '; // don't display anything if there's no choice
  var total = c.subtotalWithDiscount + c.tax;

  var shippingChoice = ultraCart.getShippingChoice();
  if (shippingChoice) {
    totalTax += shippingChoice.tax;
    if (shippingChoice.cost == 0) {
      shippingTotal = '<strong>FREE Shipping!</strong>';
    } else {
      shippingTotal = nf.toCurrency(shippingChoice.cost);
    }
    total += shippingChoice.cost;
    total += shippingChoice.tax;
  }


  jQuery('#summarySubtotal').html("<div class='summaryLabel'>Subtotal:<\/div><div class='summaryField'>" + nf.toCurrency(c.subtotalWithDiscount) + "<\/div>");
  jQuery('#summaryTax').html("<div class='summaryLabel'>Tax:<\/div><div class='summaryField'>" + (totalTax == 0 ? "<span class='tax'>No Sales Tax!</span>" : nf.toCurrency(totalTax)) + "<\/div>");
  jQuery('#summaryShipping').html("<div class='summaryLabel'>Shipping:<\/div><div class='summaryField'>" + shippingTotal + "<\/div>");
  jQuery('#summaryTotal').html("<div class='summaryLabel'>Total:<\/div><div class='summaryField'>" + nf.toCurrency(total) + "<\/div>");
}