Calculate RAL Upsells

by karlovac

HTML

<div id="output">

</div>

CSS

#output {
  font-size: 24px;
}

JavaScript

var dtgRecipeId = '61411982'; // DTG
var cyoRecipeId = '97504925'; // CYO
var dtgBasePrice = 1299; // DTG This needs to come from the server
var cyoBasePrice = 1399; // CYO
var currency = 'RMB';

function isDtg(vendorId) {
	return vendorId.indexOf('dtg') > -1;
}

function checkPrice(recipeId, basePrice, callback) {
  var recipeUrl = 'https://api.fluidretail.net/recipe/'+recipeId+'?api-key=R8lp7_l6U7en-K1C4XrWRYgdjY4bPzVlk&locale=en_us&include=localized_configuration,extended_attributes';
  // Remember that within China, the recipe URL will be:
	// http://cdn-prod.fluidconfigure.cn/api/recipe/91002197?api-key=R8lp7_l6U7en-K1C4XrWRYgdjY4bPzVlk&locale=en_us&include=localized_configuration,extended_attributes


  $.ajax({
    url: recipeUrl,
    beforeSend: function( xhr ) {
      xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
    }
  })
    .done(function( data ) {
      var recipe = JSON.parse(data);
      console.log('data: ', recipe);
      var totalPrice = basePrice;
      if (!isDtg(recipe.product.vendor_id)) {
        // CYO Products - calculate total price based on upsells
        for (var alias in recipe.extended_attributes) {
          var attribute = recipe.extended_attributes[alias];
          var valueVendorId = attribute.value !== false ? attribute.value.info.vendorIds : '-';

          var valuePrice = 0;
          // Skip the size because that is where the SKU lives, and it has a hard-coded "$1" price, whereas in the actual calculation we use the SKU price
          if (alias !== 'size' && attribute.value !== false  && attribute.value.pricing.upcharge !== 0) {
            valuePrice = attribute.value.pricing.upcharge[currency];
            totalPrice += valuePrice;
          }

          console.log('Attribute ' + alias + ' has value ' + valueVendorId + ' and the price is $' + valuePrice);
        }
      } else {
        // DGG Products - assume fixed upsell of ¥300
        var dtgUpsell = 300;
        totalPrice += dtgUpsell;
      }
...