-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
customer.js
102 lines (98 loc) · 3.57 KB
/
customer.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
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*global define*/
define(
[
'jquery',
'ko',
'underscore',
'./address-list'
],
function($, ko, _, addressList) {
"use strict";
var isLoggedIn = ko.observable(window.isCustomerLoggedIn),
customerData = {};
if (isLoggedIn()) {
customerData = window.customerData;
} else {
customerData = {};
}
return {
customerData: customerData,
customerDetails: {},
isLoggedIn: isLoggedIn,
setIsLoggedIn: function (flag) {
isLoggedIn(flag);
},
getBillingAddressList: function () {
return addressList();
},
getShippingAddressList: function () {
return addressList();
},
setDetails: function (fieldName, value) {
if (fieldName) {
this.customerDetails[fieldName] = value;
}
},
getDetails: function (fieldName) {
if (fieldName) {
if (this.customerDetails.hasOwnProperty(fieldName)) {
return this.customerDetails[fieldName];
}
return undefined;
} else {
return this.customerDetails;
}
},
addCustomerAddress: function (address) {
var fields = [
'customer_id', 'country_id', 'street', 'company', 'telephone', 'fax', 'postcode', 'city',
'firstname', 'lastname', 'middlename', 'prefix', 'suffix', 'vat_id', 'default_billing',
'default_shipping'
],
customerAddress = {},
hasAddress = 0,
existingAddress;
if (!this.customerData.addresses) {
this.customerData.addresses = [];
}
customerAddress = _.pick(address, fields);
if (address.hasOwnProperty('region_id')) {
customerAddress.region = {
region_id: address.region_id,
region: address.region
};
}
for (existingAddress in this.customerData.addresses) {
if (this.customerData.addresses.hasOwnProperty(existingAddress)) {
if (_.isEqual(this.customerData.addresses[existingAddress], customerAddress)) {
hasAddress = existingAddress;
break;
}
}
}
if (hasAddress === 0) {
return this.customerData.addresses.push(customerAddress) - 1;
}
return hasAddress;
},
setAddressAsDefaultBilling: function (addressId) {
if (this.customerData.addresses[addressId]) {
this.customerData.addresses[addressId].default_billing = 1;
return true;
}
return false;
},
setAddressAsDefaultShipping: function (addressId) {
if (this.customerData.addresses[addressId]) {
this.customerData.addresses[addressId].default_shipping = 1;
return true;
}
return false;
}
};
}
);