/
commerce.js
1822 lines (1661 loc) · 55.4 KB
/
commerce.js
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
function _commerce_addressfield_inject_onto_pane(order) {
try {
console.log(drupalgap_router_path_get(), order);
switch (drupalgap_router_path_get()) {
case 'checkout/%':
// If there is billing information on the order, inject it into the address field.
if (order.commerce_customer_billing) {
_commerce_addressfield_inject_components(order, 'commerce_customer_billing');
}
break;
case 'checkout/shipping/%':
// If there is billing information on the order, inject it into the address field.
if (order.commerce_customer_shipping) {
_commerce_addressfield_inject_components(order, 'commerce_customer_shipping');
}
break;
}
}
catch (error) { console.log('_commerce_addressfield_inject_onto_pane', error); }
}
/**
* Given an order object and an address field machine name, this will inject the address
* components into the address widget on the current checkout pane.
* @param {Object} order
* @param {String} field_name
* @private
*/
function _commerce_addressfield_inject_components(order, field_name) {
var field_name_wrap = field_name + '_entities';
var address = order[field_name_wrap][order[field_name]]['commerce_customer_address'];
var form_id = drupalgap_router_path_get() == 'checkout/%' ? 'commerce_checkout_view' : 'commerce_checkout_shipping_view';
var addressfield_name = form_id == 'commerce_checkout_view' ? 'billing_information' : 'shipping_information';
var prefix = drupalgap_form_get_element_id(addressfield_name, form_id);
// @TODO use the new addressfield_inject_components() helper here instead!
$.each(address, function(component, value) {
var id = prefix + '-' + component;
var input = $('#' + id);
if (input.get(0)) {
// If the address' country differs from the widgets country, force change the
// country, which then in turn re triggers this via a services post process.
if (component == 'country' && value != 'US' && value != $(input).val()) {
$(input).val(value).selectmenu('refresh', true).change();
return false;
}
if ($(input).attr('type') == 'text') {
$(input).val(value);
}
else if ($(input).get(0).tagName == 'SELECT') {
$(input).val(value).selectmenu('refresh', true);
}
}
});
}
/**
* Implements hook_block_info().
*/
function commerce_block_info() {
var blocks = {
commerce_cart: {
delta: 'commerce_cart',
module: 'commerce'
}
};
return blocks;
}
/**
* Implements hook_block_view().
*/
function commerce_block_view(delta) {
var content = '';
if (delta == 'commerce_cart') {
var page_id = drupalgap_get_page_id();
var cart_container_id = page_id + '_cart';
content = '<div id="' + cart_container_id + '" class="commerce_cart"></div>' +
drupalgap_jqm_page_event_script_code({
page_id: page_id,
jqm_page_event: 'pageshow',
jqm_page_event_callback: '_commerce_block_view',
jqm_page_event_args: JSON.stringify({
cart_container_id: cart_container_id
})
});
}
return content;
}
/**
*
*/
function _commerce_block_view(options) {
try {
commerce_cart_index(null, {
success: function(result) {
if (result.length != 0) {
$.each(result, function(order_id, order) {
var html = theme('commerce_cart_block', {order: order});
$('#' + options.cart_container_id).html(html).trigger('create');
return false; // Process only one cart.
});
}
}
});
}
catch (error) {
console.log('_commerce_block_view - ' + error);
}
}
/**
*
*/
function _commerce_cart_attribute_change() {
try {
_commerce_product_display_product_id = _commerce_product_display_get_current_product_id();
}
catch (error) { console.log('_commerce_cart_attribute_change - ' + error); }
}
/**
*
*/
function commerce_cart_view() {
try {
return '<div id="commerce_cart"></div>';
}
catch (error) { console.log('commerce_cart_view - ' + error); }
}
/**
*
*/
function commerce_cart_view_pageshow() {
try {
commerce_cart_index(null, {
success: function(result) {
if (result.length != 0) {
$.each(result, function(order_id, order) {
// Set aside the order so it can be used later without fetching
// it again.
_commerce_order[order_id] = order;
// Theme the cart and render it on the page.
var html = theme('commerce_cart', { order: order });
$('#commerce_cart').html(html).trigger('create');
return false; // Process only one cart.
});
}
}
});
}
catch (error) { console.log('commerce_cart_view_pageshow - ' + error); }
}
/**
* Implements hook_field_formatter_view().
*/
function commerce_cart_field_formatter_view(entity_type, entity, field,
instance, langcode, items, display
) {
try {
var element = {};
if (!empty(items)) {
// Generate markup that will place an empty div placeholder and pageshow
// handler that will dynamically inject the cart into the page.
var markup =
'<div id="' + commerce_cart_container_id(entity_type, entity.nid) + '"></div>' +
drupalgap_jqm_page_event_script_code({
page_id: drupalgap_get_page_id(),
jqm_page_event: 'pageshow',
jqm_page_event_callback:
'_commerce_cart_field_formatter_view_pageshow',
jqm_page_event_args: JSON.stringify({
entity_type: entity_type,
entity_id: entity.nid
})
});
// Place the markup on delta zero of the element.
element[0] = {
markup: markup
};
}
return element;
}
catch (error) {
console.log('commerce_cart_field_formatter_view - ' + error);
}
}
/**
*
*/
function _commerce_cart_field_formatter_view_pageshow(options) {
try {
var entity_type = options.entity_type;
var entity_id = options.entity_id;
// Load the product display.
commerce_product_display_load(entity_id, {
success: function(product_display) {
// Inject the add to cart form html into the container.
var form_html = drupalgap_get_form('commerce_cart_add_to_cart_form', product_display);
$('#' + commerce_cart_container_id(entity_type, entity_id)).html(form_html).trigger('create');
}
});
}
catch (error) {
console.log('_commerce_cart_field_formatter_view_pageshow - ' + error);
}
}
/**
* Given an entity type and entity id, this will return the html attribute id to
* use on the empty div container.
*/
function commerce_cart_container_id(entity_type, entity_id) {
try {
return 'commerce_cart_container_' + entity_type + '_' + entity_id;
}
catch (error) { console.log('commerce_cart_container_id - ' + error); }
}
function commerce_checkout_complete_container_id(order_id) {
return 'commerce_checkout_complete_' + order_id;
}
/**
*
*/
function commerce_checkout_complete_view(order_id) {
return '<div id="' + commerce_checkout_complete_container_id(order_id) + '"></div>';
}
/**
* The billing page.
*/
function commerce_checkout_view(form, form_state, order_id) {
try {
// @NOTE - when testing, if you sent the app's front page to the checkout
// page, the drupalgap.commerce object may not be available yet. It's better
// to set the front page to the cart page instead.
// Order ID
form.elements['order_id'] = {
type: 'hidden',
default_value: order_id
};
form.elements['billing_information'] = {
type: 'addressfield_form_element',
title: t('Billing information'),
default_country: 'US',
required: true,
value_callback: 'addressfield_field_value_callback',
components: {
name_line: true,
thoroughfare: true,
premise: true
}
};
// Buttons
form.elements['submit'] = {
type: 'submit',
value: 'Continue to next step'
};
form.buttons['cancel'] = drupalgap_form_cancel_button();
form.buttons['cancel'].title = t('Go back');
return form;
}
catch (error) { console.log('commerce_checkout_view - ' + error); }
}
/**
* The checkout page pageshow handler.
*/
function commerce_checkout_view_pageshow(form_id, order_id) {
try {
// Load the order, so we can inject potential addressfield info
// into the pane via a services post process hook
commerce_order_load(order_id, {
success: function(order) {
}
});
//commerce_checkout_customer_profile_copy_toggle();
}
catch (error) { console.log('commerce_checkout_view_pageshow - ' + error); }
}
/**
*
*/
function commerce_checkout_view_validate(form, form_state) {
try {
// If the shipping info checkbox is checked, fill in the shipping fields
// with the billing fields.
if (form_state.values['customer_profile_copy']) {
var names = commerce_checkout_shipping_element_names();
$.each(names, function(index, name) {
var _name = name.replace('shipping', 'billing');
form_state.values[name] = form_state.values[_name];
});
}
}
catch (error) { console.log('commerce_checkout_view_validate - ' + error); }
}
/**
*
*/
function commerce_checkout_view_submit(form, form_state) {
try {
//console.log(form_state.values);
variable_set('commerce_checkout_form_state', form_state);
var order_id = form_state.values.order_id;
// Load the order...
commerce_order_load(order_id, {
success: function(order) {
//console.log(order);
// When continuing, send them along to review their order unless shipping is enabled,
// then send them off to shipping first.
var done = function() {
drupalgap_goto(
drupalgap.commerce.commerce_shipping ?
'checkout/shipping/' + order_id : 'checkout/review/' + order_id
);
};
// If there is no billing customer profile on the order, create the customer
// profile and attach it to the order,
if (!order.commerce_customer_billing) {
commerce_customer_profile_create({
type: 'billing',
commerce_customer_address: form_state.values.billing_information
}, {
success: function(customer_profile) {
order.commerce_customer_billing = customer_profile.profile_id;
commerce_order_update(order, { success: done });
}
});
}
else {
// They already have a billing customer profile on the order, update the customer profile
// then continue.
var profile_id = order.commerce_customer_billing;
var customer_profile = order.commerce_customer_billing_entities[profile_id];
customer_profile.commerce_customer_address = form_state.values.billing_information;
commerce_customer_profile_update(customer_profile, { success: done });
}
}
});
}
catch (error) { console.log('commerce_checkout_view_submit - ' + error); }
}
function commerce_checkout_review_order_container_id(order_id) {
return 'commerce-checkout-review-order-' + order_id;
}
function commerce_checkout_review_order_page(order_id) {
return '<div id="' + commerce_checkout_review_order_container_id(order_id) + '"></div>';
}
function commerce_checkout_review_order_pageshow(order_id) {
commerce_order_load(order_id, {
success: function(order) {
var content = {};
// Render line items.
var items = [];
$.each(order.commerce_line_items_entities, function(line_item_id, line_item) {
var item = theme('commerce_cart_line_item_review', {
line_item: line_item,
order: order
});
items.push(item);
});
content['line-items'] = {
theme: 'jqm_item_list',
items: items
};
// Render the order total..
content['order-total'] = {
theme: 'commerce_cart_total',
order: order
};
// Render the review order form.
content['form'] = {
markup: drupalgap_get_form('commerce_checkout_review_order_view', order)
};
// Inject the html into the container
var id = commerce_checkout_review_order_container_id(order_id);
$('#' + id).html(drupalgap_render(content)).trigger('create');
}
});
}
/**
*
*/
function commerce_checkout_review_order_view(form, form_state, order) {
try {
//console.log(order);
// Grab the checkout form state.
//var checkout_info = JSON.parse(variable_get('commerce_checkout_form_state', {}));
// Order ID
var order_id = order.order_id;
form.elements['order_id'] = {
type: 'hidden',
default_value: order_id
};
// Account Information
var markup = '<p><strong>Username</strong>: ' + Drupal.user.name + '</p>';
markup += '<p><strong>E-mail address</strong>: ' + Drupal.user.mail + '</p>';
form.elements['account_information'] = {
title: t('Account information'),
markup: markup
};
// Billing Information
var profile_id = order.commerce_customer_billing;
var customer_profile = order.commerce_customer_billing_entities[profile_id];
form.elements['billing_information'] = {
title: t('Billing information'),
markup: theme('addressfield', customer_profile.commerce_customer_address)
};
// Shipping Information
if (drupalgap.commerce.commerce_shipping) {
profile_id = order.commerce_customer_shipping;
customer_profile = order.commerce_customer_shipping_entities[profile_id];
form.elements['shipping_information'] = {
title: t('Shipping information'),
markup: theme('addressfield', customer_profile.commerce_customer_address)
};
}
// Buttons
form.elements['submit'] = {
type: 'submit',
value: t('Continue to next step')
};
form.buttons['cancel'] = drupalgap_form_cancel_button();
form.buttons['cancel'].title = t('Go back');
return form;
}
catch (error) { console.log('commerce_checkout_review_order_view - ' + error); }
}
/**
*
*/
function commerce_checkout_review_order_view_submit(form, form_state) {
try {
drupalgap_goto('checkout/payment/' + form_state.values['order_id']);
}
catch (error) { console.log('commerce_checkout_review_order_view_submit - ' + error); }
}
/**
*
*/
function commerce_checkout_element_names() {
return [
'name_line',
'country',
'thoroughfare',
'premise',
'locality',
'administrative_area',
'postal_code'
];
}
/**
*
*/
function commerce_checkout_billing_element_names() {
try {
var names = commerce_checkout_element_names();
var shipping_names = [];
$.each(names, function(index, name) {
shipping_names.push('billing_' + name)
});
return shipping_names;
}
catch (error) { console.log('commerce_checkout_billing_element_names - ' + error); }
}
/**
*
*/
function commerce_checkout_shipping_element_names() {
try {
var names = commerce_checkout_element_names();
var shipping_names = [];
$.each(names, function(index, name) {
shipping_names.push('shipping_' + name)
});
return shipping_names;
}
catch (error) { console.log('commerce_checkout_shipping_element_names - ' + error); }
}
/**
*
*/
function _commerce_checkout_customer_profile_copy_onclick() {
try {
commerce_checkout_customer_profile_copy_toggle();
}
catch (error) { console.log('_commerce_checkout_customer_profile_copy_onclick - ' + error); }
}
/**
* Completes the checkout process.
* @param {Object} options
*/
function commerce_checkout_complete(options) {
try {
options.method = 'POST';
options.contentType = 'application/x-www-form-urlencoded';
options.path = 'checkout_complete.json';
if (typeof options.flatten_fields !== 'undefined' && options.flatten_fields === false) {
options.path += '&flatten_fields=false';
}
options.service = 'checkout_complete';
options.resource = 'create';
// Since the service resource is expecting URL encoded data, change the data
// object into a string.
if (options.data) {
var data = '';
for (var property in options.data) {
if (options.data.hasOwnProperty(property)) {
data += property + '=' + options.data[property] + '&';
}
}
// Remove last ampersand.
if (data != '') {
data = data.substring(0, data.length - 1);
options.data = data;
}
}
Drupal.services.call(options);
}
catch (error) {
console.log('commerce_checkout_complete - ' + error);
}
}
function commerce_customer_profile_primary_key() {
return 'profile_id';
}
/**
* Saves a line item.
* @param {Object} line_item
* @param {Object} options
*/
function commerce_line_item_save(line_item, options) {
try {
commerce_line_item_update(line_item, options);
}
catch (error) { console.log('commerce_line_item_save - ' + error); }
}
/**
* Loads a product display.
* @param {Number} ids
* @param {Object} options
*/
function commerce_product_display_load(ids, options) {
try {
commerce_product_display_retrieve(ids, options);
}
catch (error) { console.log('commerce_product_display_load - ' + error); }
}
/**
* Loads a commerce order.
* @param {Number} ids
* @param {Object} options
*/
function commerce_order_load(ids, options) {
try {
commerce_order_retrieve(ids, options);
}
catch (error) { console.log('commerce_order_load - ' + error); }
}
/**
* Saves a commerce order.
* @param {Object} order
* @param {Object} options
*/
function commerce_order_save(order, options) {
try {
commerce_order_update(order, options);
}
catch (error) { console.log('commerce_order_save - ' + error); }
}
/**
* Loads a commerce product.
* @param {Number} ids
* @param {Object} options
*/
function commerce_product_load(ids, options) {
try {
commerce_product_retrieve(ids, options);
}
catch (error) { console.log('commerce_product_load - ' + error); }
}
/**
*
*/
function commerce_cart_add_to_cart_form(form, form_state, product_display) {
try {
//console.log('commerce_cart_add_to_cart_form');
//console.log(product_display);
// Set the global product display variable so we have access to it later.
_commerce_product_display = product_display;
// Prep array to hold attribute field names.
_commerce_product_attribute_field_names = [];
// Clear the referenced product id.
_commerce_product_display_product_id = null;
// Set the form entity type and bundle.
form.entity_type = 'commerce_product';
form.bundle = product_display.type;
// Determine the internal commerce field name for the product
// display's entity reference(s). Note, this is not the same field
// name used by the Product reference field on the node content
// type.
var product_entities_field_name = commerce_get_content_type_product_reference_entities_field_name(product_display.type);
// If there are any product entities...
if (product_display[product_entities_field_name]) {
//console.log('commerce_product field_info_instances');
//console.log(drupalgap.field_info_instances);
// Locate the first product referenced product's type and use it for the field_info_instances.
var product_type = null;
$.each(product_display[product_entities_field_name], function(pid, product) {
product_type = product.type;
return false;
});
var field_info_instances = drupalgap_field_info_instances('commerce_product', product_type);
if (!field_info_instances) {
// Failed to load the instances, throw some informative warnings.
console.log('WARNING: commerce_cart_add_to_cart_form() - no field instances were located for ' + product_type + '');
return;
}
// For each field instance on this product, if it has cart
// settings, and is an attributed field, iterate over each product
// and extract the form element items required to build the
// widget.
$.each(field_info_instances, function(field_name, field) {
// Skip fields that aren't part of the cart (e.g. price, images).
// @TODO - These fields need be rendered as markup.
if (
typeof field.commerce_cart_settings === 'undefined' ||
typeof field.commerce_cart_settings.attribute_field === 'undefined'
) { return; }
// Save this field name for later.
_commerce_product_attribute_field_names.push(field_name);
// What widget module and widget type are being used on this
// field?
var module = field.widget.module;
var type = field.widget.type;
// Depending on the module, let's handle the widget type.
//dpm(field_name);
//dpm(field);
switch (module) {
// Handle options.
case 'options':
switch (type) {
// Handle select options.
case 'options_select':
// Since this is a field, we need to bundle it into the
// the language code as an item.
form.elements[field_name] = {
title: field.label,
type: 'select',
required: field.required,
default_value: 1
};
// Build the field items (only one), then go over each product,
// extracting the options for this field, skipping any that are
// already set. Then attach an onchange handler and attach the
// field items to the element. Save a reference to the first
// product id, in case the user adds the default product to
// the cart.
var field_items = { 0: { options: { attributes: {
onchange: '_commerce_cart_attribute_change()',
'class': '_commerce_cart_attribute',
field_name: field_name
} } } };
$.each(product_display[product_entities_field_name], function(product_id, product) {
if (!_commerce_product_display_product_id) { _commerce_product_display_product_id = product_id; }
// Depending on the field type, the value can be bundled
// differently. For example, Commerce Kickstart uses
// taxonomy term reference fields, which have a tid as the
// value, whereas a List (text) field can have multiple
// types of values, string, number, etc. So if an int
// isn't extracted, just use the straight value.
var value = product[field_name];
var label = product[field_name];
if (typeof product[field_name + '_taxonomy_term_name'] !== 'undefined') {
value = parseInt(product[field_name])
label = product[field_name + '_taxonomy_term_name'];
}
if (typeof field_items[0].options[value] !== 'undefined') { return; }
field_items[0].options[value] = label;
console.log(JSON.stringify(field_items[0].options));
});
form.elements[field_name][product_display.language] = field_items;
break;
default:
console.log('WARNING: commerce_cart_add_to_cart_form - unsupported type (' + type + ')');
break;
}
break;
default:
console.log('WARNING: commerce_cart_add_to_cart_form - unsupported module (' + module + ')');
break;
}
});
}
// Add to cart submit button.
form.elements.submit = {
type: 'submit',
value: 'Add to cart'
};
return form;
}
catch (error) { console.log('commerce_cart_add_to_cart_form - ' + error); }
}
/**
* Define the form's submit function.
*/
function commerce_cart_add_to_cart_form_submit(form, form_state) {
try {
// Get the user's current cart.
commerce_cart_index(null, {
success: function(result) {
//dpm('commerce_cart_index');
//dpm(result);
if (result.length == 0) {
// The cart doesn't exist yet, create it, then add the line item to it.
commerce_cart_create({
success: function(order) {
_commerce_line_item_add_to_order({
order: order,
success: _commerce_cart_add_to_cart_form_submit_success
});
}
});
}
else {
// The cart already exists, add the line item to it.
$.each(result, function(order_id, order) {
_commerce_line_item_add_to_order({
order: order,
success: _commerce_cart_add_to_cart_form_submit_success
});
return false; // Process only one cart.
});
}
}
});
}
catch (error) { console.log('commerce_cart_add_to_cart_form_submit - ' + error); }
}
/**
*
*/
function _commerce_cart_add_to_cart_form_submit_success(result) {
try {
drupalgap_goto('cart', { reloadPage: true });
}
catch (error) { console.log('_commerce_cart_add_to_cart_form_submit_success - ' + error); }
}
// Holds onto order objects, keyed by order_id.
var _commerce_order = {};
// Holds onto the current product display.
var _commerce_product_display = null;
// Holds onto the current product display's referenced product entity field names.
var _commerce_product_attribute_field_names = null;
// Holds onto the current product display's referenced product id.
var _commerce_product_display_product_id = null;
function commerce_get_content_type_product_reference_field_name(type) {
return drupalgap.settings.commerce.bundles[type].product_reference_field_name;
}
function commerce_get_content_type_product_reference_entities_field_name(type) {
return commerce_get_content_type_product_reference_field_name(type) + '_entities';
}
/**
* The click handler for the "Remove" button on a line item on the cart page.
*/
function _commerce_cart_line_item_remove(order_id, line_item_id) {
try {
commerce_line_item_delete(line_item_id, {
success: function(result) {
// @TODO - once DG core supports reloading the same page, we can just
// make a call to drupalgap_goto(). Just re-run the pageshow event
// handler.
//drupalgap_goto('cart', { reloadPage: true });
commerce_cart_view_pageshow();
}
});
}
catch (error) { console.log('_commerce_cart_line_item_remove - ' + error); }
}
/**
* Handle clicks on the "Update cart" button.
*/
function commerce_cart_button_update_click(order_id) {
try {
// @TODO - this is working, but since it appears we can't use the order
// update resource to make one single call to update line item quantities,
// we have manually update each line item, which results in multiple
// server calls, and multiple cart rebuilds on success.
var order = _commerce_order[order_id];
$.each(order.commerce_line_items_entities, function(line_item_id, line_item) {
var quantity = $('#commerce_cart_line_item_quantity_' + line_item_id).val() + '.00';
var _quantity = order.commerce_line_items_entities[line_item_id].quantity;
if (quantity != _quantity) {
var line_item = {
line_item_id: parseInt(line_item_id),
quantity: quantity
};
commerce_line_item_save(line_item, {
quantity: quantity,
success: function(result) {
commerce_cart_view_pageshow();
}
});
}
});
}
catch (error) { console.log('commerce_cart_button_update_click - ' + error); }
}
/**
*
*/
function commerce_checkout_customer_profile_copy_toggle() {
try {
var checked = $('#edit-commerce-checkout-view-customer-profile-copy').is(':checked');
// Hide the shipping input fields.
var names = commerce_checkout_shipping_element_names();
$.each(names, function(index, name) {
var selector = '.' + drupalgap_form_get_element_container_class(name).replace('form-item ', '');
if (!checked) { $(selector).show(); }
else { $(selector).hide(); }
});
}
catch (error) { console.log('commerce_checkout_customer_profile_copy_toggle - ' + error); }
}
/**
* Implements hook_install().
*/
function commerce_install() {
drupalgap_add_css(drupalgap_get_path('module', 'commerce') + '/commerce.css');
// Init default settings if there aren't any.
if (typeof drupalgap.settings.commerce === 'undefined') {
console.log('WARNING - drupalgap.settings.commerce is undefined, view the README file for commerce.js');
}
}
/**
* Implements hook_menu().
*/
function commerce_menu() {
try {
var items = {};
items['cart'] = {
'title': 'Shopping cart',
'page_callback': 'commerce_cart_view',
'pageshow': 'commerce_cart_view_pageshow'
};
items['checkout/%'] = {
'title': 'Checkout',
'page_callback': 'drupalgap_get_form',
'page_arguments': ['commerce_checkout_view', 1],
'pageshow': 'commerce_checkout_view_pageshow'
};
items['checkout/shipping/%'] = {
'title': 'Shipping',
'page_callback': 'drupalgap_get_form',
'page_arguments': ['commerce_checkout_shipping_view', 2]
};
items['checkout/review/%'] = {
'title': 'Review order',
'page_callback': 'commerce_checkout_review_order_page',
'page_arguments': [2],
'pageshow': 'commerce_checkout_review_order_pageshow'
};
items['checkout/complete/%'] = {
'title': 'Checkout complete',
'page_callback': 'commerce_checkout_complete_view',
'page_arguments': [2]
};
return items;
}
catch (error) { console.log('commerce_menu - ' + error); }
}
/**
* Implements hook_services_success().
*/
function commerce_services_postprocess(options, data) {
try {
//console.log(options.service, options.resource);
// Extract the commerce object from the system connect result data.
if (options.service == 'system' && options.resource == 'connect') {
if (data.commerce) { drupalgap.commerce = data.commerce; }
else {
console.log('commerce_services_postprocess - failed to extract ' +
' commerce object from system connect. Is the commerce_drupalgap ' +
' module enabled on your Drupal site?');
}
}
else if (options.service == 'commerce_order' && options.resource == 'retrieve') {
// Set aside orders when they are loaded.
_commerce_order[data.order_id] = data;
}
// When retrieving address information, inject it into the order's address fields if possible.
else if (options.service == 'services_addressfield' &&
options.resource == 'get_address_format_and_administrative_areas') {
switch (drupalgap_router_path_get()) {
case 'checkout/%':
_commerce_addressfield_inject_onto_pane(_commerce_order[arg(1)]);
break;
case 'checkout/shipping/%':
_commerce_addressfield_inject_onto_pane(_commerce_order[arg(2)]);
break;
}
}
}
catch (error) { console.log('commerce_services_postprocess - ' + error); }
}
/**