-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcart.php
More file actions
145 lines (121 loc) · 5.16 KB
/
cart.php
File metadata and controls
145 lines (121 loc) · 5.16 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
<?php require('header.php'); ?>
<!-- Page Content -->
<div class="container-fluid" >
<?php if (count($cart->items) > 0) { ?>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h3>Shopping cart</h3>
<p>You have <?php echo count($cart->items); ?> items in your cart.</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th colspan="2">Product</th>
<th>Quantity</th>
<th>Unit price</th>
<th colspan="2">Total</th>
</tr>
</thead>
<tbody>
<?php for ( $i=0; $i< count($cart->items); $i++) {
$item = $cart->items[$i];
?>
<tr>
<td>
<a href="product.php?id=<?php echo $item->id; ?>">
<img src="<?php echo $item->images[0]; ?>" alt="<?php echo $item->name ; ?>" style="width:32px;"></a></td>
<td>
<a href="product.php?id=<?php echo $item->id; ?>"><?php echo $item->name; ?></a>
<div>
<?php if ($item->has_variants === true) {
foreach ($item->variantsDefinition as $v_name => $v_values) { ?>
<span class="label label-primary" style="margin-right:5px">
<?php echo $item->variant->{$v_name} ?>
</span>
<?php } ?>
<?php } ?>
</div>
</td>
<td>
<input type="number"
id="<?php echo "quantity_".$item->id."_".$item->variant_id; ?>"
class="form-control"
value="<?php echo $item->quantity; ?>">
</td>
<?php if ($item->price_discount) { ?>
<td><strike>$ <?php echo $item->price; ?></strike> <span>$ <?php echo $item->price_discount; ?></span></td>
<?php } else { ?>
<td><?php echo $item->price; ?></td>
<?php } ?>
<td>$ <?php echo ($item->price * $item->quantity); ?></td>
<td><a href="#" class="btn btn-danger removeButton" data-product-id="<?php echo $item->product_id; ?>" data-variant-id="<?php echo $item->variant_id; ?>"><i class="fa fa-trash"></i> Remove</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-xs-12 text-right">
<button class="btn btn-primary" id="updateCartButton">Update cart</button>
<a href="/checkout.php" class="btn btn-success btn-lg">Checkout</a>
</div>
</div>
</div>
</div>
<?php } else { ?>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h4 style="text-align:center;">Your cart is empty :(</h4>
</div>
</div>
<?php } ?>
</div>
<!-- /.container -->
<div class="container">
<hr>
<!-- Footer -->
<footer>
<div class="row">
<div class="col-lg-12">
<p>Copyright © Your Website 2014</p>
</div>
</div>
</footer>
</div>
<!-- /.container -->
<script type="text/javascript">
var cart = <?php echo json_encode($cart) ?>;
function updateCart() {
var payload = [];
cart.items.forEach(function(item){
payload.push({
product_id : item.product_id,
variant_id : item.variant_id,
quantity : Number($("input#quantity_"+item.product_id+"_"+item.variant_id).val())
})
})
marketcloud.carts.update(cart.id,payload,function(err,data){
if (err)
alert("An error has occurred.")
else
alert("Cart updated")
})
}
$("#updateCartButton").click(updateCart);
$(".removeButton").click(function(e){
var btn = $(e.target);
var product_id = btn.data('product-id');
var variant_id = btn.data('variant-id');
var payload = [{
product_id : product_id,
variant_id : variant_id
}]
marketcloud.carts.remove(cart.id,payload,function(err,data){
if (err)
return alert("An error has occurred");
alert("Cart updated");
window.location.reload()
})
})
</script>
<?php include("footer.php"); ?>