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

https://github.com/seloshow/Magento-Pruebas · PHP · 299 lines · 193 code · 42 blank · 64 comment · 40 complexity · 7e3afac7cb8dc1a6e46d3cb58a755846 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@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. continue;
  72. }
  73. $productRequest = $this->_getProductRequest($productItem);
  74. try {
  75. $result = $quote->addProduct($productByItem, $productRequest);
  76. if (is_string($result)) {
  77. Mage::throwException($result);
  78. }
  79. } catch( Exception $e) {
  80. $errors[] = $e->getMessage();
  81. }
  82. }
  83. if (!empty($errors)) {
  84. $this->_fault("add_product_fault", implode(PHP_EOL, $errors));
  85. }
  86. try {
  87. $quote->collectTotals()->save();
  88. } catch(Exception $e) {
  89. $this->_fault("add_product_fault", $e->getMessage());
  90. }
  91. return true;
  92. }
  93. /**
  94. * @param $quoteId
  95. * @param $productsData
  96. * @param $store
  97. * @return bool
  98. */
  99. public function update($quoteId, $productsData, $store=null)
  100. {
  101. $quote = $this->_getQuote($quoteId, $store);
  102. if (empty($store)) {
  103. $store = $quote->getStoreId();
  104. }
  105. $productsData = $this->_prepareProductsData($productsData);
  106. if (empty($productsData)) {
  107. $this->_fault('invalid_product_data');
  108. }
  109. foreach ($productsData as $productItem) {
  110. if (isset($productItem['product_id'])) {
  111. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  112. } else if (isset($productItem['sku'])) {
  113. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  114. } else {
  115. continue;
  116. }
  117. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  118. $quoteItem = $quote->getItemByProduct($productByItem);
  119. if ($quoteItem === false) {
  120. continue;
  121. }
  122. if ($productItem['qty'] > 0) {
  123. $quoteItem->setQty($productItem['qty']);
  124. }
  125. }
  126. try {
  127. $quote->save();
  128. } catch(Exception $e) {
  129. $this->_fault("update_product_fault", $e->getMessage());
  130. }
  131. return true;
  132. }
  133. /**
  134. * @param $quoteId
  135. * @param $productsData
  136. * @param $store
  137. * @return bool
  138. */
  139. public function remove($quoteId, $productsData, $store=null)
  140. {
  141. $quote = $this->_getQuote($quoteId, $store);
  142. if (empty($store)) {
  143. $store = $quote->getStoreId();
  144. }
  145. $productsData = $this->_prepareProductsData($productsData);
  146. if (empty($productsData)) {
  147. $this->_fault('invalid_product_data');
  148. }
  149. foreach ($productsData as $productItem) {
  150. if (isset($productItem['product_id'])) {
  151. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  152. } else if (isset($productItem['sku'])) {
  153. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  154. } else {
  155. continue;
  156. }
  157. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  158. $quoteItem = $quote->getItemByProduct($productByItem);
  159. if ($quoteItem === false) {
  160. continue;
  161. }
  162. $quote->removeItem($quoteItem->getId());
  163. }
  164. try {
  165. $quote->save();
  166. } catch(Exception $e) {
  167. $this->_fault("remove_product_fault", $e->getMessage());
  168. }
  169. return true;
  170. }
  171. /**
  172. * @param $quoteId
  173. * @param $store
  174. * @return array
  175. */
  176. public function items($quoteId, $store = null)
  177. {
  178. $quote = $this->_getQuote($quoteId, $store);
  179. if (empty($store)) {
  180. $store = $quote->getStoreId();
  181. }
  182. if (!$quote->getItemsCount()) {
  183. return array();
  184. }
  185. $productsResult = array();
  186. foreach ($quote->getAllItems() as $item) {
  187. /** @var $item Mage_Sales_Model_Quote_Item */
  188. $product = $item->getProduct();
  189. $productsResult[] = array( // Basic product data
  190. 'product_id' => $product->getId(),
  191. 'sku' => $product->getSku(),
  192. 'set' => $product->getAttributeSetId(),
  193. 'type' => $product->getTypeId(),
  194. 'categories' => $product->getCategoryIds(),
  195. 'websites' => $product->getWebsiteIds()
  196. );
  197. }
  198. return $productsResult;
  199. }
  200. /**
  201. * @param $quoteId
  202. * @param $productsData
  203. * @param $store
  204. * @return bool
  205. */
  206. public function moveToCustomerQuote($quoteId, $productsData, $store=null)
  207. {
  208. $quote = $this->_getQuote($quoteId, $store);
  209. if (empty($store)) {
  210. $store = $quote->getStoreId();
  211. }
  212. $customer = $quote->getCustomer();
  213. if (is_null($customer->getId())) {
  214. $this->_fault('customer_not_set_for_quote');
  215. }
  216. /** @var $customerQuote Mage_Sales_Model_Quote */
  217. $customerQuote = Mage::getModel('sales/quote')
  218. ->setStoreId($store)
  219. ->loadByCustomer($customer);
  220. if (is_null($customerQuote->getId())) {
  221. $this->_fault('customer_quote_not_exist');
  222. }
  223. if ($customerQuote->getId() == $quote->getId()) {
  224. $this->_fault('quotes_are_similar');
  225. }
  226. $productsData = $this->_prepareProductsData($productsData);
  227. foreach($productsData as $key => $productItem){
  228. if (isset($productItem['product_id'])) {
  229. $productByItem = $this->_getProduct($productItem['product_id'], $store, "id");
  230. } else if (isset($productItem['sku'])) {
  231. $productByItem = $this->_getProduct($productItem['sku'], $store, "sku");
  232. } else {
  233. continue;
  234. }
  235. $quoteItem = $quote->getItemByProduct($productByItem);
  236. if($quoteItem->getId()){
  237. $customerQuote->addItem($quoteItem);
  238. $quote->removeItem($quoteItem->getId());
  239. unset($productsData[$key]);
  240. }
  241. }
  242. if (count($productsData)) {
  243. $this->_fault('unable_to_move_all_products');
  244. }
  245. try {
  246. $customerQuote
  247. ->collectTotals()
  248. ->save();
  249. $quote
  250. ->collectTotals()
  251. ->save();
  252. } catch (Exception $e) {
  253. $this->_fault($e->getMessage(), $e->getMessage());
  254. }
  255. return true;
  256. }
  257. }