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_Shipping_Api extends Mage_Checkout_Model_Api_Resource
36: {
37: public function __construct()
38: {
39: $this->_ignoredAttributeCodes['quote_shipping_rate'] = array('address_id', 'created_at', 'updated_at', 'rate_id', 'carrier_sort_order');
40: }
41:
42: 43: 44: 45: 46: 47: 48: 49:
50: public function setShippingMethod($quoteId, $shippingMethod, $store = null)
51: {
52: $quote = $this->_getQuote($quoteId, $store);
53:
54: $quoteShippingAddress = $quote->getShippingAddress();
55: if(is_null($quoteShippingAddress->getId()) ) {
56: $this->_fault("shipping_address_is_not_set");
57: }
58:
59: $rate = $quote->getShippingAddress()->collectShippingRates()->getShippingRateByCode($shippingMethod);
60: if (!$rate) {
61: $this->_fault('shipping_method_is_not_available');
62: }
63:
64: try {
65: $quote->getShippingAddress()->setShippingMethod($shippingMethod);
66: $quote->collectTotals()->save();
67: } catch(Mage_Core_Exception $e) {
68: $this->_fault('shipping_method_is_not_set', $e->getMessage());
69: }
70:
71: return true;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function getShippingMethodsList($quoteId, $store=null)
82: {
83: $quote = $this->_getQuote($quoteId, $store);
84:
85: $quoteShippingAddress = $quote->getShippingAddress();
86: if (is_null($quoteShippingAddress->getId())) {
87: $this->_fault("shipping_address_is_not_set");
88: }
89:
90: try {
91: $quoteShippingAddress->collectShippingRates()->save();
92: $groupedRates = $quoteShippingAddress->getGroupedAllShippingRates();
93:
94: $ratesResult = array();
95: foreach ($groupedRates as $carrierCode => $rates ) {
96: $carrierName = $carrierCode;
97: if (!is_null(Mage::getStoreConfig('carriers/'.$carrierCode.'/title'))) {
98: $carrierName = Mage::getStoreConfig('carriers/'.$carrierCode.'/title');
99: }
100:
101: foreach ($rates as $rate) {
102: $rateItem = $this->_getAttributes($rate, "quote_shipping_rate");
103: $rateItem['carrierName'] = $carrierName;
104: $ratesResult[] = $rateItem;
105: unset($rateItem);
106: }
107: }
108: } catch (Mage_Core_Exception $e) {
109: $this->_fault('shipping_methods_list_could_not_be_retrived', $e->getMessage());
110: }
111:
112: return $ratesResult;
113: }
114:
115:
116: }
117: