From 258559f9d0d17fc2ab357e884cc4fad5a88cd179 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 1 Jul 2012 10:07:08 +1000 Subject: [PATCH] Update the price on the product details page in real time when the variant or the quantity are changed --- app/assets/javascripts/store/products.js | 38 +++++++++++++++++++ .../spree/products_helper_decorator.rb | 3 +- spec/helpers/products_helper_spec.rb | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/store/products.js diff --git a/app/assets/javascripts/store/products.js b/app/assets/javascripts/store/products.js new file mode 100644 index 0000000000..ccf5293bf4 --- /dev/null +++ b/app/assets/javascripts/store/products.js @@ -0,0 +1,38 @@ +/** + * Update the price on the product details page in real time when the variant or the quantity are changed. + **/ + +$(document).ready(function() { + // Product page with variant choice + $("#product-variants input[type='radio']").change(products_update_price_with_variant); + $("#quantity").change(products_update_price_with_variant); + $("#quantity").change(); + + // Product page with master price only + $(".add-to-cart input.title:not(#quantity)").change(products_update_price_without_variant).change(); +}); + + +function products_update_price_with_variant() { + var variant_price = $("#product-variants input[type='radio']:checked").parent().find("span.price").html(); + variant_price = variant_price.substr(2, variant_price.length-3); + + var quantity = $("#quantity").val(); + + $("#product-price span.price").html("$"+(parseFloat(variant_price) * parseInt(quantity)).toFixed(2)); +} + + +function products_update_price_without_variant() { + var master_price = $("#product-price span.price").data('master-price'); + if(master_price == null) { + // Store off the master price + master_price = $("#product-price span.price").html(); + master_price = master_price.substr(1, master_price.length-2); + $("#product-price span.price").data('master-price', master_price); + } + + var quantity = $(this).val(); + + $("#product-price span.price").html("$"+(parseFloat(master_price)*parseInt(quantity)).toFixed(2)); +} diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index 7c17b71a36..8c7b7fbe54 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -1,8 +1,7 @@ module Spree ProductsHelper.class_eval do - # Return the price of the variant, or nil if it is identical to the master price + # Return the price of the variant def variant_price_diff(variant) - return nil if variant.price == variant.product.price "(#{number_to_currency variant.price})" end end diff --git a/spec/helpers/products_helper_spec.rb b/spec/helpers/products_helper_spec.rb index bc55cc208d..a6e16bab55 100644 --- a/spec/helpers/products_helper_spec.rb +++ b/spec/helpers/products_helper_spec.rb @@ -11,7 +11,7 @@ module Spree it "displays variant price differences as absolute, not relative values" do variant = make_variant_stub(10.00, 10.00) - subject.variant_price_diff(variant).should be_nil + subject.variant_price_diff(variant).should == "($10.00)" variant = make_variant_stub(10.00, 15.55) subject.variant_price_diff(variant).should == "($15.55)"