Repository navigation
Expand file tree
/
Copy pathCartClass.php
More file actions
64 lines (53 loc) · 1.55 KB
/
Copy pathCartClass.php
File metadata and controls
64 lines (53 loc) · 1.55 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
<?php
// AI tools were used during development to assist developers
// Robert Walters and Thadd McLeod
class CartClass
{
private $id;
private $products; //Associative array of products and quantities
private $customer;
public function __construct($customer)
{
$this->products = [];
$this->customer = $customer;
}
public function getProducts(){
return $this->products;
}
public function getCustomer(){
return $this->customer;
}
public function getTotalPrice(){
foreach($this->products as $product){
$price = $product->getPrice();
}
}
public function addProduct(ProductClass $product, $qty = 1) {
$id = $product->getId();
if (isset($this->products[$id])) {
$this->products[$id]['quantity'] += $qty;
} else {
$this->products[$id] = [
"product" => $product,
"quantity" => $qty
];
}
}
public function removeProduct(ProductClass $product){
$id = $product->getId();
if(isset($this->products[$id])){
$this->products[$id]['quantity'] = 0;
unset($this->products[$id]);
}
}
public function decreaseProductQty(ProductClass $product){
$id = $product->getId();
if(isset($this->products[$id])){
if($this->products[$id]['quantity'] === 1){
$this->removeProduct($product);
}else {
$this->products[$id]['quantity']--;
}
}
}
}