-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
160 lines (154 loc) · 6.52 KB
/
cart.html
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
{% extends 'base.html' %}
<!-- Title Page -->
<!-- Cart -->
{% block title %}
Cart
{% endblock %}
{% block section %}
<section class="cart bgwhite p-t-70 p-b-100">
<div class="container">
<!-- Cart item -->
<div class="container-table-cart pos-relative">
<div class="wrap-table-shopping-cart bgwhite">
<table class="table-shopping-cart">
<tr class="table-head">
<th class="column-1"></th>
<th class="column-2">Product</th>
<th class="column-3">Price</th>
<th class="column-4 p-l-70">Quantity</th>
<th class="column-4">Available</th>
<th class="column-5">Total</th>
</tr>
</table>
</div>
</div>
<div class="flex-w flex-sb-m p-t-25 p-b-25 bo8 p-l-35 p-r-60 p-lr-15-sm">
<div class="size10 trans-0-4 m-t-10 m-b-10">
<!-- Button -->
<B>ALL TOTAL</B>
</div>
<p class="stop column-5" id="total">$00.00</p>
</div>
<div class="flex-w flex-sb-m p-t-25 p-b-25 bo8 p-l-35 p-r-60 p-lr-15-sm">
<div class="size10 trans-0-4 m-t-10 m-b-10">
<!-- Button -->
<button class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4" id="update">
Update Cart
</button>
</div>
<div class="size10 trans-0-4 m-t-10 m-b-10">
<button type="submit" class="flex-c-m column-5 sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4" id="sell">
Sell
</button>
</div>
</div>
<!-- Total -->
</div>
</section>
{% endblock %}
{% block js %}
<script>
var cartItems = JSON.parse(localStorage.getItem('items'));
var hold = document.getElementsByTagName('tbody')[0]
if ((cartItems) && (cartItems.length > 0)) {
for (i = 0; i < cartItems.length; i++) {
var table_row = document.createElement('tr');
var column_1 = document.createElement('td');
var cart_img = document.createElement('div');
var img = document.createElement('img');
img.src = "/static/img/" + cartItems[i][4]
img.alt = 'IMG-PRODUCT'
cart_img.appendChild(img)
cart_img.className = "cart-img-product b-rad-4 o-f-hidden"
column_1.appendChild(cart_img)
column_1.className = "column-1"
table_row.appendChild(column_1)
table_row.className = "table-row"
var column_2 = document.createElement("td");
column_2.className = "column-2"
column_2.innerText = cartItems[i][1]
column_2.id = cartItems[i][0];
table_row.appendChild(column_2)
var column_3 = document.createElement("td");
column_3.className = "column-3";
column_3.innerHTML = "$<span>" + cartItems[i][3] + "</span>.00"
table_row.appendChild(column_3);
var column_4 = document.createElement("td");
column_4.className = "column-4";
var div_btn = document.createElement("div");
div_btn.className = "flex-w bo5 of-hidden w-size17";
var bt_down = document.createElement("button");
bt_down.className = 'btn-num-product-down color1 flex-c-m size7 bg8 eff2';
var bt_down_i = document.createElement('i');
bt_down_i.className = 'fs-12 fa fa-minus';
bt_down_i.setAttribute = 'aria-hidden=true';
bt_down.appendChild(bt_down_i);
div_btn.appendChild(bt_down);
column_4.appendChild(div_btn);
var item_val = document.createElement('input');
item_val.className = "size8 m-text18 t-center num-product";
item_val.value = 0;
item_val.type = "number";
div_btn.appendChild(item_val);
column_4.appendChild(div_btn);
var bt_up = document.createElement("button");
bt_up.className = 'btn-num-product-up color1 flex-c-m size7 bg8 eff2';
var bt_up_i = document.createElement('i');
bt_up_i.className = 'fs-12 fa fa-plus';
bt_up_i.setAttribute = 'aria-hidden=true';
bt_up.appendChild(bt_up_i);
div_btn.appendChild(bt_up);
column_4.appendChild(div_btn);
table_row.appendChild(column_4);
var column_4b = document.createElement('td');
column_4b.className = 'column-4';
column_4b.innerText = cartItems[i][2];
table_row.appendChild(column_4b);
var column_5 = document.createElement("td");
column_5.className = "column-5";
column_5.innerHTML = "$<span class='total'>0</span>.00"
table_row.appendChild(column_5);
hold.appendChild(table_row);
}
}
else{
document.getElementsByClassName('container')[0].innerHTML = '<b>NO ITEM IN CART YET</b>'
}
$('#update').on("click", function (e) {
var all_total = [];
e.preventDefault();
x = $('.total')
if (x.length > 0) {
for (i = 0; i < x.length; i++) {
all_total.push(parseInt(x[i].innerText));
}
$('.stop')[0].innerText = '$' + all_total.reduce((a, b) => a + b, 0) + '.00';
}
})
var cart = []
$('#sell').on('click', function(e){
e.preventDefault()
var all_columns = $('.column-2')
for (i=0; i<all_columns.length; i++){
var good_id = all_columns[i].id
if (good_id !== ''){
var good_name = all_columns[i].innerText
var good_bought = all_columns[i].parentElement.childNodes[3].children[0].children[1].value
cart.push([good_id, good_name, good_bought])
}
}
cart = JSON.stringify(cart)
console.log(cart)
$.getJSON(window.location.protocol + '//' + document.domain + ':' + window.location.port + '/sale', {
cart: cart
},
function(data){
if(data.response === 'success'){
alert('total cost: ' + data.cost)
localStorage.removeItem('items');
document.getElementsByClassName('container')[0].innerHTML = '<b>NO ITEM IN CART YET</b>'
}
})
})
</script>
{% endblock %}