woocommerce/assets/js/woocommerce.js

578 lines
18 KiB
JavaScript
Raw Normal View History

2011-09-08 09:52:18 +00:00
jQuery(document).ready(function($) {
2011-08-09 15:16:18 +00:00
if (woocommerce_params.option_ajax_add_to_cart=='yes') {
// Ajax add to cart
$('.add_to_cart_button').live('click', function() {
// AJAX add to cart request
var thisbutton = $(this);
if (thisbutton.is('.product_type_simple, .product_type_downloadable, .product_type_virtual')) {
$(thisbutton).addClass('loading');
2011-08-30 11:43:16 +00:00
var data = {
action: 'woocommerce_add_to_cart',
product_id: $(thisbutton).attr('rel'),
security: woocommerce_params.add_to_cart_nonce
};
2011-08-24 19:32:33 +00:00
// Trigger event
$('body').trigger('adding_to_cart');
2011-08-24 19:32:33 +00:00
// Ajax action
$.post( woocommerce_params.ajax_url, data, function(response) {
$(thisbutton).removeClass('loading');
// Get response
data = $.parseJSON( response );
if (data.error) {
alert(data.error);
return;
}
fragments = data;
// Block fragments class
if (fragments) {
$.each(fragments, function(key, value) {
$(key).addClass('updating');
});
}
// Block widgets and fragments
$('.widget_shopping_cart, .shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({message: null, overlayCSS: {background: 'transparent url(' + woocommerce_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
// Changes button classes
$(thisbutton).addClass('added');
// Cart widget load
if ($('.widget_shopping_cart').size()>0) {
$('.widget_shopping_cart:eq(0)').load( window.location + ' .widget_shopping_cart:eq(0) > *', function() {
// Replace fragments
if (fragments) {
$.each(fragments, function(key, value) {
$(key).replaceWith(value);
});
}
// Unblock
$('.widget_shopping_cart, .updating').css('opacity', '1').unblock();
} );
} else {
2011-09-14 14:55:53 +00:00
// Replace fragments
if (fragments) {
$.each(fragments, function(key, value) {
$(key).replaceWith(value);
});
}
// Unblock
$('.widget_shopping_cart, .updating').css('opacity', '1').unblock();
2011-08-24 19:32:33 +00:00
}
// Cart page elements
$('.shop_table.cart').load( window.location + ' .shop_table.cart:eq(0) > *', function() {
$("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass('buttons_added').append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
$('.shop_table.cart').css('opacity', '1').unblock();
});
2011-08-24 19:32:33 +00:00
$('.cart_totals').load( window.location + ' .cart_totals:eq(0) > *', function() {
$('.cart_totals').css('opacity', '1').unblock();
});
2011-08-24 19:32:33 +00:00
// Trigger event so themes can refresh other areas
$('body').trigger('added_to_cart');
});
return false;
} else {
return true;
}
});
}
2011-08-17 23:42:07 +00:00
// Orderby
2011-09-08 09:52:18 +00:00
$('select.orderby').change(function(){
$(this).closest('form').submit();
2011-08-17 23:42:07 +00:00
});
2011-08-09 15:16:18 +00:00
// Star ratings
2011-09-08 09:52:18 +00:00
$('#rating').hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>');
2011-08-09 15:16:18 +00:00
2011-09-08 09:52:18 +00:00
$('p.stars a').click(function(){
2011-11-04 18:58:15 +00:00
var star = $(this);
$('#rating').val( star.text() );
2011-09-08 09:52:18 +00:00
$('p.stars a').removeClass('active');
2011-11-04 18:58:15 +00:00
star.addClass('active');
2011-08-09 15:16:18 +00:00
return false;
});
// Price slider
2011-09-08 09:52:18 +00:00
var min_price = $('.price_slider_amount #min_price').val();
var max_price = $('.price_slider_amount #max_price').val();
2011-08-09 15:16:18 +00:00
2011-11-04 18:58:15 +00:00
current_min_price = parseInt(min_price);
current_max_price = parseInt(max_price);
2011-08-09 15:16:18 +00:00
2011-11-04 18:58:15 +00:00
if (woocommerce_params.min_price) current_min_price = parseInt(woocommerce_params.min_price);
if (woocommerce_params.max_price) current_max_price = parseInt(woocommerce_params.max_price);
2011-08-17 23:42:07 +00:00
2011-09-08 09:52:18 +00:00
$('.price_slider').slider({
2011-08-09 15:16:18 +00:00
range: true,
2011-08-17 23:42:07 +00:00
animate: true,
2011-08-09 15:16:18 +00:00
min: min_price,
max: max_price,
2011-08-17 23:42:07 +00:00
values: [current_min_price,current_max_price],
2011-08-09 15:16:18 +00:00
create : function( event, ui ) {
2011-08-27 11:57:06 +00:00
2011-08-27 19:20:28 +00:00
if (woocommerce_params.currency_pos == "left"){
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( woocommerce_params.currency_symbol + current_min_price + " - " + woocommerce_params.currency_symbol + current_max_price );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "left_space") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( woocommerce_params.currency_symbol + " " + current_min_price + " - " + woocommerce_params.currency_symbol + " " + current_max_price );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "right") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( current_min_price + woocommerce_params.currency_symbol + " - " + current_max_price + woocommerce_params.currency_symbol );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "right_space") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( current_min_price + " " + woocommerce_params.currency_symbol + " - " + current_max_price + " " + woocommerce_params.currency_symbol );
2011-08-27 11:57:06 +00:00
}
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount #min_price" ).val(current_min_price);
$( ".price_slider_amount #max_price" ).val(current_max_price);
2011-08-09 15:16:18 +00:00
},
slide: function( event, ui ) {
2011-08-27 11:57:06 +00:00
2011-08-27 19:20:28 +00:00
if (woocommerce_params.currency_pos == "left"){
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( woocommerce_params.currency_symbol + ui.values[ 0 ] + " - " + woocommerce_params.currency_symbol + ui.values[ 1 ] );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "left_space") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( woocommerce_params.currency_symbol + " " + ui.values[ 0 ] + " - " + woocommerce_params.currency_symbol + " " + ui.values[ 1 ] );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "right") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( ui.values[ 0 ] + woocommerce_params.currency_symbol + " - " + ui.values[ 1 ] + woocommerce_params.currency_symbol );
2011-08-27 19:20:28 +00:00
} else if (woocommerce_params.currency_pos == "right_space") {
2011-09-08 09:52:18 +00:00
$( ".price_slider_amount span" ).html( ui.values[ 0 ] + " " + woocommerce_params.currency_symbol + " - " + ui.values[ 1 ] + " " + woocommerce_params.currency_symbol );
2011-08-27 11:57:06 +00:00
}
2011-09-08 09:52:18 +00:00
$( "input#min_price" ).val(ui.values[ 0 ]);
$( "input#max_price" ).val(ui.values[ 1 ]);
2011-08-09 15:16:18 +00:00
}
});
// Quantity buttons
2011-09-08 09:52:18 +00:00
$("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass('buttons_added').append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
2011-08-19 20:11:04 +00:00
2011-11-04 18:58:15 +00:00
$(".plus").live('click', function() {
2011-09-08 09:52:18 +00:00
var currentVal = parseInt($(this).prev(".qty").val());
2011-08-09 15:16:18 +00:00
if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 0;
2011-09-08 09:52:18 +00:00
$(this).prev(".qty").val(currentVal + 1);
2011-08-09 15:16:18 +00:00
});
2011-11-04 18:58:15 +00:00
$(".minus").live('click', function() {
2011-09-08 09:52:18 +00:00
var currentVal = parseInt($(this).next(".qty").val());
if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 1;
2011-11-07 11:41:52 +00:00
if (currentVal > 0) $(this).next(".qty").val(currentVal - 1);
2011-08-09 15:16:18 +00:00
});
/* states */
2011-08-24 19:35:02 +00:00
var states_json = woocommerce_params.countries.replace(/&quot;/g, '"');
2011-09-08 09:52:18 +00:00
var states = $.parseJSON( states_json );
2011-08-09 15:16:18 +00:00
2011-09-08 09:52:18 +00:00
$('select.country_to_state').change(function(){
2011-08-09 15:16:18 +00:00
2011-09-08 09:52:18 +00:00
var country = $(this).val();
var state_box = $('#' + $(this).attr('rel'));
2011-08-09 15:16:18 +00:00
2011-09-08 09:52:18 +00:00
var input_name = $(state_box).attr('name');
var input_id = $(state_box).attr('id');
2011-08-09 15:16:18 +00:00
if (states[country]) {
var options = '';
var state = states[country];
for(var index in state) {
options = options + '<option value="' + index + '">' + state[index] + '</option>';
}
2011-09-08 09:52:18 +00:00
if ($(state_box).is('input')) {
2011-08-09 15:16:18 +00:00
// Change for select
2011-09-08 09:52:18 +00:00
$(state_box).replaceWith('<select name="' + input_name + '" id="' + input_id + '"><option value="">' + woocommerce_params.select_state_text + '</option></select>');
state_box = $('#' + $(this).attr('rel'));
2011-08-09 15:16:18 +00:00
}
2011-11-01 18:27:25 +00:00
$(state_box).html(options);
2011-08-09 15:16:18 +00:00
} else {
2011-09-08 09:52:18 +00:00
if ($(state_box).is('select')) {
$(state_box).replaceWith('<input type="text" placeholder="' + woocommerce_params.state_text + '" name="' + input_name + '" id="' + input_id + '" />');
state_box = $('#' + $(this).attr('rel'));
2011-08-09 15:16:18 +00:00
}
}
}).change();
/* Tabs */
2011-09-08 09:52:18 +00:00
$('div.woocommerce_tabs .panel').hide();
$('div.woocommerce_tabs ul.tabs li a').click(function(){
2011-09-05 15:16:59 +00:00
2011-11-04 18:58:15 +00:00
var tab = $(this);
var tabs_wrapper = tab.closest('div.woocommerce_tabs');
$('ul.tabs li', tabs_wrapper).removeClass('active');
2011-09-08 09:52:18 +00:00
$('div.panel', tabs_wrapper).hide();
2011-11-04 18:58:15 +00:00
$('div' + tab.attr('href')).show();
tab.parent().addClass('active');
2011-09-05 15:16:59 +00:00
return false;
});
2011-09-08 09:52:18 +00:00
$('div.woocommerce_tabs').each(function() {
2011-09-21 16:17:28 +00:00
var hash = window.location.hash;
if (hash.toLowerCase().indexOf("comment-") >= 0) {
$('ul.tabs li.reviews_tab a', $(this)).click();
} else {
2011-09-08 09:52:18 +00:00
$('ul.tabs li:first a', $(this)).click();
2011-09-05 15:16:59 +00:00
}
2011-08-09 15:16:18 +00:00
});
/* Shipping calculator */
$('.shipping-calculator-form').hide();
2011-08-09 15:16:18 +00:00
$('.shipping-calculator-button').click(function() {
2011-11-04 18:58:15 +00:00
$('.shipping-calculator-form').slideToggle('slow');
2011-08-09 15:16:18 +00:00
});
// Stop anchors moving the viewport
$(".shipping-calculator-button").click(function() {return false;});
2011-08-09 15:16:18 +00:00
// Variations
2011-08-22 14:10:22 +00:00
//check if two arrays of attributes match
function variations_match(attrs1, attrs2) {
var match = true;
for (name in attrs1) {
var val1 = attrs1[name];
var val2 = attrs2[name];
if(val1.length != 0 && val2.length != 0 && val1 != val2) {
match = false;
}
}
return match;
}
//search for matching variations for given set of attributes
function find_matching_variations(settings) {
var matching = [];
for (var i = 0; i < product_variations.length; i++) {
var variation = product_variations[i];
var variation_id = variation.variation_id;
if(variations_match(variation.attributes, settings)) {
matching.push(variation);
}
}
return matching;
}
//disable option fields that are unavaiable for current set of attributes
function update_variation_values(variations) {
// Loop through selects and disable/enable options based on selections
$('.variations select').each(function( index, el ){
current_attr_select = $(el);
// Disable all
current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');
// Get name
var current_attr_name = current_attr_select.attr('name');
// Loop through variations
for(num in variations) {
var attributes = variations[num].attributes;
for(attr_name in attributes) {
var attr_val = attributes[attr_name];
if(attr_name == current_attr_name) {
if (attr_val) {
current_attr_select.find('option[value="'+attr_val+'"]').removeAttr('disabled');
} else {
current_attr_select.find('option').removeAttr('disabled');
}
}
}
}
});
2011-08-22 14:10:22 +00:00
}
//show single variation details (price, stock, image)
function show_variation(variation) {
2011-09-08 09:52:18 +00:00
var img = $('div.images img:eq(0)');
var link = $('div.images a.zoom:eq(0)');
2011-09-08 09:52:18 +00:00
var o_src = $(img).attr('original-src');
var o_link = $(link).attr('original-href');
2011-08-09 15:16:18 +00:00
2011-08-22 14:10:22 +00:00
var variation_image = variation.image_src;
var variation_link = variation.image_link;
$('.variations_button').show();
2011-09-08 09:52:18 +00:00
$('.single_variation').html( variation.price_html + variation.availability_html );
2011-08-09 15:16:18 +00:00
2011-08-22 14:10:22 +00:00
if (!o_src) {
2011-09-08 09:52:18 +00:00
$(img).attr('original-src', $(img).attr('src'));
2011-08-22 14:10:22 +00:00
}
2011-08-09 15:16:18 +00:00
2011-08-22 14:10:22 +00:00
if (!o_link) {
2011-09-08 09:52:18 +00:00
$(link).attr('original-href', $(link).attr('href'));
2011-08-22 14:10:22 +00:00
}
if (variation_image && variation_image.length > 1) {
2011-09-08 09:52:18 +00:00
$(img).attr('src', variation_image);
$(link).attr('href', variation_link);
2011-08-22 14:10:22 +00:00
} else {
2011-09-08 09:52:18 +00:00
$(img).attr('src', o_src);
$(link).attr('href', o_link);
2011-08-22 14:10:22 +00:00
}
$('.single_variation_wrap').slideDown('200');
2011-08-22 14:10:22 +00:00
}
2011-08-09 15:16:18 +00:00
2011-08-22 14:10:22 +00:00
//when one of attributes is changed - check everything to show only valid options
function check_variations( exclude ) {
2011-08-22 14:10:22 +00:00
var all_set = true;
var current_settings = {};
2011-09-08 09:52:18 +00:00
$('.variations select').each(function(){
2011-09-28 12:34:41 +00:00
if ( exclude && $(this).attr('name') == exclude ) {
all_set = false;
current_settings[$(this).attr('name')] = '';
} else {
2011-10-05 10:09:28 +00:00
if ($(this).val().length == 0) all_set = false;
// Get value
value = $(this).val();
value = value.replace('"', '&quot;');
// Add to settings array
current_settings[$(this).attr('name')] = value;
}
2011-08-22 14:10:22 +00:00
});
var matching_variations = find_matching_variations(current_settings);
if(all_set) {
var variation = matching_variations.pop();
if (variation) {
$('form input[name=variation_id]').val(variation.variation_id);
show_variation(variation);
} else {
// Nothing found - reset fields
$('.variations select').val('');
}
2011-08-22 14:10:22 +00:00
} else {
update_variation_values(matching_variations);
}
}
2011-09-08 09:52:18 +00:00
$('.variations select').change(function(){
2011-08-22 14:10:22 +00:00
$('form input[name=variation_id]').val('');
$('.single_variation_wrap').hide();
$('.single_variation').text('');
check_variations();
2011-10-05 10:09:28 +00:00
$(this).blur();
if($.isFunction($.uniform.update)) {
$.uniform.update();
}
}).focus(function(){
check_variations( $(this).attr('name') );
}).change();
2011-08-22 14:10:22 +00:00
2011-09-11 17:33:26 +00:00
if (woocommerce_params.is_cart==1) {
$('select#shipping_method').live('change', function() {
var method = $('#shipping_method').val();
$('div.cart_totals').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
var data = {
action: 'woocommerce_update_shipping_method',
security: woocommerce_params.update_shipping_method_nonce,
shipping_method: method
};
$.post( woocommerce_params.ajax_url, data, function(response) {
$('div.cart_totals').replaceWith( response );
});
});
}
if (woocommerce_params.is_checkout==1 || woocommerce_params.is_pay_page==1) {
2011-08-09 15:16:18 +00:00
2011-09-08 10:13:12 +00:00
var updateTimer;
var xhr;
2011-09-08 10:13:12 +00:00
function update_checkout() {
2011-08-09 15:16:18 +00:00
if (xhr) xhr.abort();
2011-09-08 10:13:12 +00:00
var method = $('#shipping_method').val();
2011-09-14 14:55:53 +00:00
var country = $('#billing_country').val();
var state = $('#billing_state').val();
var postcode = $('input#billing_postcode').val();
2011-09-08 10:13:12 +00:00
if ($('#shiptobilling input').is(':checked') || $('#shiptobilling input').size()==0) {
2011-09-14 14:55:53 +00:00
var s_country = $('#billing_country').val();
var s_state = $('#billing_state').val();
var s_postcode = $('input#billing_postcode').val();
2011-09-08 10:13:12 +00:00
} else {
2011-09-14 14:55:53 +00:00
var s_country = $('#shipping_country').val();
var s_state = $('#shipping_state').val();
var s_postcode = $('input#shipping_postcode').val();
2011-09-08 10:13:12 +00:00
}
2011-08-09 15:16:18 +00:00
2011-09-08 10:13:12 +00:00
$('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
2011-09-08 10:13:12 +00:00
var data = {
action: 'woocommerce_update_order_review',
security: woocommerce_params.update_order_review_nonce,
shipping_method: method,
country: country,
state: state,
postcode: postcode,
s_country: s_country,
s_state: s_state,
2011-09-14 14:55:53 +00:00
s_postcode: s_postcode,
post_data: $('form.checkout').serialize()
2011-09-08 10:13:12 +00:00
};
xhr = $.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: data,
success: function( response ) {
$('#order_methods, #order_review').remove();
$('#order_review_heading').after(response);
$('#order_review input[name=payment_method]:checked').click();
}
2011-09-08 10:13:12 +00:00
});
2011-08-09 15:16:18 +00:00
2011-09-08 10:13:12 +00:00
}
$(function(){
$('p.password').hide();
$('input.show_password').change(function(){
$('p.password').slideToggle();
});
2011-09-14 14:55:53 +00:00
$('div.shipping_address').hide();
2011-08-09 15:16:18 +00:00
2011-09-08 10:13:12 +00:00
$('#shiptobilling input').change(function(){
2011-09-14 14:55:53 +00:00
$('div.shipping_address').hide();
2011-09-08 10:13:12 +00:00
if (!$(this).is(':checked')) {
2011-09-14 14:55:53 +00:00
$('div.shipping_address').slideDown();
2011-09-08 10:13:12 +00:00
}
}).change();
2011-08-09 15:16:18 +00:00
2011-09-08 10:13:12 +00:00
if (woocommerce_params.option_guest_checkout=='yes') {
2011-09-08 09:52:18 +00:00
$('div.create-account').hide();
2011-09-08 10:13:12 +00:00
$('input#createaccount').change(function(){
$('div.create-account').hide();
if ($(this).is(':checked')) {
$('div.create-account').slideDown();
}
}).change();
}
$('.payment_methods input.input-radio').live('click', function(){
$('div.payment_box').hide();
2011-09-08 09:52:18 +00:00
if ($(this).is(':checked')) {
2011-09-08 10:13:12 +00:00
$('div.payment_box.' + $(this).attr('ID')).slideDown();
2011-08-09 15:16:18 +00:00
}
2011-09-08 10:13:12 +00:00
});
$('#order_review input[name=payment_method]:checked').click();
$('form.login').hide();
$('a.showlogin').click(function(){
$('form.login').slideToggle();
return false;
2011-09-08 10:13:12 +00:00
});
/* Update totals */
$('#shipping_method').live('change', function(){
clearTimeout(updateTimer);
update_checkout();
});
2011-09-14 14:55:53 +00:00
$('input#billing_country, input#billing_state, #billing_postcode, input#shipping_country, input#shipping_state, #shipping_postcode').live('keydown', function(){
2011-09-08 10:13:12 +00:00
clearTimeout(updateTimer);
updateTimer = setTimeout("update_checkout()", '1000');
2011-08-09 15:16:18 +00:00
});
2011-09-14 14:55:53 +00:00
$('select#billing_country, select#billing_state, select#shipping_country, select#shipping_state, #shiptobilling input, .update_totals_on_change').live('change', function(){
2011-09-08 10:13:12 +00:00
clearTimeout(updateTimer);
update_checkout();
});
// Update on page load
if (woocommerce_params.is_checkout==1) update_checkout();
2011-09-08 10:13:12 +00:00
/* AJAX Form Submission */
$('form.checkout').submit(function(){
var form = this;
$(form).block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
$.ajax({
type: 'POST',
url: woocommerce_params.checkout_url,
data: $(form).serialize(),
success: function( code ) {
2011-09-28 12:34:41 +00:00
$('.woocommerce_error, .woocommerce_message').remove();
try {
success = $.parseJSON( code );
window.location = decodeURI(success.redirect);
}
catch(err) {
$(form).prepend( code );
$(form).unblock();
$('html, body').animate({
scrollTop: ($('form.checkout').offset().top - 100)
}, 1000);
}
},
2011-09-08 10:13:12 +00:00
dataType: "html"
});
return false;
});
2011-08-09 15:16:18 +00:00
});
2011-09-08 10:13:12 +00:00
}
});