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) 2012 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: /**
28: * Shopping cart api
29: *
30: * @category Mage
31: * @package Mage_Checkout
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34:
35: class Mage_Checkout_Model_Cart_Coupon_Api extends Mage_Checkout_Model_Api_Resource
36: {
37: /**
38: * @param $quoteId
39: * @param $couponCode
40: * @param $storeId
41: * @return bool
42: */
43: public function add($quoteId, $couponCode, $store = null)
44: {
45: return $this->_applyCoupon($quoteId, $couponCode, $store = null);
46: }
47:
48: /**
49: * @param $quoteId
50: * @param $storeId
51: * @return void
52: */
53: public function remove($quoteId, $store = null)
54: {
55: $couponCode = '';
56: return $this->_applyCoupon($quoteId, $couponCode, $store);
57: }
58:
59: /**
60: * @param $quoteId
61: * @param $storeId
62: * @return string
63: */
64: public function get($quoteId, $store = null)
65: {
66: $quote = $this->_getQuote($quoteId, $store);
67:
68: return $quote->getCouponCode();
69: }
70:
71: /**
72: * @param $quoteId
73: * @param $couponCode
74: * @param $store
75: * @return bool
76: */
77: protected function _applyCoupon($quoteId, $couponCode, $store = null)
78: {
79: $quote = $this->_getQuote($quoteId, $store);
80:
81: if (!$quote->getItemsCount()) {
82: $this->_fault('quote_is_empty');
83: }
84:
85: $oldCouponCode = $quote->getCouponCode();
86: if (!strlen($couponCode) && !strlen($oldCouponCode)) {
87: return false;
88: }
89:
90: try {
91: $quote->getShippingAddress()->setCollectShippingRates(true);
92: $quote->setCouponCode(strlen($couponCode) ? $couponCode : '')
93: ->collectTotals()
94: ->save();
95: } catch (Exception $e) {
96: $this->_fault("cannot_apply_coupon_code", $e->getMessage());
97: }
98:
99: if ($couponCode) {
100: if (!$couponCode == $quote->getCouponCode()) {
101: $this->_fault('coupon_code_is_not_valid');
102: }
103: }
104:
105: return true;
106: }
107:
108:
109: }
110: