PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Checkout/Model/Cart/Product/Api.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 329 lines | 221 code | 43 blank | 65 comment | 45 complexity | e7984d5750a7cda84298ae82e540a559 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Shopping cart api for product
  28. *
  29. * @category Mage
  30. * @package Mage_Checkout
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Checkout_Model_Cart_Product_Api extends Mage_Checkout_Model_Api_Resource_Product
  34. {
  35. protected function _prepareProductsData($data)
  36. {
  37. if (!is_array($data)) {
  38. return null;
  39. }
  40. $_data = array();
  41. if (is_array($data) && is_null($data[0])) {
  42. $_data[] = $data;
  43. } else {
  44. $_data = $data;
  45. }
  46. return $_data;
  47. }
  48. /**
  49. * @param $quoteId
  50. * @param $productsData
  51. * @param $store
  52. * @return bool
  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. $productsData = $this->_prepareProductsData($productsData);
  61. if (empty($productsData)) {
  62. $this->_fault('invalid_product_data');
  63. }
  64. $errors = array();
  65. foreach ($productsData as $productItem) {
  66. if (isset($productItem['product_id'])) {
  67. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  68. } else if (isset($productItem['sku'])) {
  69. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  70. } else {
  71. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  72. continue;
  73. }
  74. $productRequest = $this->_getProductRequest($productItem);
  75. try {
  76. $result = $quote->addProduct($productByItem, $productRequest);
  77. if (is_string($result)) {
  78. Mage::throwException($result);
  79. }
  80. } catch (Mage_Core_Exception $e) {
  81. $errors[] = $e->getMessage();
  82. }
  83. }
  84. if (!empty($errors)) {
  85. $this->_fault("add_product_fault", implode(PHP_EOL, $errors));
  86. }
  87. try {
  88. $quote->collectTotals()->save();
  89. } catch(Exception $e) {
  90. $this->_fault("add_product_quote_save_fault", $e->getMessage());
  91. }
  92. return true;
  93. }
  94. /**
  95. * @param $quoteId
  96. * @param $productsData
  97. * @param $store
  98. * @return bool
  99. */
  100. public function update($quoteId, $productsData, $store=null)
  101. {
  102. $quote = $this->_getQuote($quoteId, $store);
  103. if (empty($store)) {
  104. $store = $quote->getStoreId();
  105. }
  106. $productsData = $this->_prepareProductsData($productsData);
  107. if (empty($productsData)) {
  108. $this->_fault('invalid_product_data');
  109. }
  110. $errors = array();
  111. foreach ($productsData as $productItem) {
  112. if (isset($productItem['product_id'])) {
  113. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  114. } else if (isset($productItem['sku'])) {
  115. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  116. } else {
  117. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  118. continue;
  119. }
  120. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  121. $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem, $this->_getProductRequest($productItem));
  122. if (is_null($quoteItem->getId())) {
  123. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  124. continue;
  125. }
  126. if ($productItem['qty'] > 0) {
  127. $quoteItem->setQty($productItem['qty']);
  128. }
  129. }
  130. if (!empty($errors)) {
  131. $this->_fault("update_product_fault", implode(PHP_EOL, $errors));
  132. }
  133. try {
  134. $quote->save();
  135. } catch(Exception $e) {
  136. $this->_fault("update_product_quote_save_fault", $e->getMessage());
  137. }
  138. return true;
  139. }
  140. /**
  141. * @param $quoteId
  142. * @param $productsData
  143. * @param $store
  144. * @return bool
  145. */
  146. public function remove($quoteId, $productsData, $store=null)
  147. {
  148. $quote = $this->_getQuote($quoteId, $store);
  149. if (empty($store)) {
  150. $store = $quote->getStoreId();
  151. }
  152. $productsData = $this->_prepareProductsData($productsData);
  153. if (empty($productsData)) {
  154. $this->_fault('invalid_product_data');
  155. }
  156. $errors = array();
  157. foreach ($productsData as $productItem) {
  158. if (isset($productItem['product_id'])) {
  159. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  160. } else if (isset($productItem['sku'])) {
  161. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  162. } else {
  163. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  164. continue;
  165. }
  166. try {
  167. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  168. $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem, $this->_getProductRequest($productItem));
  169. if (is_null($quoteItem->getId())) {
  170. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  171. continue;
  172. }
  173. $quote->removeItem($quoteItem->getId());
  174. } catch (Mage_Core_Exception $e) {
  175. $errors[] = $e->getMessage();
  176. }
  177. }
  178. if (!empty($errors)) {
  179. $this->_fault("remove_product_fault", implode(PHP_EOL, $errors));
  180. }
  181. try {
  182. $quote->save();
  183. } catch(Exception $e) {
  184. $this->_fault("remove_product_quote_save_fault", $e->getMessage());
  185. }
  186. return true;
  187. }
  188. /**
  189. * @param $quoteId
  190. * @param $store
  191. * @return array
  192. */
  193. public function items($quoteId, $store = null)
  194. {
  195. $quote = $this->_getQuote($quoteId, $store);
  196. if (empty($store)) {
  197. $store = $quote->getStoreId();
  198. }
  199. if (!$quote->getItemsCount()) {
  200. return array();
  201. }
  202. $productsResult = array();
  203. foreach ($quote->getAllItems() as $item) {
  204. /** @var $item Mage_Sales_Model_Quote_Item */
  205. $product = $item->getProduct();
  206. $productsResult[] = array( // Basic product data
  207. 'product_id' => $product->getId(),
  208. 'sku' => $product->getSku(),
  209. 'set' => $product->getAttributeSetId(),
  210. 'type' => $product->getTypeId(),
  211. 'categories' => $product->getCategoryIds(),
  212. 'websites' => $product->getWebsiteIds()
  213. );
  214. }
  215. return $productsResult;
  216. }
  217. /**
  218. * @param $quoteId
  219. * @param $productsData
  220. * @param $store
  221. * @return bool
  222. */
  223. public function moveToCustomerQuote($quoteId, $productsData, $store=null)
  224. {
  225. $quote = $this->_getQuote($quoteId, $store);
  226. if (empty($store)) {
  227. $store = $quote->getStoreId();
  228. }
  229. $customer = $quote->getCustomer();
  230. if (is_null($customer->getId())) {
  231. $this->_fault('customer_not_set_for_quote');
  232. }
  233. /** @var $customerQuote Mage_Sales_Model_Quote */
  234. $customerQuote = Mage::getModel('sales/quote')
  235. ->setStoreId($store)
  236. ->loadByCustomer($customer);
  237. if (is_null($customerQuote->getId())) {
  238. $this->_fault('customer_quote_not_exist');
  239. }
  240. if ($customerQuote->getId() == $quote->getId()) {
  241. $this->_fault('quotes_are_similar');
  242. }
  243. $productsData = $this->_prepareProductsData($productsData);
  244. if (empty($productsData)) {
  245. $this->_fault('invalid_product_data');
  246. }
  247. $errors = array();
  248. foreach($productsData as $key => $productItem){
  249. if (isset($productItem['product_id'])) {
  250. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  251. } else if (isset($productItem['sku'])) {
  252. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  253. } else {
  254. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  255. continue;
  256. }
  257. try {
  258. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  259. $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem, $this->_getProductRequest($productItem));
  260. if($quoteItem->getId()){
  261. $customerQuote->addItem($quoteItem);
  262. $quote->removeItem($quoteItem->getId());
  263. unset($productsData[$key]);
  264. } else {
  265. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  266. }
  267. } catch (Mage_Core_Exception $e) {
  268. $errors[] = $e->getMessage();
  269. }
  270. }
  271. if (count($productsData) || !empty($errors)) {
  272. $this->_fault('unable_to_move_all_products', implode(PHP_EOL, $errors));
  273. }
  274. try {
  275. $customerQuote
  276. ->collectTotals()
  277. ->save();
  278. $quote
  279. ->collectTotals()
  280. ->save();
  281. } catch (Exception $e) {
  282. $this->_fault("product_move_quote_save_fault", $e->getMessage());
  283. }
  284. return true;
  285. }
  286. }