Update the price on the product details page in real time when the variant or the quantity are changed

This commit is contained in:
Rohan Mitchell
2012-07-01 10:07:08 +10:00
parent af05448dd8
commit 258559f9d0
3 changed files with 40 additions and 3 deletions

View File

@@ -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));
}

View File

@@ -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

View File

@@ -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)"