1: <?php
2: namespace Izberg\Resource;
3: use Izberg\Resource;
4:
5: class Cart extends Resource
6: {
7: 8: 9: 10: 11:
12: public function getItems($params = null, $accept_type = "Accept: application/json")
13: {
14: $list = self::$Izberg->Call("cart/".$this->id."/items/", 'GET', $params, $accept_type);
15: $object_list = array();
16: if (!isset($list->objects))
17: return null;
18: foreach ($list->objects as $object)
19: {
20: $obj = new CartItem();
21: $obj->hydrate($object);
22: $object_list[] = $obj;
23: }
24: if (!isset($this->items)) {
25: $this->items = array();
26: }
27: $this->items = array_merge($this->items, $object_list);
28: return $object_list;
29: }
30:
31: 32: 33: 34: 35:
36: public function addItem($params = null, $accept_type = 'Content-Type: application/json')
37: {
38:
39:
40:
41:
42:
43:
44: $response = self::$Izberg->Call("cart/".($this->id ? $this->id : "none")."/items/", 'POST', $params, $accept_type);
45: $object = new CartItem();
46: $object->hydrate($response);
47: $this->items[] = $object;
48: return $object;
49: }
50:
51: 52: 53: 54: 55:
56: public function updateItem($id, $params = null, $accept_type = 'Accept: application/json')
57: {
58:
59:
60:
61:
62:
63:
64: $object = new CartItem();
65: $response = parent::$Izberg->Call($object->getName()."/".$id."/", "PUT", $params, $accept_type);
66: $object->hydrate($response);
67: return $object;
68: }
69:
70: 71: 72: 73: 74:
75: public function setShippingAddress($id)
76: {
77: $params["shipping_address"] = "/v1/address/".$id."/";
78: $this->shipping_address = "/v1/address/".$id."/";
79: return parent::$Izberg->update('Cart', $this->id, $params);
80: }
81:
82:
83: 84: 85: 86: 87:
88: public function setBillingAddress($id)
89: {
90: $params["billing_address"] = "/v1/address/".$id."/";
91: $this->billing_address = "/v1/address/".$id."/";
92: return parent::$Izberg->update('Cart', $this->id, $params);
93: }
94:
95: public function createOrder($params = null, $accept_type = 'Content-Type: application/json')
96: {
97: $object = new Order();
98: $response = parent::$Izberg->Call("cart/" . $this->id . "/createOrder/", 'POST', $params, $accept_type);
99: $object->hydrate($response);
100: return $object;
101: }
102:
103: public function addOffer($product_offer_id,$quantity = 1)
104: {
105: $params = array(
106: 'offer_id'=> $product_offer_id,
107: 'quantity'=> $quantity
108: );
109: return parent::$Izberg->Call($this->getName()."/items/", "POST", $params);
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function removeItem($id, $accept_type = 'Accept: application/json')
119: {
120: $object = new CartItem();
121: $response = parent::$Izberg->Call($object->getName()."/".$id."/", "DELETE", array(), $accept_type);
122: $object->hydrate($response);
123: return $object;
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function discountCode($code, $action = "add")
133: {
134: $params = array('discount_code'=> $code);
135: $id = $this->id ? $this->id : 'mine';
136: return parent::$Izberg->Call("cart/" . $id . "/" . $action . "_discount_code/", "POST", $params, 'Content-Type: application/json');
137: }
138:
139: 140: 141: 142:
143: public function clean() {
144: $this->getItems();
145: foreach ($this->items as $item) {
146: $item->delete();
147: }
148: return true;
149: }
150:
151:
152: 153: 154: 155:
156: public function shippingOptions() {
157: $id = $this->id ? $this->id : 'mine';
158: return parent::$Izberg->Call("cart/" . $id . "/shipping_options/" , "GET", null, 'Content-Type: application/json');
159: }
160:
161: 162: 163: 164:
165: public function shippingOption($option_id) {
166: return parent::$Izberg->Call("cart_shipping_choice/" . $option_id . "/" , "GET", null, 'Content-Type: application/json');
167: }
168:
169: 170: 171: 172:
173: public function selectShippingOption($option_id) {
174: $id = $this->id ? $this->id : 'mine';
175: return parent::$Izberg->Call("cart/" . $id . "/shipping_options/" . $option_id . "/select/" , "POST", array(), 'Content-Type: application/json');
176: }
177:
178: 179: 180: 181: 182:
183: public function selectShippingOptions($params) {
184: $id = $this->id ? $this->id : 'mine';
185: return parent::$Izberg->Call("cart/" . $id . "/shipping_options/" , "POST", $params, 'Content-Type: application/json');
186: }
187:
188: 189: 190: 191:
192: public function updateShippingOptions() {
193: $id = $this->id ? $this->id : 'mine';
194: return parent::$Izberg->Call("cart/" . $id . "/shipping_options/update/" , "POST", null, 'Content-Type: application/json');
195: }
196: }
197: