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: class Mage_Checkout_Block_Cart_Shipping extends Mage_Checkout_Block_Cart_Abstract
29: {
30: /**
31: * Available Carriers Instances
32: * @var null|array
33: */
34: protected $_carriers = null;
35:
36: /**
37: * Estimate Rates
38: * @var array
39: */
40: protected $_rates = array();
41:
42: /**
43: * Address Model
44: *
45: * @var array
46: */
47: protected $_address = array();
48:
49: /**
50: * Get Estimate Rates
51: *
52: * @return array
53: */
54: public function getEstimateRates()
55: {
56: if (empty($this->_rates)) {
57: $groups = $this->getAddress()->getGroupedAllShippingRates();
58: $this->_rates = $groups;
59: }
60: return $this->_rates;
61: }
62:
63: /**
64: * Get Address Model
65: *
66: * @return Mage_Sales_Model_Quote_Address
67: */
68: public function getAddress()
69: {
70: if (empty($this->_address)) {
71: $this->_address = $this->getQuote()->getShippingAddress();
72: }
73: return $this->_address;
74: }
75:
76: /**
77: * Get Carrier Name
78: *
79: * @param string $carrierCode
80: * @return mixed
81: */
82: public function getCarrierName($carrierCode)
83: {
84: if ($name = Mage::getStoreConfig('carriers/'.$carrierCode.'/title')) {
85: return $name;
86: }
87: return $carrierCode;
88: }
89:
90: /**
91: * Get Shipping Method
92: *
93: * @return string
94: */
95: public function getAddressShippingMethod()
96: {
97: return $this->getAddress()->getShippingMethod();
98: }
99:
100: /**
101: * Get Estimate Country Id
102: *
103: * @return string
104: */
105: public function getEstimateCountryId()
106: {
107: return $this->getAddress()->getCountryId();
108: }
109:
110: /**
111: * Get Estimate Postcode
112: *
113: * @return string
114: */
115: public function getEstimatePostcode()
116: {
117: return $this->getAddress()->getPostcode();
118: }
119:
120: /**
121: * Get Estimate City
122: *
123: * @return string
124: */
125: public function getEstimateCity()
126: {
127: return $this->getAddress()->getCity();
128: }
129:
130: /**
131: * Get Estimate Region Id
132: *
133: * @return mixed
134: */
135: public function getEstimateRegionId()
136: {
137: return $this->getAddress()->getRegionId();
138: }
139:
140: /**
141: * Get Estimate Region
142: *
143: * @return string
144: */
145: public function getEstimateRegion()
146: {
147: return $this->getAddress()->getRegion();
148: }
149:
150: /**
151: * Show City in Shipping Estimation
152: *
153: * @return bool
154: */
155: public function getCityActive()
156: {
157: return (bool)Mage::getStoreConfig('carriers/dhl/active')
158: || (bool)Mage::getStoreConfig('carriers/dhlint/active');
159: }
160:
161: /**
162: * Show State in Shipping Estimation
163: *
164: * @return bool
165: */
166: public function getStateActive()
167: {
168: return (bool)Mage::getStoreConfig('carriers/dhl/active')
169: || (bool)Mage::getStoreConfig('carriers/tablerate/active')
170: || (bool)Mage::getStoreConfig('carriers/dhlint/active');
171: }
172:
173: /**
174: * Convert price from default currency to current currency
175: *
176: * @param float $price
177: * @return float
178: */
179: public function formatPrice($price)
180: {
181: return $this->getQuote()->getStore()->convertPrice($price, true);
182: }
183:
184: /**
185: * Get Shipping Price
186: *
187: * @param float $price
188: * @param bool $flag
189: * @return float
190: */
191: public function getShippingPrice($price, $flag)
192: {
193: return $this->formatPrice($this->helper('tax')->getShippingPrice(
194: $price,
195: $flag,
196: $this->getAddress(),
197: $this->getQuote()->getCustomerTaxClassId()
198: ));
199: }
200:
201: /**
202: * Obtain available carriers instances
203: *
204: * @return array
205: */
206: public function getCarriers()
207: {
208: if (null === $this->_carriers) {
209: $this->_carriers = array();
210: $this->getEstimateRates();
211: foreach ($this->_rates as $rateGroup) {
212: if (!empty($rateGroup)) {
213: foreach ($rateGroup as $rate) {
214: $this->_carriers[] = $rate->getCarrierInstance();
215: }
216: }
217: }
218: }
219: return $this->_carriers;
220: }
221:
222: /**
223: * Check if one of carriers require state/province
224: *
225: * @return bool
226: */
227: public function isStateProvinceRequired()
228: {
229: foreach ($this->getCarriers() as $carrier) {
230: if ($carrier->isStateProvinceRequired()) {
231: return true;
232: }
233: }
234: return false;
235: }
236:
237: /**
238: * Check if one of carriers require city
239: *
240: * @return bool
241: */
242: public function isCityRequired()
243: {
244: foreach ($this->getCarriers() as $carrier) {
245: if ($carrier->isCityRequired()) {
246: return true;
247: }
248: }
249: return false;
250: }
251:
252: /**
253: * Check if one of carriers require zip code
254: *
255: * @return bool
256: */
257: public function isZipCodeRequired()
258: {
259: foreach ($this->getCarriers() as $carrier) {
260: if ($carrier->isZipCodeRequired($this->getEstimateCountryId())) {
261: return true;
262: }
263: }
264: return false;
265: }
266: }
267: