app/assets/javascripts/caboose/product.js in caboose-cms-0.5.128 vs app/assets/javascripts/caboose/product.js in caboose-cms-0.5.129

- old
+ new

@@ -15,10 +15,12 @@ // self.initialize = function() { self.$product = $('#product'); self.$price = self.$product.find('#product-price'); + $("<span id='percent-off'></span").insertAfter(self.$price); + $("<span id='sale-price'></span").insertBefore(self.$price); if (!self.$product.length) return false; $.get('/products/' + self.$product.data('id') + '/info', function(response) { self.product = response.product; self.option1_values = response.option1_values; @@ -262,10 +264,38 @@ self.set_variant = function(variant) { self.variant = variant; Caboose.Store.Modules.Cart.set_variant(variant); if (variant) self.set_image_from_variant(variant); if (variant && self.$price.length) self.$price.empty().text('$' + parseFloat((variant.price * 100) / 100).toFixed(2)); + if (self.variant_on_sale(variant)) { + self.$price.addClass("on-sale"); + var percent = 100 - ((variant.sale_price / variant.price) * 100).toFixed(0); + $("#percent-off").text("SALE! Save " + percent + "%"); + $("#sale-price").text('$' + parseFloat((variant.sale_price * 100) / 100).toFixed(2)); + } + else { + self.$price.removeClass("on-sale"); + $("#percent-off").text(''); + $("#sale-price").text(''); + } }; + + self.variant_on_sale = function(variant) { + var is_on_sale = false; + if (variant.sale_price != "" && variant.sale_price != 0) { + var d = new Date(); + if (variant.date_sale_starts && d < variant.date_sale_starts) { + is_on_sale = false; + } + else if (variant.date_sale_ends && d > variant.date_sale_ends) { + is_on_sale = false; + } + else { + is_on_sale = true; + } + } + return is_on_sale; + } self.get_variant = function(id) { return _.find(self.product.variants, function(variant) { return variant.id == (id || self.variant.id) }); };