PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 329 lines | 218 code | 40 blank | 71 comment | 42 complexity | f57a302df22b6d5e5b92d385095b4a82 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.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. /**
  36. * Base preparation of product data
  37. *
  38. * @param mixed $data
  39. * @return null|array
  40. */
  41. protected function _prepareProductsData($data)
  42. {
  43. return is_array($data) ? $data : null;
  44. }
  45. /**
  46. * @param $quoteId
  47. * @param $productsData
  48. * @param $store
  49. * @return bool
  50. */
  51. public function add($quoteId, $productsData, $store = null)
  52. {
  53. $quote = $this->_getQuote($quoteId, $store);
  54. if (empty($store)) {
  55. $store = $quote->getStoreId();
  56. }
  57. $productsData = $this->_prepareProductsData($productsData);
  58. if (empty($productsData)) {
  59. $this->_fault('invalid_product_data');
  60. }
  61. $errors = array();
  62. foreach ($productsData as $productItem) {
  63. if (isset($productItem['product_id'])) {
  64. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  65. } else if (isset($productItem['sku'])) {
  66. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  67. } else {
  68. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  69. continue;
  70. }
  71. $productRequest = $this->_getProductRequest($productItem);
  72. try {
  73. $result = $quote->addProduct($productByItem, $productRequest);
  74. if (is_string($result)) {
  75. Mage::throwException($result);
  76. }
  77. } catch (Mage_Core_Exception $e) {
  78. $errors[] = $e->getMessage();
  79. }
  80. }
  81. if (!empty($errors)) {
  82. $this->_fault("add_product_fault", implode(PHP_EOL, $errors));
  83. }
  84. try {
  85. $quote->collectTotals()->save();
  86. } catch (Exception $e) {
  87. $this->_fault("add_product_quote_save_fault", $e->getMessage());
  88. }
  89. return true;
  90. }
  91. /**
  92. * @param $quoteId
  93. * @param $productsData
  94. * @param $store
  95. * @return bool
  96. */
  97. public function update($quoteId, $productsData, $store = null)
  98. {
  99. $quote = $this->_getQuote($quoteId, $store);
  100. if (empty($store)) {
  101. $store = $quote->getStoreId();
  102. }
  103. $productsData = $this->_prepareProductsData($productsData);
  104. if (empty($productsData)) {
  105. $this->_fault('invalid_product_data');
  106. }
  107. $errors = array();
  108. foreach ($productsData as $productItem) {
  109. if (isset($productItem['product_id'])) {
  110. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  111. } else if (isset($productItem['sku'])) {
  112. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  113. } else {
  114. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  115. continue;
  116. }
  117. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  118. $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem,
  119. $this->_getProductRequest($productItem));
  120. if (is_null($quoteItem->getId())) {
  121. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  122. continue;
  123. }
  124. if ($productItem['qty'] > 0) {
  125. $quoteItem->setQty($productItem['qty']);
  126. }
  127. }
  128. if (!empty($errors)) {
  129. $this->_fault("update_product_fault", implode(PHP_EOL, $errors));
  130. }
  131. try {
  132. $quote->collectTotals()->save();
  133. } catch (Exception $e) {
  134. $this->_fault("update_product_quote_save_fault", $e->getMessage());
  135. }
  136. return true;
  137. }
  138. /**
  139. * @param $quoteId
  140. * @param $productsData
  141. * @param $store
  142. * @return bool
  143. */
  144. public function remove($quoteId, $productsData, $store = null)
  145. {
  146. $quote = $this->_getQuote($quoteId, $store);
  147. if (empty($store)) {
  148. $store = $quote->getStoreId();
  149. }
  150. $productsData = $this->_prepareProductsData($productsData);
  151. if (empty($productsData)) {
  152. $this->_fault('invalid_product_data');
  153. }
  154. $errors = array();
  155. foreach ($productsData as $productItem) {
  156. if (isset($productItem['product_id'])) {
  157. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  158. } else if (isset($productItem['sku'])) {
  159. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  160. } else {
  161. $errors[] = Mage::helper('checkout')->__("One item of products do not have identifier or sku");
  162. continue;
  163. }
  164. try {
  165. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  166. $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem,
  167. $this->_getProductRequest($productItem));
  168. if (is_null($quoteItem->getId())) {
  169. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  170. continue;
  171. }
  172. $quote->removeItem($quoteItem->getId());
  173. } catch (Mage_Core_Exception $e) {
  174. $errors[] = $e->getMessage();
  175. }
  176. }
  177. if (!empty($errors)) {
  178. $this->_fault("remove_product_fault", implode(PHP_EOL, $errors));
  179. }
  180. try {
  181. $quote->collectTotals()->save();
  182. } catch (Exception $e) {
  183. $this->_fault("remove_product_quote_save_fault", $e->getMessage());
  184. }
  185. return true;
  186. }
  187. /**
  188. * @param $quoteId
  189. * @param $store
  190. * @return array
  191. */
  192. public function items($quoteId, $store = null)
  193. {
  194. $quote = $this->_getQuote($quoteId, $store);
  195. if (empty($store)) {
  196. $store = $quote->getStoreId();
  197. }
  198. if (!$quote->getItemsCount()) {
  199. return array();
  200. }
  201. $productsResult = array();
  202. foreach ($quote->getAllItems() as $item) {
  203. /** @var $item Mage_Sales_Model_Quote_Item */
  204. $product = $item->getProduct();
  205. $productsResult[] = array( // Basic product data
  206. 'product_id' => $product->getId(),
  207. 'sku' => $product->getSku(),
  208. 'name' => $product->getName(),
  209. 'set' => $product->getAttributeSetId(),
  210. 'type' => $product->getTypeId(),
  211. 'category_ids' => $product->getCategoryIds(),
  212. 'website_ids' => $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,
  260. $this->_getProductRequest($productItem));
  261. if ($quoteItem && $quoteItem->getId()) {
  262. $newQuoteItem = clone $quoteItem;
  263. $newQuoteItem->setId(null);
  264. $customerQuote->addItem($newQuoteItem);
  265. $quote->removeItem($quoteItem->getId());
  266. unset($productsData[$key]);
  267. } else {
  268. $errors[] = Mage::helper('checkout')->__("One item of products is not belong any of quote item");
  269. }
  270. } catch (Mage_Core_Exception $e) {
  271. $errors[] = $e->getMessage();
  272. }
  273. }
  274. if (count($productsData) || !empty($errors)) {
  275. $this->_fault('unable_to_move_all_products', implode(PHP_EOL, $errors));
  276. }
  277. try {
  278. $customerQuote
  279. ->collectTotals()
  280. ->save();
  281. $quote
  282. ->collectTotals()
  283. ->save();
  284. } catch (Exception $e) {
  285. $this->_fault("product_move_quote_save_fault", $e->getMessage());
  286. }
  287. return true;
  288. }
  289. }