1: <?php
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: class Mage_Checkout_Model_Cart_Product_Api extends Mage_Checkout_Model_Api_Resource_Product
36: {
37: 38: 39: 40: 41: 42:
43: protected function _prepareProductsData($data)
44: {
45: return is_array($data) ? $data : null;
46: }
47:
48: 49: 50: 51: 52: 53:
54: public function add($quoteId, $productsData, $store=null)
55: {
56: $quote = $this->_getQuote($quoteId, $store);
57: if (empty($store)) {
58: $store = $quote->getStoreId();
59: }
60:
61: $productsData = $this->_prepareProductsData($productsData);
62: if (empty($productsData)) {
63: $this->_fault('invalid_product_data');
64: }
65:
66: $errors = array();
67: foreach ($productsData as $productItem) {
68: if (isset($productItem['product_id'])) {
69: $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
70: } else if (isset($productItem['sku'])) {
71: $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
72: } else {
73: $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
74: continue;
75: }
76:
77: $productRequest = $this->_getProductRequest($productItem);
78: try {
79: $result = $quote->addProduct($productByItem, $productRequest);
80: if (is_string($result)) {
81: Mage::throwException($result);
82: }
83: } catch (Mage_Core_Exception $e) {
84: $errors[] = $e->getMessage();
85: }
86: }
87:
88: if (!empty($errors)) {
89: $this->_fault("add_product_fault", implode(PHP_EOL, $errors));
90: }
91:
92: try {
93: $quote->collectTotals()->save();
94: } catch(Exception $e) {
95: $this->_fault("add_product_quote_save_fault", $e->getMessage());
96: }
97:
98: return true;
99: }
100:
101: 102: 103: 104: 105: 106:
107: public function update($quoteId, $productsData, $store=null)
108: {
109: $quote = $this->_getQuote($quoteId, $store);
110: if (empty($store)) {
111: $store = $quote->getStoreId();
112: }
113:
114: $productsData = $this->_prepareProductsData($productsData);
115: if (empty($productsData)) {
116: $this->_fault('invalid_product_data');
117: }
118:
119: $errors = array();
120: foreach ($productsData as $productItem) {
121: if (isset($productItem['product_id'])) {
122: $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
123: } else if (isset($productItem['sku'])) {
124: $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
125: } else {
126: $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
127: continue;
128: }
129:
130:
131: $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem,
132: $this->_getProductRequest($productItem));
133: if (is_null($quoteItem->getId())) {
134: $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
135: continue;
136: }
137:
138: if ($productItem['qty'] > 0) {
139: $quoteItem->setQty($productItem['qty']);
140: }
141: }
142:
143: if (!empty($errors)) {
144: $this->_fault("update_product_fault", implode(PHP_EOL, $errors));
145: }
146:
147: try {
148: $quote->save();
149: } catch(Exception $e) {
150: $this->_fault("update_product_quote_save_fault", $e->getMessage());
151: }
152:
153: return true;
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function remove($quoteId, $productsData, $store=null)
163: {
164: $quote = $this->_getQuote($quoteId, $store);
165: if (empty($store)) {
166: $store = $quote->getStoreId();
167: }
168:
169: $productsData = $this->_prepareProductsData($productsData);
170: if (empty($productsData)) {
171: $this->_fault('invalid_product_data');
172: }
173:
174: $errors = array();
175: foreach ($productsData as $productItem) {
176: if (isset($productItem['product_id'])) {
177: $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
178: } else if (isset($productItem['sku'])) {
179: $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
180: } else {
181: $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
182: continue;
183: }
184:
185: try {
186:
187: $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem,
188: $this->_getProductRequest($productItem));
189: if (is_null($quoteItem->getId())) {
190: $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
191: continue;
192: }
193: $quote->removeItem($quoteItem->getId());
194: } catch (Mage_Core_Exception $e) {
195: $errors[] = $e->getMessage();
196: }
197: }
198:
199: if (!empty($errors)) {
200: $this->_fault("remove_product_fault", implode(PHP_EOL, $errors));
201: }
202:
203: try {
204: $quote->save();
205: } catch(Exception $e) {
206: $this->_fault("remove_product_quote_save_fault", $e->getMessage());
207: }
208:
209: return true;
210: }
211:
212: 213: 214: 215: 216:
217: public function items($quoteId, $store = null)
218: {
219: $quote = $this->_getQuote($quoteId, $store);
220: if (empty($store)) {
221: $store = $quote->getStoreId();
222: }
223:
224: if (!$quote->getItemsCount()) {
225: return array();
226: }
227:
228: $productsResult = array();
229: foreach ($quote->getAllItems() as $item) {
230:
231: $product = $item->getProduct();
232: $productsResult[] = array(
233: 'product_id' => $product->getId(),
234: 'sku' => $product->getSku(),
235: 'name' => $product->getName(),
236: 'set' => $product->getAttributeSetId(),
237: 'type' => $product->getTypeId(),
238: 'category_ids' => $product->getCategoryIds(),
239: 'website_ids' => $product->getWebsiteIds()
240: );
241: }
242:
243: return $productsResult;
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function moveToCustomerQuote($quoteId, $productsData, $store=null)
253: {
254: $quote = $this->_getQuote($quoteId, $store);
255:
256: if (empty($store)) {
257: $store = $quote->getStoreId();
258: }
259:
260: $customer = $quote->getCustomer();
261: if (is_null($customer->getId())) {
262: $this->_fault('customer_not_set_for_quote');
263: }
264:
265:
266: $customerQuote = Mage::getModel('sales/quote')
267: ->setStoreId($store)
268: ->loadByCustomer($customer);
269:
270: if (is_null($customerQuote->getId())) {
271: $this->_fault('customer_quote_not_exist');
272: }
273:
274: if ($customerQuote->getId() == $quote->getId()) {
275: $this->_fault('quotes_are_similar');
276: }
277:
278: $productsData = $this->_prepareProductsData($productsData);
279: if (empty($productsData)) {
280: $this->_fault('invalid_product_data');
281: }
282:
283: $errors = array();
284: foreach($productsData as $key => $productItem){
285: if (isset($productItem['product_id'])) {
286: $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
287: } else if (isset($productItem['sku'])) {
288: $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
289: } else {
290: $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
291: continue;
292: }
293:
294: try {
295:
296: $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem,
297: $this->_getProductRequest($productItem));
298: if($quoteItem->getId()){
299: $customerQuote->addItem($quoteItem);
300: $quote->removeItem($quoteItem->getId());
301: unset($productsData[$key]);
302: } else {
303: $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
304: }
305: } catch (Mage_Core_Exception $e) {
306: $errors[] = $e->getMessage();
307: }
308: }
309:
310: if (count($productsData) || !empty($errors)) {
311: $this->_fault('unable_to_move_all_products', implode(PHP_EOL, $errors));
312: }
313:
314: try {
315: $customerQuote
316: ->collectTotals()
317: ->save();
318:
319: $quote
320: ->collectTotals()
321: ->save();
322: } catch (Exception $e) {
323: $this->_fault("product_move_quote_save_fault", $e->getMessage());
324: }
325:
326: return true;
327: }
328: }
329: