-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathwp-e-commerce.js
More file actions
executable file
·1456 lines (1206 loc) · 52.9 KB
/
wp-e-commerce.js
File metadata and controls
executable file
·1456 lines (1206 loc) · 52.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* globals jQuery */
///////////////////////////////////////////////////////////////////////////////////////////////
// This section is used to create the globals that were originally defined in the
// dynamic-js file pre 3.8.14. Note that variables also also exist in the "wpsc_ajax" structure.
// To add a new global property that can be referenced in the script see the hook
// wpsc_javascript_localizations in wpsc-core/wpsc-functions.php
//
/**
* javascript variables for WP-e-Commerce
*
* These WPeC WordPress localized variables were in use prior to release 3.8.14, and are explicitly
* declared here for maximum backwards compatibility.
*
* In releases prior to 3.8.14 these legacy variables may have been declared in the dynamically
* created javascript, or in the HTML as a localized variable.
*
* For javascript variables added after version 3.8.14 use the following utility function to access the
* localized variables.
*
* wpsc_var_get ( name )
* wpsc_var_set ( name, value )
* wpsc_var_isset ( name, value );
*
*/
if ( typeof wpsc_vars !== 'undefined' ) {
var wpsc_ajax = wpsc_vars.wpsc_ajax;
var base_url = wpsc_vars.base_url;
var WPSC_URL = wpsc_vars.WPSC_URL;
var WPSC_IMAGE_URL = wpsc_vars.WPSC_IMAGE_URL;
var WPSC_IMAGE_URL = wpsc_vars.WPSC_IMAGE_URL;
var WPSC_CORE_IMAGES_URL = wpsc_vars.WPSC_CORE_IMAGES_URL;
var fileThickboxLoadingImage = wpsc_vars.fileThickboxLoadingImage;
var wpsc_debug = wpsc_vars.hasOwnProperty( 'debug' );
}
// end of variable definitions
///////////////////////////////////////////////////////////////////////////////////////////////
/**
* check if a localized WPeC value is set
*
* @since 3.8.14
*
* @param string name name of localized variable
*
* @returns boolean true if the variable is set, false otherwise
*
*/
function wpsc_var_isset( name ) {
if ( typeof wpsc_vars !== 'undefined' ) {
return wpsc_vars[name] !== undefined;
}
return false;
}
/**
* get the value of a localized WPeC value if it is set
*
* @since 3.8.14
*
* @param string name name of localized variable
*
* @returns varies value of the var set
*
*/
function wpsc_var_get( name ) {
if ( typeof wpsc_vars !== 'undefined' ) {
return wpsc_vars[name];
}
return undefined;
}
/**
* Checks to determine whether or not an element is fully visible
*
* @since 3.8.14.1
* @param jQuery object el Element being checked for visibility.
* @return boolean Whether or not element is visible.
*/
function wpsc_element_is_visible( el ) {
// check to be sure the form element exists, it may have been passed to us unveriified in a callback
if ( typeof el !== 'undefined' && el.length ) {
var top = jQuery(window).scrollTop(),
bottom = top + jQuery(window).height(),
elTop = el.offset().top;
visible = ( (elTop >= top ) && ( elTop <= bottom ) && ( elTop <= bottom ) && ( elTop >= top ) ) && el.is(':visible');
} else {
visible = false;
}
return visible;
}
/**
* change the value of a localized WPeC var
*
* @since 3.8.14
*
* @param string name name of localized variable
* @param varies value value of the var being set
*
* @returns varies value of the var being set
*
*/
function wpsc_var_set( name, value ) {
if ( typeof wpsc_vars !== 'undefined' ) {
wpsc_vars[name] = value;
return value;
}
return undefined;
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Setting up the WPEC customer identifier
//
// When WPEC generates a page it sets a 'customer cookie' into the browser. This cookie is a
// persistent identifier that connects a user's session to their cart or other profile data a
// store may need to work correctly.
//
// When page caching or a CDN is in place WPEC does not get to set the cookie because
// the page is served without the overhead of computing the page contents.
// This means that the first GET/POST request, including requests using AJAX are required to
// initialize the customer identifier
//
// Because browsers may execute these requests in parallel the probability of multiple unique
// cookies being set is very high. This means that in the absence of the logic below WPEC would
// have to create multiple unique profiles as each of the parallel requests are executed. This
// can cause data when one request uses one profile and the other request uses a different profile.
// It could also cause performance issues on the back-end, and create a potentially resource
// intensive and wasteful situation.
//
// The mitigation for this issue is to look for the customer identifier when this script first
// runs. If the identifier is not found, initiate a very quick synchronous AJAX request. This
// happens before any other processing takes place. This request should create the unique
// customer identifier before it is required by other processing.
//
// a global variable used to hold the current users visitor id,
// if you are going to user it always check to be sure it is not false
var wpsc_visitor_id = false;
var i, cookieName, cookieValue, docCookies = document.cookie.split( ';' );
for ( i = 0; i < docCookies.length; i++ ) {
cookieName = docCookies[i].substr( 0, docCookies[i].indexOf( '=' ) );
cookieName = cookieName.replace( /^\s+|\s+$/g, '' );
cookieValue = docCookies[i].substr( docCookies[i].indexOf('=') + 1);
var cookieValueClean = decodeURI(cookieValue);
var idAsText = cookieValueClean.substr(0,cookieValueClean.indexOf( '|' ) );
if ( document.cookie.indexOf("wpsc_customer_cookie") < 0 ) {
if ( document.cookie.indexOf("wpsc_attempted_validate") < 0 ) {
// create a cookie to signal that we have attempted validation. If we find the cookie is set
// we don't re-attempt validation. This means will only try to validate once and not slow down
// subsequent page views.
// The lack of expiration date means the cookie will be deleted when the browser
// is closed, so the next time the visitor attempts to access the site after closing the browser
// they will revalidate.
var now = new Date();
document.cookie = "wpsc_attempted_validate="+now;
var wpsc_http = new XMLHttpRequest();
// open setup and send the request in synchronous mode
wpsc_http.open( "POST", wpsc_ajax.ajaxurl + "?action=wpsc_validate_customer", false );
wpsc_http.setRequestHeader( "Content-type", "application/json; charset=utf-8" );
// Note that we cannot set a timeout on synchronous requests due to XMLHttpRequest limitations
wpsc_http.send();
if ( wpsc_debug && window.console ) {
// we did the request in synchronous mode so we don't need the on load or ready state change events to check the result
if (wpsc_http.status == 200) {
var result = JSON.parse( wpsc_http.responseText );
if ( result.valid && result.id ) {
wpsc_visitor_id = result.id;
if ( wpsc_debug ) {
console.log("The new WPeC visitor id is " + wpsc_visitor_id);
}
}
if ( wpsc_debug ) {
console.log("The HTTP request to validate the WPeC validate visitor id was not successful, HTTP error " + wpsc_http.status);
}
}
}
if ( wpsc_debug ) {
console.log("The existing WPeC visitor id is " + wpsc_visitor_id);
}
}
}
}
// end of setting up the WPEC customer identifier
///////////////////////////////////////////////////////////////////////////////////////////////
function wpsc_do_ajax_request( data, success_callback ) {
jQuery.ajax({
type : "post",
dataType : "json",
url : wpsc_ajax.ajaxurl,
data : data,
success : success_callback,
});
}
/**
* update a customer meta value
*
* @since 3.8.14
* @param meta_key string
* @param meta_value string
* @param response_callback function
*/
function wpsc_update_customer_data( meta_key, meta_value, response_callback ) {
// wrap our ajax request in a try/catch so that an error doesn't stop the script from running
try {
var ajax_data = {action: 'wpsc_customer_updated_data', meta_key : meta_key, meta_value : meta_value };
wpsc_do_ajax_request( ajax_data, response_callback );
} catch ( err ) {
if ( wpsc_debug ) {
if (window.console && window.console.log) {
console.log(err);
}
}
}
}
/**
* get all customer data
*
* @since 3.8.14
* @param meta_key string
* @param meta_value string
* @param response_callback function
*/
function wpsc_get_customer_data( response_callback ) {
// wrap our ajax request in a try/catch so that an error doesn't stop the script from running
try {
var ajax_data = {action: 'wpsc_get_customer_meta' };
wpsc_do_ajax_request( ajax_data, response_callback );
} catch ( err ) {
if ( wpsc_debug ) {
if (window.console && window.console.log) {
console.log(err);
}
}
}
}
/**
* common callback to update fields based on response from ajax processing.
*
* @since 3.8.14
* @param response object returned from ajax request
*/
function wpsc_update_customer_meta( response ) {
var element_that_caused_change_event = response.data.request.meta_key;
if ( response.hasOwnProperty( 'data' ) && response.data.hasOwnProperty( 'customer_meta' )) {
var customer_meta = response.data.customer_meta;
// if the response includes customer meta values find out where the value
// belongs and then put it there
jQuery.each( customer_meta, function( meta_key, meta_value ) {
// if there are other fields on the current page that are used to change the same meta value then
// they need to be updated
var selector = '[data-wpsc-meta-key="' + meta_key + '"]';
jQuery( selector ).each( function( index, value ) {
var element_meta_key = wpsc_get_element_meta_key( this );
if ( element_meta_key != element_that_caused_change_event ) {
if ( jQuery(this).is(':checkbox') ) {
var boolean_meta_value = wpsc_string_to_boolean( meta_value );
if ( boolean_meta_value ) {
jQuery( this ).attr( 'checked', 'checked' );
} else {
jQuery( this ).removeAttr( 'checked' );
}
} else if ( jQuery(this).hasClass('wpsc-region-dropdown') ) {
// we are going to skip updating the region value because the select is dependant on
// the value of other meta, specifically the billing or shipping country.
// rather than enforce a field order in the response we will take care of it by doing
// a second pass through the updates looking for only the region drop downs
} else if ( jQuery(this).hasClass('wpsc-country-dropdown') ) {
var current_value = jQuery( this ).val();
if ( current_value != meta_value ) {
jQuery( this ).val( meta_value );
// if we are updating a country drop down we need to make sure that
// the correct regions are in the list before we change the value
wpsc_update_regions_list_to_match_country( jQuery( this ) );
}
} else {
var current_value = jQuery( this ).val();
if ( current_value != meta_value ) {
jQuery( this ).val( meta_value );
}
}
}
});
});
// this second pass through the properties only looks for region drop downs, their
// contents is dependant on other meta values so we do these after everything else has
jQuery.each( customer_meta, function( meta_key, meta_value ) {
// if there are other fields on the current page that are used to change the same meta value then
// they need to be updated
var selector = '[data-wpsc-meta-key="' + meta_key + '"]';
jQuery( selector ).each( function( index, value ) {
var element_meta_key = wpsc_get_element_meta_key( this );
if ( element_meta_key != element_that_caused_change_event ) {
if ( jQuery(this).hasClass('wpsc-region-dropdown') ) {
var current_value = jQuery( this ).val();
if ( current_value != meta_value ) {
jQuery( this ).val( meta_value );
}
}
}
});
});
}
}
/**
* If shipping quotes need to be recalcualted adjust the checkout form and notify the user
*
* @param response data from AJAX request
*/
function wpsc_check_for_shipping_recalc_needed( response ) {
// TODO: if shipping needs to be re-calculated we need to refresh the page. This is the only option
// in version 3.8.14 and earlier. Future versions should support replacing the shipping quote elements
// via AJAX
if ( response.hasOwnProperty( 'needs_shipping_recalc' ) && jQuery( '#checkout_page_container' ).length ) {
if ( response.needs_shipping_recalc ) {
var form = jQuery('table.productcart' ).first();
var msg = wpsc_var_get( 'msg_shipping_need_recalc' );
if ( ! jQuery( '#shipping_quotes_need_recalc').length ) {
form.before( '<div id="shipping_quotes_need_recalc" style="display:none">' + msg + '</div>' );
jQuery( '#shipping_quotes_need_recalc' ).show( 375 );
if ( wpsc_ajax.hasOwnProperty( 'slide_to_shipping_error' ) && wpsc_ajax.slide_to_shipping_error && ! wpsc_element_is_visible( jQuery( '#shipping_quotes_need_recalc' ) ) ) {
jQuery( 'html, body' ).animate({
scrollTop : jQuery( '#checkout_page_container' ).offset().top
}, 600 );
}
}
jQuery( 'input:radio[name=shipping_method]' ).prop('checked', false).attr('disabled',true);
jQuery( 'input:radio[name=shipping_method]' ).closest( 'tr' ).hide( 275 );
jQuery( 'tr.wpsc_shipping_header' ).hide( 275 );
jQuery( '.wpsc_checkout_table_totals' ).hide( 275 );
jQuery( '.total_tax' ).closest( 'table' ).hide( 275 );
}
}
}
/**
* Take data from checkout data array and put it where it belongs
*
* Note: logic extracted from pre 3.8.14 wpsc_handle_country_change function
*
* @since 3.8.14
* @param checkout_info
* @return true if execution should continue, false if it should stop
*/
function wpsc_update_checkout_info( checkout_info ) {
if ( checkout_info.hasOwnProperty( 'shipping_keys' ) ) {
jQuery.each( checkout_info.shipping_keys, function( key, shipping ) {
jQuery( '#shipping_' + key ).html( shipping );
});
}
if ( checkout_info.hasOwnProperty( 'tax' ) ) {
if ( checkout_info.tax > 0 ) {
jQuery( 'tr.total_tax' ).show();
} else {
jQuery( 'tr.total_tax' ).hide();
}
}
if ( checkout_info.hasOwnProperty( 'cart_shipping' ) ) {
jQuery( '#checkout_shipping' ).html( checkout_info.cart_shipping );
}
if ( checkout_info.hasOwnProperty( 'widget_output' ) ) {
jQuery( 'div.shopping-cart-wrapper' ).html( checkout_info.widget_output );
}
if ( checkout_info.hasOwnProperty( 'display_tax' ) ) {
jQuery( '#checkout_tax' ).html( "<span class='pricedisplay'>" + checkout_info.display_tax + "</span>" );
}
if ( checkout_info.hasOwnProperty( 'total_input' ) ) {
jQuery( '#checkout_total' ).html( checkout_info.total + "<input id='shopping_cart_total_price' type='hidden' value='" + checkout_info.total_input + "' />" );
}
jQuery( ".wpsc-visitor-meta").on( "change", wpsc_meta_item_change );
jQuery( document ).trigger( { type : 'wpsc_update_checkout_info', info : checkout_info } );
return true;
}
/**
* common callback to update checkout fields based on response from ajax processing. All fields that set
* are present to make it easier to see where the plugin can be extended
*
* @since 3.8.14
* @param response object returned from ajax request
*/
function wpsc_meta_item_change_response( response ) {
jQuery( ".wpsc-visitor-meta").off( 'change', wpsc_meta_item_change );
if ( response.hasOwnProperty('success') && response.success && response.hasOwnProperty('data') ) {
// Whatever replacements have been sent for the checkout form can be efficiently
// put into view
if ( response.data.hasOwnProperty('replacements') ) {
jQuery.each( response.data.replacements, function( elementname, replacement ) {
jQuery( '#' + replacement.elementid ).replaceWith( replacement.element );
});
}
// Whatever has changed as far as customer meta should be processed
if ( response.data.hasOwnProperty( 'checkout_info' ) ) {
if ( ! wpsc_update_checkout_info( response.data.checkout_info ) ) {
return false;
}
}
// Whatever has changed as far as customer meta should be processed
if ( response.data.hasOwnProperty( 'customer_meta' ) ) {
wpsc_update_customer_meta( response );
}
// TODO: this is where we can rely on the PHP application to generate and format the content for the
// checkout screen rather than doing lot's of work in this js. If we update the PHP application top
// return the elements for the checkout screen using the same logic that is used when the checkout
// page is originally created we simplify this script, maintain consistency, allow WordPress and WPEC
// hooks to be used to change checkout and make chckout display better for those client paltforms
// that may not have the necessary computing power to use js to do the work we are asking.
var event = jQuery.Event( "wpsc-visitor-meta-change" );
event.response = response;
jQuery( "wpsc-visitor-meta:first" ).trigger( event );
// Check if shipping quotes need to be updated
wpsc_check_for_shipping_recalc_needed( response.data );
}
jQuery( ".wpsc-visitor-meta" ).on( "change", wpsc_meta_item_change );
wpsc_adjust_checkout_form_element_visibility();
}
/**
* find the WPeC meta key associated with an element, if there is one
*
* @param The element to extract the meta key from
*
* @returns string meta_key
*/
function wpsc_get_element_meta_key( element ) {
if ( element instanceof jQuery ) {
;
} else if ( typeof input == "string" ) {
element = wpsc_get_wpsc_meta_element( element );
} else if ( typeof input == "object" ){
element = jQuery( element );
} else {
return null;
}
var meta_key = element.attr( "data-wpsc-meta-key" );
if ( meta_key === undefined ) {
meta_key = element.attr( "title" );
if ( meta_key === undefined ) {
meta_key = element.attr( "id" );
}
}
return meta_key;
}
/**
* common callback triggered whenever a WPEC meta value is changed
*
* @since 3.8.14
*/
function wpsc_meta_item_change() {
var meta_value = wpsc_get_value_from_wpsc_meta_element( this );
var meta_key = wpsc_get_element_meta_key( jQuery( this ) );
// if there are other fields on the current page that are used to change the same meta value then
// they need to be updated
var selector = '[data-wpsc-meta-key="' + meta_key + '"]';
var element_that_changed_meta_value = this;
jQuery( selector ).each( function( index, value ) {
if ( element_that_changed_meta_value != this ) {
if ( jQuery(this).is(':checkbox') ) {
var boolean_meta_value = wpsc_string_to_boolean( meta_value );
if ( boolean_meta_value ) {
jQuery( this ).attr( 'checked', 'checked' );
} else {
jQuery( this ).removeAttr( 'checked' );
}
} else {
jQuery( this ).val( meta_value );
}
}
});
wpsc_update_customer_data( meta_key, meta_value, wpsc_meta_item_change_response );
}
function wpsc_adjust_checkout_form_element_visibility() {
// make sure any item that changes checkout data is bound to the proper event handler
jQuery( ".wpsc-visitor-meta" ).off( "change", wpsc_meta_item_change );
if ( jQuery( "#shippingSameBilling" ).length ) {
var shipping_row = jQuery( "#shippingSameBilling" ).closest( "tr" );
if( ! wpsc_show_checkout_shipping_fields() ) {
jQuery( shipping_row ).siblings( ":not( .checkout-heading-row, :has( #agree ), :has( .wpsc_gateway_container ) ) ").hide();
jQuery( "#shippingsameasbillingmessage" ).show();
} else {
jQuery( shipping_row ).siblings().show();
jQuery( "#shippingsameasbillingmessage" ).hide();
}
}
wpsc_update_location_elements_visibility();
wpsc_countries_lists_handle_restrictions();
// make sure any item that changes checkout data is bound to the proper event handler
jQuery( ".wpsc-visitor-meta" ).on( "change", wpsc_meta_item_change );
return true;
}
/*
* update the countries lists as appropriate based on acceptible shipping countries and
* shipping same as billing
*
* since 3.8.14
*
*/
function wpsc_countries_lists_handle_restrictions() {
// if there isn't a list of acceptable countries then we don't have any work to do
if ( typeof wpsc_acceptable_shipping_countries === "object" ) {
// we need to know the current billing country
var current_billing_country = wpsc_get_value_from_wpsc_meta_element( 'billingcountry' );
var current_shipping_country = wpsc_get_value_from_wpsc_meta_element( 'shippingcountry' );
var selector = 'select[data-wpsc-meta-key="shippingcountry"]';
var put_allowed_countries_into_billing_list = false;
if ( jQuery( "#shippingSameBilling" ).length ) {
if ( jQuery( "#shippingSameBilling" ).is(":checked") ) {
put_allowed_countries_into_billing_list = true;
selector = selector + ', select[data-wpsc-meta-key="billingcountry"]';
}
}
var country_drop_downs = jQuery( selector );
country_drop_downs.empty();
country_drop_downs.append( new Option( wpsc_var_get( 'no_country_selected' ), '' ) );
for ( var isocode in wpsc_acceptable_shipping_countries ) {
if ( wpsc_acceptable_shipping_countries.hasOwnProperty( isocode ) ) {
var country_name = wpsc_acceptable_shipping_countries[isocode];
country_drop_downs.append( new Option( country_name, isocode ) );
}
}
country_drop_downs.val( current_shipping_country );
if ( ! put_allowed_countries_into_billing_list ) {
selector = 'select[data-wpsc-meta-key="billingcountry"]';
country_drop_downs = jQuery( selector );
if ( country_drop_downs.length ) {
country_drop_downs.empty();
country_drop_downs.append( new Option( wpsc_var_get( 'no_country_selected' ), '' ) );
countries = wpsc_var_get( 'wpsc_countries' );
for ( var isocode in countries ) {
if ( countries.hasOwnProperty( isocode ) ) {
var country_name = countries[isocode];
country_drop_downs.append( new Option( country_name, isocode ) );
}
}
}
country_drop_downs.val( current_billing_country );
}
}
return true;
}
/*
* Change the labels assicated with country and region fields to match the
* terminology for the selected location. For example, regions in the USA are
* called states, regions in Canada are called provinces
*
* since 3.8.14
*
*/
function wpsc_update_location_labels( country_select ) {
var country_meta_key = wpsc_get_element_meta_key( country_select ),
label, country_code;
if ( country_meta_key == 'billingcountry' ) {
var billing_state_element = wpsc_get_wpsc_meta_element( 'billingstate' ) ;
if ( billing_state_element ) {
country_code = wpsc_get_value_from_wpsc_meta_element( 'billingcountry' );
label = wpsc_country_region_label( country_code );
billing_state_element.attr( 'placeholder', label );
wpsc_update_labels( wpsc_get_wpsc_meta_elements( 'billingstate' ), label );
}
} else if ( country_meta_key == 'shippingcountry' ) {
var shipping_state_element = wpsc_get_wpsc_meta_element( 'shippingstate' );
if ( shipping_state_element ) {
country_code = wpsc_get_value_from_wpsc_meta_element( 'shippingcountry' );
label = wpsc_country_region_label( country_code );
shipping_state_element.attr( 'placeholder', label );
wpsc_update_labels( wpsc_get_wpsc_meta_elements( 'shippingstate' ), label );
}
}
return true;
}
/**
* Fill the associated regions drop down based on the value in the country drop down
*
* @param country_select jQuery Object Country drop down to work with
*/
function wpsc_update_regions_list_to_match_country( country_select ) {
var country_meta_key = wpsc_get_element_meta_key( country_select );
var region_meta_key;
if ( country_meta_key.indexOf( "shipping" ) === 0 ) {
region_meta_key = 'shippingregion';
} else {
region_meta_key = 'billingregion';
}
var region_select = wpsc_country_region_element( country_select );
var all_region_selects = wpsc_get_wpsc_meta_elements( region_meta_key );
var country_code = wpsc_get_value_from_wpsc_meta_element( country_select );
var region = wpsc_get_value_from_wpsc_meta_element( region_meta_key );
if ( wpsc_country_has_regions( country_code ) ) {
var select_a_region_message = wpsc_no_region_selected_message( country_code );
var regions = wpsc_country_regions( country_code );
all_region_selects.empty();
all_region_selects.append( new Option( select_a_region_message, '' ) );
for ( var region_code in regions ) {
if ( regions.hasOwnProperty( region_code ) ) {
var region_name = regions[region_code];
all_region_selects.append( new Option( region_name, region_code ) );
}
}
if ( region ) {
all_region_selects.val( region );
}
region_select.show();
} else {
region_select.hide();
region_select.empty();
}
wpsc_update_location_labels( country_select );
wpsc_update_location_elements_visibility();
wpsc_copy_meta_value_to_similiar( country_select );
}
function wpsc_string_to_boolean( string ) {
return string.trim() !== '';
}
/*
* Load the region dropdowns based on changes to the country dropdowns
*
* since 3.8.14
*
*/
function wpsc_change_regions_when_country_changes() {
wpsc_copy_meta_value_to_similiar( jQuery( this ) );
wpsc_update_regions_list_to_match_country( jQuery( this ) );
return true;
}
function wpsc_copy_meta_value_to_similiar( element ) {
var element_meta_key = wpsc_get_element_meta_key( element ),
meta_value = element.val(),
current_value;
// if there are other fields on the current page that are used to change the same meta value then
// they need to be updated
var selector = '[data-wpsc-meta-key="' + element_meta_key + '"]';
jQuery( selector ).each( function( index, value ) {
if ( this != element) {
if ( jQuery(this).is(':checkbox') ) {
var boolean_meta_value = wpsc_string_to_boolean( meta_value );
if ( boolean_meta_value ) {
jQuery( this ).attr( 'checked', 'checked' );
} else {
jQuery( this ).removeAttr( 'checked' );
}
} else {
current_value = jQuery( this ).val();
if ( current_value != meta_value ) {
jQuery( this ).val( meta_value );
}
}
}
});
}
/*
* returns the element id for the cehckout item if it is in the checkout form
*
* @since 3.8.14
*
* @param string name unqiue name of the checkout item
*
* @return int|boolean element id if it is in the checkout form, false if the element is not in the checkout form
*/
function wpsc_checkout_item_form_id( name ) {
var map_from_name_to_id = wpsc_var_get( 'wpsc_checkout_unique_name_to_form_id_map' );
var checkout_item_form_id = false;
if ( map_from_name_to_id ) {
if ( map_from_name_to_id.hasOwnProperty( name ) ) {
checkout_item_form_id = map_from_name_to_id[name];
}
}
return checkout_item_form_id;
}
/*
* decide if shipping fields should be show or not
*/
function wpsc_show_checkout_shipping_fields() {
// we will need to know if shipping fields should be show or not, if there
// isn't a shipping same as billing element, then we show by default, if there
// is a shipping same as billing we show if the element is not checked
var show_shipping_field = true;
if( jQuery("#shippingSameBilling").length ) {
show_shipping_field = ! jQuery("#shippingSameBilling").is(":checked");
}
return show_shipping_field;
}
function wpsc_setup_region_dropdowns() {
wpsc_get_wpsc_meta_elements( 'billingcountry' ).each( function( index, value ){
wpsc_update_regions_list_to_match_country( jQuery( this ) );
});
wpsc_get_wpsc_meta_elements( 'shippingcountry' ).each( function( index, value ){
wpsc_update_regions_list_to_match_country( jQuery( this ) );
});
}
/**
* changes the visibility of the region edit element and the region drop down element based on
* on the state and contents of the coutnry drop down
*
* @since 3.8.14
*
* @returns {Boolean}
*/
function wpsc_update_location_elements_visibility() {
if ( wpsc_checkout_item_active( 'billingstate' ) ) {
// for convenience, get the jQuery objects for each of the billing elements we want to manipulate up front
var billing_state_elements = wpsc_get_wpsc_meta_elements( 'billingstate' );
var billing_region_elements = wpsc_get_wpsc_meta_elements( 'billingregion' );
if ( wpsc_billing_country_has_regions() ) {
billing_region_elements.show();
billing_state_elements.hide();
} else {
billing_region_elements.hide();
billing_state_elements.show();
}
}
if ( wpsc_checkout_item_active( 'shippingstate' ) ) {
// for convenience, get the jQuery objects for each of the billing elements we want to manipulate up front
var shipping_state_elements = wpsc_get_wpsc_meta_elements( 'shippingstate' );
var shipping_region_elements = wpsc_get_wpsc_meta_elements( 'shippingregion' );
if ( wpsc_shipping_country_has_regions() ) {
shipping_region_elements.show();
shipping_state_elements.hide();
} else {
shipping_region_elements.hide();
shipping_state_elements.show();
}
}
return true;
}
function wpsc_country_has_regions( country_code ) {
var regions_object_name = "wpsc_country_" + country_code + "_regions";
return wpsc_var_isset( regions_object_name );
}
function wpsc_country_regions( country_code ) {
var regions_object_name = "wpsc_country_" + country_code + "_regions";
return wpsc_var_get( regions_object_name );
}
function wpsc_country_region_label( country_code ) {
var regions_label_name = "wpsc_country_" + country_code + "_region_label";
var label = wpsc_var_get( regions_label_name );
if ( ! label ) {
label = wpsc_var_get( 'no_region_label' );
}
return label;
}
function wpsc_current_destination_country() {
return wpsc_get_value_from_wpsc_meta_element( 'shippingcountry' );
}
function wpsc_no_region_selected_message( country_code ) {
var label = wpsc_country_region_label( country_code );
var format = wpsc_var_get( 'no_region_selected_format' );
var message = format.replace("%s",label);
return message;
}
function wpsc_get_label_element( input ) {
var input_element;
if ( input instanceof jQuery ) {
input_element = input;
} else if ( typeof input == "string" ) {
input_element = wpsc_get_wpsc_meta_element( input );
} else if ( typeof input == "object" ){
input_element = jQuery( input );
} else {
return null;
}
var label_element;
if ( input_element ) {
var element_id = input_element.attr('id');
if ( element_id ) {
label_element = jQuery( "label[for='" + element_id + "']" ).first();
}
}
return label_element;
}
function wpsc_update_labels( elements, label ) {
elements.each( function( index, value ){
var label_element = wpsc_get_label_element( jQuery( this ) );
if ( label_element !== undefined ) {
if ( label_element.find('.asterix') ) {
label = label + '<span class="asterix">*</span>';
}
label_element.html( label );
}
});
}
function wpsc_get_wpsc_meta_element( meta_key ) {
var elements = wpsc_get_wpsc_meta_elements( meta_key );
return elements.first();
}
function wpsc_get_wpsc_meta_elements( meta_key ) {
var selector = '[data-wpsc-meta-key="' + meta_key + '"]';
var elements = jQuery( selector );
return elements;
}
function wpsc_get_value_from_wpsc_meta_element( meta ) {
var element;
if ( meta instanceof jQuery ) {
element = meta;
} else if ( typeof meta == "string" ) {
element = wpsc_get_wpsc_meta_element( meta );
} else if ( typeof meta == "object" ){
element = jQuery( meta );
} else {
return null;
}
var meta_value = false;
if ( element.is(':checkbox') ) {
if ( element.is(':checked') ) {
meta_value = element.val();
} else {
meta_value = '';
}
} else if ( element.is('select') ) {
meta_value = element.find( 'option:selected' ).val();
} else {
meta_value = element.val();
}
return meta_value;
}
/*
* find the region dropdown that goes with the country dropdown
*
* since 3.8.14
*
*/
function wpsc_country_region_element( country ) {
// if the meta key was was given as the arument we can find the element easy enough
if ( typeof country == "string" ) {
country = wpsc_get_wpsc_meta_element( country );
}
var country_id = country.attr('id');
var region_id = country_id + "_region";
var region_select = jQuery( "#" + region_id );
return region_select;
}
/**
* process region drop down change event
*
* @since 3.8.14
*/
function wpsc_region_change() {
wpsc_copy_meta_value_to_similiar( jQuery( this ) );
}
function wpsc_checkout_item_active( $checkout_item ) {
var active_items = wpsc_var_get( "wpsc_checkout_item_active" );
if ( 'undefined' === typeof active_items ) {
return false;
}