Mage_nopobox - Version 1.0.0

Version Notes

Magento No PO Box Shipping Extension 1.0.0 Release!

Download this release

Release Info

Developer Matt
Extension Mage_nopobox
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/local/Matt/Nopobox/Model/Quote/Address.php ADDED
@@ -0,0 +1,1249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_Sales
23
+ * @copyright Copyright (c) 2011 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
+ /**
29
+ * Sales Quote address model
30
+ *
31
+ * @method Mage_Sales_Model_Resource_Quote_Address _getResource()
32
+ * @method Mage_Sales_Model_Resource_Quote_Address getResource()
33
+ * @method int getQuoteId()
34
+ * @method Mage_Sales_Model_Quote_Address setQuoteId(int $value)
35
+ * @method string getCreatedAt()
36
+ * @method Mage_Sales_Model_Quote_Address setCreatedAt(string $value)
37
+ * @method string getUpdatedAt()
38
+ * @method Mage_Sales_Model_Quote_Address setUpdatedAt(string $value)
39
+ * @method int getCustomerId()
40
+ * @method Mage_Sales_Model_Quote_Address setCustomerId(int $value)
41
+ * @method int getSaveInAddressBook()
42
+ * @method Mage_Sales_Model_Quote_Address setSaveInAddressBook(int $value)
43
+ * @method int getCustomerAddressId()
44
+ * @method Mage_Sales_Model_Quote_Address setCustomerAddressId(int $value)
45
+ * @method string getAddressType()
46
+ * @method Mage_Sales_Model_Quote_Address setAddressType(string $value)
47
+ * @method string getEmail()
48
+ * @method Mage_Sales_Model_Quote_Address setEmail(string $value)
49
+ * @method string getPrefix()
50
+ * @method Mage_Sales_Model_Quote_Address setPrefix(string $value)
51
+ * @method string getFirstname()
52
+ * @method Mage_Sales_Model_Quote_Address setFirstname(string $value)
53
+ * @method string getMiddlename()
54
+ * @method Mage_Sales_Model_Quote_Address setMiddlename(string $value)
55
+ * @method string getLastname()
56
+ * @method Mage_Sales_Model_Quote_Address setLastname(string $value)
57
+ * @method string getSuffix()
58
+ * @method Mage_Sales_Model_Quote_Address setSuffix(string $value)
59
+ * @method string getCompany()
60
+ * @method Mage_Sales_Model_Quote_Address setCompany(string $value)
61
+ * @method string getCity()
62
+ * @method Mage_Sales_Model_Quote_Address setCity(string $value)
63
+ * @method Mage_Sales_Model_Quote_Address setRegion(string $value)
64
+ * @method Mage_Sales_Model_Quote_Address setRegionId(int $value)
65
+ * @method string getPostcode()
66
+ * @method Mage_Sales_Model_Quote_Address setPostcode(string $value)
67
+ * @method string getCountryId()
68
+ * @method Mage_Sales_Model_Quote_Address setCountryId(string $value)
69
+ * @method string getTelephone()
70
+ * @method Mage_Sales_Model_Quote_Address setTelephone(string $value)
71
+ * @method string getFax()
72
+ * @method Mage_Sales_Model_Quote_Address setFax(string $value)
73
+ * @method int getSameAsBilling()
74
+ * @method Mage_Sales_Model_Quote_Address setSameAsBilling(int $value)
75
+ * @method int getFreeShipping()
76
+ * @method Mage_Sales_Model_Quote_Address setFreeShipping(int $value)
77
+ * @method int getCollectShippingRates()
78
+ * @method Mage_Sales_Model_Quote_Address setCollectShippingRates(int $value)
79
+ * @method string getShippingMethod()
80
+ * @method Mage_Sales_Model_Quote_Address setShippingMethod(string $value)
81
+ * @method string getShippingDescription()
82
+ * @method Mage_Sales_Model_Quote_Address setShippingDescription(string $value)
83
+ * @method float getWeight()
84
+ * @method Mage_Sales_Model_Quote_Address setWeight(float $value)
85
+ * @method float getSubtotal()
86
+ * @method Mage_Sales_Model_Quote_Address setSubtotal(float $value)
87
+ * @method float getBaseSubtotal()
88
+ * @method Mage_Sales_Model_Quote_Address setBaseSubtotal(float $value)
89
+ * @method Mage_Sales_Model_Quote_Address setSubtotalWithDiscount(float $value)
90
+ * @method Mage_Sales_Model_Quote_Address setBaseSubtotalWithDiscount(float $value)
91
+ * @method float getTaxAmount()
92
+ * @method Mage_Sales_Model_Quote_Address setTaxAmount(float $value)
93
+ * @method float getBaseTaxAmount()
94
+ * @method Mage_Sales_Model_Quote_Address setBaseTaxAmount(float $value)
95
+ * @method float getShippingAmount()
96
+ * @method float getBaseShippingAmount()
97
+ * @method float getShippingTaxAmount()
98
+ * @method Mage_Sales_Model_Quote_Address setShippingTaxAmount(float $value)
99
+ * @method float getBaseShippingTaxAmount()
100
+ * @method Mage_Sales_Model_Quote_Address setBaseShippingTaxAmount(float $value)
101
+ * @method float getDiscountAmount()
102
+ * @method Mage_Sales_Model_Quote_Address setDiscountAmount(float $value)
103
+ * @method float getBaseDiscountAmount()
104
+ * @method Mage_Sales_Model_Quote_Address setBaseDiscountAmount(float $value)
105
+ * @method float getGrandTotal()
106
+ * @method Mage_Sales_Model_Quote_Address setGrandTotal(float $value)
107
+ * @method float getBaseGrandTotal()
108
+ * @method Mage_Sales_Model_Quote_Address setBaseGrandTotal(float $value)
109
+ * @method string getCustomerNotes()
110
+ * @method Mage_Sales_Model_Quote_Address setCustomerNotes(string $value)
111
+ * @method string getDiscountDescription()
112
+ * @method Mage_Sales_Model_Quote_Address setDiscountDescription(string $value)
113
+ * @method float getShippingDiscountAmount()
114
+ * @method Mage_Sales_Model_Quote_Address setShippingDiscountAmount(float $value)
115
+ * @method float getBaseShippingDiscountAmount()
116
+ * @method Mage_Sales_Model_Quote_Address setBaseShippingDiscountAmount(float $value)
117
+ * @method float getSubtotalInclTax()
118
+ * @method Mage_Sales_Model_Quote_Address setSubtotalInclTax(float $value)
119
+ * @method float getBaseSubtotalTotalInclTax()
120
+ * @method Mage_Sales_Model_Quote_Address setBaseSubtotalTotalInclTax(float $value)
121
+ * @method int getGiftMessageId()
122
+ * @method Mage_Sales_Model_Quote_Address setGiftMessageId(int $value)
123
+ * @method float getHiddenTaxAmount()
124
+ * @method Mage_Sales_Model_Quote_Address setHiddenTaxAmount(float $value)
125
+ * @method float getBaseHiddenTaxAmount()
126
+ * @method Mage_Sales_Model_Quote_Address setBaseHiddenTaxAmount(float $value)
127
+ * @method float getShippingHiddenTaxAmount()
128
+ * @method Mage_Sales_Model_Quote_Address setShippingHiddenTaxAmount(float $value)
129
+ * @method float getBaseShippingHiddenTaxAmount()
130
+ * @method Mage_Sales_Model_Quote_Address setBaseShippingHiddenTaxAmount(float $value)
131
+ * @method float getShippingInclTax()
132
+ * @method Mage_Sales_Model_Quote_Address setShippingInclTax(float $value)
133
+ * @method float getBaseShippingInclTax()
134
+ * @method Mage_Sales_Model_Quote_Address setBaseShippingInclTax(float $value)
135
+ *
136
+ * @category Mage
137
+ * @package Mage_Sales
138
+ * @author Magento Core Team <core@magentocommerce.com>
139
+ */
140
+ class Matt_Nopobox_Model_Quote_Address extends Mage_Customer_Model_Address_Abstract
141
+ {
142
+ const TYPE_BILLING = 'billing';
143
+ const TYPE_SHIPPING = 'shipping';
144
+ const RATES_FETCH = 1;
145
+ const RATES_RECALCULATE = 2;
146
+
147
+ protected $_eventPrefix = 'sales_quote_address';
148
+ protected $_eventObject = 'quote_address';
149
+
150
+ /**
151
+ * Quote object
152
+ *
153
+ * @var Mage_Sales_Model_Quote
154
+ */
155
+ protected $_items = null;
156
+
157
+ /**
158
+ * Quote object
159
+ *
160
+ * @var Mage_Sales_Model_Quote
161
+ */
162
+ protected $_quote = null;
163
+
164
+ /**
165
+ * Sales Quote address rates
166
+ *
167
+ * @var Mage_Sales_Model_Quote_Address_Rate
168
+ */
169
+ protected $_rates = null;
170
+
171
+ /**
172
+ * Total models collector
173
+ *
174
+ * @var Mage_Sales_Model_Quote_Address_Totla_Collector
175
+ */
176
+ protected $_totalCollector = null;
177
+
178
+ /**
179
+ * Total data as array
180
+ *
181
+ * @var array
182
+ */
183
+ protected $_totals = array();
184
+
185
+ protected $_totalAmounts = array();
186
+ protected $_baseTotalAmounts = array();
187
+
188
+ /**
189
+ * Whether to segregate by nominal items only
190
+ *
191
+ * @var bool
192
+ */
193
+ protected $_nominalOnly = null;
194
+
195
+ /**
196
+ * Initialize resource
197
+ */
198
+ protected function _construct()
199
+ {
200
+ $this->_init('sales/quote_address');
201
+ }
202
+
203
+ /**
204
+ * Init mapping array of short fields to its full names
205
+ *
206
+ * @return Mage_Sales_Model_Quote_Address
207
+ */
208
+ protected function _initOldFieldsMap()
209
+ {
210
+ $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('quote_address');
211
+ return $this;
212
+ }
213
+
214
+ /**
215
+ * Initialize quote identifier before save
216
+ *
217
+ * @return Mage_Sales_Model_Quote_Address
218
+ */
219
+ protected function _beforeSave()
220
+ {
221
+ parent::_beforeSave();
222
+ if ($this->getQuote()) {
223
+ $quoteId = $this->getQuote()->getId();
224
+ if ($quoteId) {
225
+ $this->setQuoteId($quoteId);
226
+ } else {
227
+ $this->_dataSaveAllowed = false;
228
+ }
229
+ $this->setCustomerId($this->getQuote()->getCustomerId());
230
+ /**
231
+ * Init customer address id if customer address is assigned
232
+ */
233
+ if ($this->getCustomerAddress()) {
234
+ $this->setCustomerAddressId($this->getCustomerAddress()->getId());
235
+ }
236
+ }
237
+ if ($this->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING
238
+ && $this->getSameAsBilling() === null
239
+ ) {
240
+ $this->setSameAsBilling(1);
241
+ }
242
+ return $this;
243
+ }
244
+
245
+ /**
246
+ * Save child collections
247
+ *
248
+ * @return Mage_Sales_Model_Quote_Address
249
+ */
250
+ protected function _afterSave()
251
+ {
252
+ parent::_afterSave();
253
+ if (null !== $this->_items) {
254
+ $this->getItemsCollection()->save();
255
+ }
256
+ if (null !== $this->_rates) {
257
+ $this->getShippingRatesCollection()->save();
258
+ }
259
+ return $this;
260
+ }
261
+
262
+ /**
263
+ * Declare adress quote model object
264
+ *
265
+ * @param Mage_Sales_Model_Quote $quote
266
+ * @return Mage_Sales_Model_Quote_Address
267
+ */
268
+ public function setQuote(Mage_Sales_Model_Quote $quote)
269
+ {
270
+ $this->_quote = $quote;
271
+ $this->setQuoteId($quote->getId());
272
+ return $this;
273
+ }
274
+
275
+ /**
276
+ * Retrieve quote object
277
+ *
278
+ * @return Mage_Sales_Model_Quote
279
+ */
280
+ public function getQuote()
281
+ {
282
+ return $this->_quote;
283
+ }
284
+
285
+ /**
286
+ * Import quote address data from customer address object
287
+ *
288
+ * @param Mage_Customer_Model_Address $address
289
+ * @return Mage_Sales_Model_Quote_Address
290
+ */
291
+ public function importCustomerAddress(Mage_Customer_Model_Address $address)
292
+ {
293
+ Mage::helper('core')->copyFieldset('customer_address', 'to_quote_address', $address, $this);
294
+ $email = null;
295
+ if ($address->hasEmail()) {
296
+ $email = $address->getEmail();
297
+ }
298
+ elseif ($address->getCustomer()) {
299
+ $email = $address->getCustomer()->getEmail();
300
+ }
301
+ if ($email) {
302
+ $this->setEmail($email);
303
+ }
304
+ return $this;
305
+ }
306
+
307
+ /**
308
+ * Export data to customer address object
309
+ *
310
+ * @return Mage_Customer_Model_Address
311
+ */
312
+ public function exportCustomerAddress()
313
+ {
314
+ $address = Mage::getModel('customer/address');
315
+ Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_customer_address', $this, $address);
316
+ return $address;
317
+ }
318
+
319
+ /**
320
+ * Import address data from order address
321
+ *
322
+ * @param Mage_Sales_Model_Order_Address $address
323
+ * @return Mage_Sales_Model_Quote_Address
324
+ */
325
+ public function importOrderAddress(Mage_Sales_Model_Order_Address $address)
326
+ {
327
+ $this->setAddressType($address->getAddressType())
328
+ ->setCustomerId($address->getCustomerId())
329
+ ->setCustomerAddressId($address->getCustomerAddressId())
330
+ ->setEmail($address->getEmail());
331
+
332
+ Mage::helper('core')->copyFieldset('sales_convert_order_address', 'to_quote_address', $address, $this);
333
+
334
+ return $this;
335
+ }
336
+
337
+ /**
338
+ * Convert object to array
339
+ *
340
+ * @param array $arrAttributes
341
+ * @return array
342
+ */
343
+ public function toArray(array $arrAttributes = array())
344
+ {
345
+ $arr = parent::toArray($arrAttributes);
346
+ $arr['rates'] = $this->getShippingRatesCollection()->toArray($arrAttributes);
347
+ $arr['items'] = $this->getItemsCollection()->toArray($arrAttributes);
348
+ foreach ($this->getTotals() as $k=>$total) {
349
+ $arr['totals'][$k] = $total->toArray();
350
+ }
351
+ return $arr;
352
+ }
353
+
354
+ /**
355
+ * Retrieve address items collection
356
+ *
357
+ * @return Mage_Eav_Model_Entity_Collection_Abstract
358
+ */
359
+ public function getItemsCollection()
360
+ {
361
+ if (is_null($this->_items)) {
362
+ $this->_items = Mage::getModel('sales/quote_address_item')->getCollection()
363
+ ->setAddressFilter($this->getId());
364
+
365
+ if ($this->getId()) {
366
+ foreach ($this->_items as $item) {
367
+ $item->setAddress($this);
368
+ }
369
+ }
370
+ }
371
+ return $this->_items;
372
+ }
373
+
374
+ /**
375
+ * Get all available address items
376
+ *
377
+ * @return array
378
+ */
379
+ public function getAllItems()
380
+ {
381
+ // We calculate item list once and cache it in three arrays - all items, nominal, non-nominal
382
+ $cachedItems = $this->_nominalOnly ? 'nominal' : ($this->_nominalOnly === false ? 'nonnominal' : 'all');
383
+ $key = 'cached_items_' . $cachedItems;
384
+ if (!$this->hasData($key)) {
385
+ // For compatibility we will use $this->_filterNominal to divide nominal items from non-nominal
386
+ // (because it can be overloaded)
387
+ // So keep current flag $this->_nominalOnly and restore it after cycle
388
+ $wasNominal = $this->_nominalOnly;
389
+ $this->_nominalOnly = true; // Now $this->_filterNominal() will return positive values for nominal items
390
+
391
+ $quoteItems = $this->getQuote()->getItemsCollection();
392
+ $addressItems = $this->getItemsCollection();
393
+
394
+ $items = array();
395
+ $nominalItems = array();
396
+ $nonNominalItems = array();
397
+ if ($this->getQuote()->getIsMultiShipping() && $addressItems->count() > 0) {
398
+ foreach ($addressItems as $aItem) {
399
+ if ($aItem->isDeleted()) {
400
+ continue;
401
+ }
402
+
403
+ if (!$aItem->getQuoteItemImported()) {
404
+ $qItem = $this->getQuote()->getItemById($aItem->getQuoteItemId());
405
+ if ($qItem) {
406
+ $aItem->importQuoteItem($qItem);
407
+ }
408
+ }
409
+ $items[] = $aItem;
410
+ if ($this->_filterNominal($aItem)) {
411
+ $nominalItems[] = $aItem;
412
+ } else {
413
+ $nonNominalItems[] = $aItem;
414
+ }
415
+ }
416
+ } else {
417
+ /*
418
+ * For virtual quote we assign items only to billing address, otherwise - only to shipping address
419
+ */
420
+ $addressType = $this->getAddressType();
421
+ $canAddItems = $this->getQuote()->isVirtual()
422
+ ? ($addressType == self::TYPE_BILLING)
423
+ : ($addressType == self::TYPE_SHIPPING);
424
+
425
+ if ($canAddItems) {
426
+ foreach ($quoteItems as $qItem) {
427
+ if ($qItem->isDeleted()) {
428
+ continue;
429
+ }
430
+ $items[] = $qItem;
431
+ if ($this->_filterNominal($qItem)) {
432
+ $nominalItems[] = $qItem;
433
+ } else {
434
+ $nonNominalItems[] = $qItem;
435
+ }
436
+ }
437
+ }
438
+ }
439
+
440
+ // Cache calculated lists
441
+ $this->setData('cached_items_all', $items);
442
+ $this->setData('cached_items_nominal', $nominalItems);
443
+ $this->setData('cached_items_nonnominal', $nonNominalItems);
444
+
445
+ $this->_nominalOnly = $wasNominal; // Restore original value before we changed it
446
+ }
447
+
448
+ $items = $this->getData($key);
449
+ return $items;
450
+ }
451
+
452
+ /**
453
+ * Getter for all non-nominal items
454
+ *
455
+ * @return array
456
+ */
457
+ public function getAllNonNominalItems()
458
+ {
459
+ $this->_nominalOnly = false;
460
+ $result = $this->getAllItems();
461
+ $this->_nominalOnly = null;
462
+ return $result;
463
+ }
464
+
465
+ /**
466
+ * Getter for all nominal items
467
+ *
468
+ * @return array
469
+ */
470
+ public function getAllNominalItems()
471
+ {
472
+ $this->_nominalOnly = true;
473
+ $result = $this->getAllItems();
474
+ $this->_nominalOnly = null;
475
+ return $result;
476
+ }
477
+
478
+ /**
479
+ * Segregate by nominal criteria
480
+ *
481
+ * true: get nominals only
482
+ * false: get non-nominals only
483
+ * null: get all
484
+ *
485
+ * @param Mage_Sales_Model_Quote_Item_Abstract
486
+ * @return Mage_Sales_Model_Quote_Item_Abstract|false
487
+ */
488
+ protected function _filterNominal($item)
489
+ {
490
+ return (null === $this->_nominalOnly)
491
+ || ((false === $this->_nominalOnly) && !$item->isNominal())
492
+ || ((true === $this->_nominalOnly) && $item->isNominal())
493
+ ? $item : false;
494
+ }
495
+
496
+ /**
497
+ * Retrieve all visible items
498
+ *
499
+ * @return array
500
+ */
501
+ public function getAllVisibleItems()
502
+ {
503
+ $items = array();
504
+ foreach ($this->getAllItems() as $item) {
505
+ if (!$item->getParentItemId()) {
506
+ $items[] = $item;
507
+ }
508
+ }
509
+ return $items;
510
+ }
511
+
512
+ /**
513
+ * Retrieve item quantity by id
514
+ *
515
+ * @param int $itemId
516
+ * @return float|int
517
+ */
518
+ public function getItemQty($itemId = 0)
519
+ {
520
+ if ($this->hasData('item_qty')) {
521
+ return $this->getData('item_qty');
522
+ }
523
+
524
+ $qty = 0;
525
+ if ($itemId == 0) {
526
+ foreach ($this->getAllItems() as $item) {
527
+ $qty += $item->getQty();
528
+ }
529
+ } else {
530
+ $item = $this->getItemById($itemId);
531
+ if ($item) {
532
+ $qty = $item->getQty();
533
+ }
534
+ }
535
+ return $qty;
536
+ }
537
+
538
+ /**
539
+ * Check Quote address has Items
540
+ *
541
+ * @return bool
542
+ */
543
+ public function hasItems()
544
+ {
545
+ return sizeof($this->getAllItems())>0;
546
+ }
547
+
548
+ /**
549
+ * Get address item object by id without
550
+ *
551
+ * @param int $itemId
552
+ * @return Mage_Sales_Model_Quote_Address_Item
553
+ */
554
+ public function getItemById($itemId)
555
+ {
556
+ foreach ($this->getItemsCollection() as $item) {
557
+ if ($item->getId()==$itemId) {
558
+ return $item;
559
+ }
560
+ }
561
+ return false;
562
+ }
563
+
564
+ /**
565
+ * Get prepared not deleted item
566
+ *
567
+ * @param $itemId
568
+ * @return Mage_Sales_Model_Quote_Address_Item
569
+ */
570
+ public function getValidItemById($itemId)
571
+ {
572
+ foreach ($this->getAllItems() as $item) {
573
+ if ($item->getId()==$itemId) {
574
+ return $item;
575
+ }
576
+ }
577
+ return false;
578
+ }
579
+
580
+ /**
581
+ * Retrieve item object by quote item Id
582
+ *
583
+ * @param int $itemId
584
+ * @return Mage_Sales_Model_Quote_Address_Item
585
+ */
586
+ public function getItemByQuoteItemId($itemId)
587
+ {
588
+ foreach ($this->getItemsCollection() as $item) {
589
+ if ($item->getQuoteItemId()==$itemId) {
590
+ return $item;
591
+ }
592
+ }
593
+ return false;
594
+ }
595
+
596
+ /**
597
+ * Remove item from collection
598
+ *
599
+ * @param int $itemId
600
+ * @return Mage_Sales_Model_Quote_Address
601
+ */
602
+ public function removeItem($itemId)
603
+ {
604
+ $item = $this->getItemById($itemId);
605
+ if ($item) {
606
+ $item->isDeleted(true);
607
+ }
608
+ return $this;
609
+ }
610
+
611
+ /**
612
+ * Add item to address
613
+ *
614
+ * @param Mage_Sales_Model_Quote_Item_Abstract $item
615
+ * @param int $qty
616
+ * @return Mage_Sales_Model_Quote_Address
617
+ */
618
+ public function addItem(Mage_Sales_Model_Quote_Item_Abstract $item, $qty=null)
619
+ {
620
+ if ($item instanceof Mage_Sales_Model_Quote_Item) {
621
+ if ($item->getParentItemId()) {
622
+ return $this;
623
+ }
624
+ $addressItem = Mage::getModel('sales/quote_address_item')
625
+ ->setAddress($this)
626
+ ->importQuoteItem($item);
627
+ $this->getItemsCollection()->addItem($addressItem);
628
+
629
+ if ($item->getHasChildren()) {
630
+ foreach ($item->getChildren() as $child) {
631
+ $addressChildItem = Mage::getModel('sales/quote_address_item')
632
+ ->setAddress($this)
633
+ ->importQuoteItem($child)
634
+ ->setParentItem($addressItem);
635
+ $this->getItemsCollection()->addItem($addressChildItem);
636
+ }
637
+ }
638
+ }
639
+ else {
640
+ $addressItem = $item;
641
+ $addressItem->setAddress($this);
642
+ if (!$addressItem->getId()) {
643
+ $this->getItemsCollection()->addItem($addressItem);
644
+ }
645
+ }
646
+
647
+ if ($qty) {
648
+ $addressItem->setQty($qty);
649
+ }
650
+ return $this;
651
+ }
652
+
653
+ /**
654
+ * Retrieve collection of quote shipping rates
655
+ *
656
+ * @return Mage_Eav_Model_Entity_Collection_Abstract
657
+ */
658
+ public function getShippingRatesCollection()
659
+ {
660
+ if (is_null($this->_rates)) {
661
+ $this->_rates = Mage::getModel('sales/quote_address_rate')->getCollection()
662
+ ->setAddressFilter($this->getId());
663
+ if ($this->getQuote()->hasNominalItems(false)) {
664
+ $this->_rates->setFixedOnlyFilter(true);
665
+ }
666
+ if ($this->getId()) {
667
+ foreach ($this->_rates as $rate) {
668
+ $rate->setAddress($this);
669
+ }
670
+ }
671
+ }
672
+ return $this->_rates;
673
+ }
674
+
675
+ /**
676
+ * Retrieve all address shipping rates
677
+ *
678
+ * @return array
679
+ */
680
+ public function getAllShippingRates()
681
+ {
682
+ $rates = array();
683
+ foreach ($this->getShippingRatesCollection() as $rate) {
684
+ if (!$rate->isDeleted()) {
685
+ $rates[] = $rate;
686
+ }
687
+ }
688
+ return $rates;
689
+ }
690
+
691
+ /**
692
+ * Retrieve all grouped shipping rates
693
+ *
694
+ * @return array
695
+ */
696
+ public function getGroupedAllShippingRates()
697
+ {
698
+ $rates = array();
699
+ foreach ($this->getShippingRatesCollection() as $rate) {
700
+ if (!$rate->isDeleted() && $rate->getCarrierInstance()) {
701
+ if (!isset($rates[$rate->getCarrier()])) {
702
+ $rates[$rate->getCarrier()] = array();
703
+ }
704
+
705
+ $rates[$rate->getCarrier()][] = $rate;
706
+ $rates[$rate->getCarrier()][0]->carrier_sort_order = $rate->getCarrierInstance()->getSortOrder();
707
+ }
708
+ }
709
+ uasort($rates, array($this, '_sortRates'));
710
+ return $rates;
711
+ }
712
+
713
+ /**
714
+ * Sort rates recursive callback
715
+ *
716
+ * @param array $a
717
+ * @param array $b
718
+ * @return int
719
+ */
720
+ protected function _sortRates($a, $b)
721
+ {
722
+ if ((int)$a[0]->carrier_sort_order < (int)$b[0]->carrier_sort_order) {
723
+ return -1;
724
+ }
725
+ elseif ((int)$a[0]->carrier_sort_order > (int)$b[0]->carrier_sort_order) {
726
+ return 1;
727
+ }
728
+ else {
729
+ return 0;
730
+ }
731
+ }
732
+
733
+ /**
734
+ * Retrieve shipping rate by identifier
735
+ *
736
+ * @param int $rateId
737
+ * @return Mage_Sales_Model_Quote_Address_Rate | false
738
+ */
739
+ public function getShippingRateById($rateId)
740
+ {
741
+ foreach ($this->getShippingRatesCollection() as $rate) {
742
+ if ($rate->getId()==$rateId) {
743
+ return $rate;
744
+ }
745
+ }
746
+ return false;
747
+ }
748
+
749
+ /**
750
+ * Retrieve shipping rate by code
751
+ *
752
+ * @param string $code
753
+ * @return Mage_Sales_Model_Quote_Address_Rate
754
+ */
755
+ public function getShippingRateByCode($code)
756
+ {
757
+ foreach ($this->getShippingRatesCollection() as $rate) {
758
+ if ($rate->getCode()==$code) {
759
+ return $rate;
760
+ }
761
+ }
762
+ return false;
763
+ }
764
+
765
+ /**
766
+ * Mark all shipping rates as deleted
767
+ *
768
+ * @return Mage_Sales_Model_Quote_Address
769
+ */
770
+ public function removeAllShippingRates()
771
+ {
772
+ foreach ($this->getShippingRatesCollection() as $rate) {
773
+ $rate->isDeleted(true);
774
+ }
775
+ return $this;
776
+ }
777
+
778
+ /**
779
+ * Add shipping rate
780
+ *
781
+ * @param Mage_Sales_Model_Quote_Address_Rate $rate
782
+ * @return Mage_Sales_Model_Quote_Address
783
+ */
784
+ public function addShippingRate(Mage_Sales_Model_Quote_Address_Rate $rate)
785
+ {
786
+ $rate->setAddress($this);
787
+ $this->getShippingRatesCollection()->addItem($rate);
788
+ return $this;
789
+ }
790
+
791
+ /**
792
+ * Collecting shipping rates by address
793
+ *
794
+ * @return Mage_Sales_Model_Quote_Address
795
+ */
796
+ public function collectShippingRates()
797
+ {
798
+ if (!$this->getCollectShippingRates()) {
799
+ return $this;
800
+ }
801
+
802
+ $this->setCollectShippingRates(false);
803
+
804
+ $this->removeAllShippingRates();
805
+
806
+ if (!$this->getCountryId()) {
807
+ return $this;
808
+ }
809
+
810
+ $found = $this->requestShippingRates();
811
+ if (!$found) {
812
+ $this->setShippingAmount(0)
813
+ ->setBaseShippingAmount(0)
814
+ ->setShippingMethod('')
815
+ ->setShippingDescription('');
816
+ }
817
+
818
+ return $this;
819
+ }
820
+
821
+ /**
822
+ * Request shipping rates for entire address or specified address item
823
+ * Returns true if current selected shipping method code corresponds to one of the found rates
824
+ *
825
+ * @param Mage_Sales_Model_Quote_Item_Abstract $item
826
+ * @return bool
827
+ */
828
+ public function requestShippingRates(Mage_Sales_Model_Quote_Item_Abstract $item = null)
829
+ {
830
+ /** @var $request Mage_Shipping_Model_Rate_Request */
831
+ $request = Mage::getModel('shipping/rate_request');
832
+ $request->setAllItems($item ? array($item) : $this->getAllItems());
833
+ $request->setDestCountryId($this->getCountryId());
834
+ $request->setDestRegionId($this->getRegionId());
835
+ $request->setDestRegionCode($this->getRegionCode());
836
+ /**
837
+ * need to call getStreet with -1
838
+ * to get data in string instead of array
839
+ */
840
+ $request->setDestStreet($this->getStreet(-1));
841
+ $request->setDestCity($this->getCity());
842
+ $request->setDestPostcode($this->getPostcode());
843
+ $request->setPackageValue($item ? $item->getBaseRowTotal() : $this->getBaseSubtotal());
844
+ $packageValueWithDiscount = $item
845
+ ? $item->getBaseRowTotal() - $item->getBaseDiscountAmount()
846
+ : $this->getBaseSubtotalWithDiscount();
847
+ $request->setPackageValueWithDiscount($packageValueWithDiscount);
848
+ $request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());
849
+ $request->setPackageQty($item ? $item->getQty() : $this->getItemQty());
850
+
851
+ /**
852
+ * Need for shipping methods that use insurance based on price of physical products
853
+ */
854
+ $packagePhysicalValue = $item
855
+ ? $item->getBaseRowTotal()
856
+ : $this->getBaseSubtotal() - $this->getBaseVirtualAmount();
857
+ $request->setPackagePhysicalValue($packagePhysicalValue);
858
+
859
+ $request->setFreeMethodWeight($item ? 0 : $this->getFreeMethodWeight());
860
+
861
+ /**
862
+ * Store and website identifiers need specify from quote
863
+ */
864
+ /*$request->setStoreId(Mage::app()->getStore()->getId());
865
+ $request->setWebsiteId(Mage::app()->getStore()->getWebsiteId());*/
866
+
867
+ $request->setStoreId($this->getQuote()->getStore()->getId());
868
+ $request->setWebsiteId($this->getQuote()->getStore()->getWebsiteId());
869
+ $request->setFreeShipping($this->getFreeShipping());
870
+ /**
871
+ * Currencies need to convert in free shipping
872
+ */
873
+ $request->setBaseCurrency($this->getQuote()->getStore()->getBaseCurrency());
874
+ $request->setPackageCurrency($this->getQuote()->getStore()->getCurrentCurrency());
875
+ $request->setLimitCarrier($this->getLimitCarrier());
876
+
877
+ $result = Mage::getModel('shipping/shipping')->collectRates($request)->getResult();
878
+
879
+ $found = false;
880
+ if ($result) {
881
+ $shippingRates = $result->getAllRates();
882
+
883
+ foreach ($shippingRates as $shippingRate) {
884
+ $rate = Mage::getModel('sales/quote_address_rate')
885
+ ->importShippingRate($shippingRate);
886
+ if (!$item) {
887
+ $this->addShippingRate($rate);
888
+ }
889
+
890
+ if ($this->getShippingMethod() == $rate->getCode()) {
891
+ if ($item) {
892
+ $item->setBaseShippingAmount($rate->getPrice());
893
+ } else {
894
+ /**
895
+ * possible bug: this should be setBaseShippingAmount(),
896
+ * see Mage_Sales_Model_Quote_Address_Total_Shipping::collect()
897
+ * where this value is set again from the current specified rate price
898
+ * (looks like a workaround for this bug)
899
+ */
900
+ $this->setShippingAmount($rate->getPrice());
901
+ }
902
+
903
+ $found = true;
904
+ }
905
+ }
906
+ }
907
+ return $found;
908
+ }
909
+
910
+ /**
911
+ * Get totals collector model
912
+ *
913
+ * @return Mage_Sales_Model_Quote_Address_Total_Collector
914
+ */
915
+ public function getTotalCollector()
916
+ {
917
+ if ($this->_totalCollector === null) {
918
+ $this->_totalCollector = Mage::getSingleton(
919
+ 'sales/quote_address_total_collector',
920
+ array('store'=>$this->getQuote()->getStore())
921
+ );
922
+ }
923
+ return $this->_totalCollector;
924
+ }
925
+
926
+ /**
927
+ * Retrieve total models
928
+ *
929
+ * @deprecated
930
+ * @return array
931
+ */
932
+ public function getTotalModels()
933
+ {
934
+ return $this->getTotalCollector()->getRetrievers();
935
+ }
936
+
937
+ /**
938
+ * Collect address totals
939
+ *
940
+ * @return Mage_Sales_Model_Quote_Address
941
+ */
942
+ public function collectTotals()
943
+ {
944
+ foreach ($this->getTotalCollector()->getCollectors() as $model) {
945
+ $model->collect($this);
946
+ }
947
+ return $this;
948
+ }
949
+
950
+ /**
951
+ * Get address totals as array
952
+ *
953
+ * @return array
954
+ */
955
+ public function getTotals()
956
+ {
957
+ foreach ($this->getTotalCollector()->getRetrievers() as $model) {
958
+ $model->fetch($this);
959
+ }
960
+ return $this->_totals;
961
+ }
962
+
963
+ /**
964
+ * Add total data or model
965
+ *
966
+ * @param Mage_Sales_Model_Quote_Total|array $total
967
+ * @return Mage_Sales_Model_Quote_Address
968
+ */
969
+ public function addTotal($total)
970
+ {
971
+ if (is_array($total)) {
972
+ $totalInstance = Mage::getModel('sales/quote_address_total')
973
+ ->setData($total);
974
+ } elseif ($total instanceof Mage_Sales_Model_Quote_Total) {
975
+ $totalInstance = $total;
976
+ }
977
+ $totalInstance->setAddress($this);
978
+ $this->_totals[$totalInstance->getCode()] = $totalInstance;
979
+ return $this;
980
+ }
981
+
982
+ /**
983
+ * Rewrite clone method
984
+ *
985
+ * @return Mage_Sales_Model_Quote_Address
986
+ */
987
+ public function __clone()
988
+ {
989
+ $this->setId(null);
990
+ }
991
+
992
+ /**
993
+ * Validate minimum amount
994
+ *
995
+ * @return bool
996
+ */
997
+ public function validateMinimumAmount()
998
+ {
999
+ $storeId = $this->getQuote()->getStoreId();
1000
+ if (!Mage::getStoreConfigFlag('sales/minimum_order/active', $storeId)) {
1001
+ return true;
1002
+ }
1003
+
1004
+ if ($this->getQuote()->getIsVirtual() && $this->getAddressType() == self::TYPE_SHIPPING) {
1005
+ return true;
1006
+ }
1007
+ elseif (!$this->getQuote()->getIsVirtual() && $this->getAddressType() != self::TYPE_SHIPPING) {
1008
+ return true;
1009
+ }
1010
+
1011
+ $amount = Mage::getStoreConfig('sales/minimum_order/amount', $storeId);
1012
+ if ($this->getBaseSubtotalWithDiscount() < $amount) {
1013
+ return false;
1014
+ }
1015
+ return true;
1016
+ }
1017
+
1018
+ /**
1019
+ * Retrieve applied taxes
1020
+ *
1021
+ * @return array
1022
+ */
1023
+ public function getAppliedTaxes()
1024
+ {
1025
+ return unserialize($this->getData('applied_taxes'));
1026
+ }
1027
+
1028
+ /**
1029
+ * Set applied taxes
1030
+ *
1031
+ * @param array $data
1032
+ * @return Mage_Sales_Model_Quote_Address
1033
+ */
1034
+ public function setAppliedTaxes($data)
1035
+ {
1036
+ return $this->setData('applied_taxes', serialize($data));
1037
+ }
1038
+
1039
+ /**
1040
+ * Set shipping amount
1041
+ *
1042
+ * @param float $value
1043
+ * @param bool $alreadyExclTax
1044
+ * @return Mage_Sales_Model_Quote_Address
1045
+ */
1046
+ public function setShippingAmount($value, $alreadyExclTax = false)
1047
+ {
1048
+ return $this->setData('shipping_amount', $value);
1049
+ }
1050
+
1051
+ /**
1052
+ * Set base shipping amount
1053
+ *
1054
+ * @param float $value
1055
+ * @param bool $alreadyExclTax
1056
+ * @return Mage_Sales_Model_Quote_Address
1057
+ */
1058
+ public function setBaseShippingAmount($value, $alreadyExclTax = false)
1059
+ {
1060
+ return $this->setData('base_shipping_amount', $value);
1061
+ }
1062
+
1063
+ /**
1064
+ * Set total amount value
1065
+ *
1066
+ * @param string $code
1067
+ * @param float $amount
1068
+ * @return Mage_Sales_Model_Quote_Address
1069
+ */
1070
+ public function setTotalAmount($code, $amount)
1071
+ {
1072
+ $this->_totalAmounts[$code] = $amount;
1073
+ if ($code != 'subtotal') {
1074
+ $code = $code.'_amount';
1075
+ }
1076
+ $this->setData($code, $amount);
1077
+ return $this;
1078
+ }
1079
+
1080
+ /**
1081
+ * Set total amount value in base store currency
1082
+ *
1083
+ * @param string $code
1084
+ * @param float $amount
1085
+ * @return Mage_Sales_Model_Quote_Address
1086
+ */
1087
+ public function setBaseTotalAmount($code, $amount)
1088
+ {
1089
+ $this->_baseTotalAmounts[$code] = $amount;
1090
+ if ($code != 'subtotal') {
1091
+ $code = $code.'_amount';
1092
+ }
1093
+ $this->setData('base_'.$code, $amount);
1094
+ return $this;
1095
+ }
1096
+
1097
+ /**
1098
+ * Add amount total amount value
1099
+ *
1100
+ * @param string $code
1101
+ * @param float $amount
1102
+ * @return Mage_Sales_Model_Quote_Address
1103
+ */
1104
+ public function addTotalAmount($code, $amount)
1105
+ {
1106
+ $amount = $this->getTotalAmount($code)+$amount;
1107
+ $this->setTotalAmount($code, $amount);
1108
+ return $this;
1109
+ }
1110
+
1111
+ /**
1112
+ * Add amount total amount value in base store currency
1113
+ *
1114
+ * @param string $code
1115
+ * @param float $amount
1116
+ * @return Mage_Sales_Model_Quote_Address
1117
+ */
1118
+ public function addBaseTotalAmount($code, $amount)
1119
+ {
1120
+ $amount = $this->getBaseTotalAmount($code)+$amount;
1121
+ $this->setBaseTotalAmount($code, $amount);
1122
+ return $this;
1123
+ }
1124
+
1125
+ /**
1126
+ * Get total amount value by code
1127
+ *
1128
+ * @param string $code
1129
+ * @return float
1130
+ */
1131
+ public function getTotalAmount($code)
1132
+ {
1133
+ if (isset($this->_totalAmounts[$code])) {
1134
+ return $this->_totalAmounts[$code];
1135
+ }
1136
+ return 0;
1137
+ }
1138
+
1139
+ /**
1140
+ * Get total amount value by code in base store curncy
1141
+ *
1142
+ * @param string $code
1143
+ * @return float
1144
+ */
1145
+ public function getBaseTotalAmount($code)
1146
+ {
1147
+ if (isset($this->_baseTotalAmounts[$code])) {
1148
+ return $this->_baseTotalAmounts[$code];
1149
+ }
1150
+ return 0;
1151
+ }
1152
+
1153
+ /**
1154
+ * Get all total amount values
1155
+ *
1156
+ * @return array
1157
+ */
1158
+ public function getAllTotalAmounts()
1159
+ {
1160
+ return $this->_totalAmounts;
1161
+ }
1162
+
1163
+ /**
1164
+ * Get all total amount values in base currency
1165
+ *
1166
+ * @return array
1167
+ */
1168
+ public function getAllBaseTotalAmounts()
1169
+ {
1170
+ return $this->_baseTotalAmounts;
1171
+ }
1172
+
1173
+ /**
1174
+ * Get subtotal amount with applied discount in base currency
1175
+ *
1176
+ * @return float
1177
+ */
1178
+ public function getBaseSubtotalWithDiscount()
1179
+ {
1180
+ return $this->getBaseSubtotal()+$this->getBaseDiscountAmount();
1181
+ }
1182
+
1183
+ /**
1184
+ * Get subtotal amount with applied discount
1185
+ *
1186
+ * @return float
1187
+ */
1188
+ public function getSubtotalWithDiscount()
1189
+ {
1190
+ return $this->getSubtotal()+$this->getDiscountAmount();
1191
+ }
1192
+
1193
+ /**
1194
+ * Validate address attribute values
1195
+ *
1196
+ * @return bool
1197
+ */
1198
+ public function validate()
1199
+ {
1200
+ $errors = array();
1201
+ $helper = Mage::helper('customer');
1202
+ $this->implodeStreetAddress();
1203
+ if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
1204
+ $errors[] = $helper->__('Please enter the first name.');
1205
+ }
1206
+
1207
+ if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
1208
+ $errors[] = $helper->__('Please enter the last name.');
1209
+ }
1210
+
1211
+ if (!Zend_Validate::is($this->getStreet(1), 'NotEmpty')) {
1212
+ $errors[] = $helper->__('Please enter the street.');
1213
+ }
1214
+
1215
+ if (!Zend_Validate::is($this->getCity(), 'NotEmpty')) {
1216
+ $errors[] = $helper->__('Please enter the city.');
1217
+ }
1218
+
1219
+ if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
1220
+ $errors[] = $helper->__('Please enter the telephone number.');
1221
+ }
1222
+
1223
+ $_havingOptionalZip = Mage::helper('directory')->getCountriesWithOptionalZip();
1224
+ if (!in_array($this->getCountryId(), $_havingOptionalZip) && !Zend_Validate::is($this->getPostcode(), 'NotEmpty')) {
1225
+ $errors[] = $helper->__('Please enter the zip/postal code.');
1226
+ }
1227
+
1228
+ if (!Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
1229
+ $errors[] = $helper->__('Please enter the country.');
1230
+ }
1231
+
1232
+ if ($this->getCountryModel()->getRegionCollection()->getSize()
1233
+ && !Zend_Validate::is($this->getRegionId(), 'NotEmpty')) {
1234
+ $errors[] = $helper->__('Please enter the state/province.');
1235
+ }
1236
+
1237
+ //add filter for P.O. Box
1238
+ if( preg_match("/p\.* *o\.* *box/i", $this->getStreet(1))
1239
+ || preg_match("/p\.* *o\.* *box/i", $this->getStreet(2)) )
1240
+ {
1241
+ $errors[] = $helper->__('We cannot ship to PO boxes.');
1242
+ }
1243
+
1244
+ if (empty($errors) || $this->getShouldIgnoreValidation()) {
1245
+ return true;
1246
+ }
1247
+ return $errors;
1248
+ }
1249
+ }
app/code/local/Matt/Nopobox/etc/config.xml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <config>
2
+ <modules>
3
+ <Matt_Nopobox>
4
+ <version>0.1.0</version>
5
+ </Matt_Nopobox>
6
+ </modules>
7
+ <global>
8
+ <models>
9
+ <sales>
10
+ <rewrite>
11
+ <quote_address>Matt_Nopobox_Model_Quote_Address</quote_address>
12
+ </rewrite>
13
+ </sales>
14
+ </models>
15
+ </global>
16
+ </config>
app/etc/modules/Matt_Nopobox.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <config>
2
+ <modules>
3
+ <Matt_Nopobox>
4
+ <active>true</active>
5
+ <codePool>local</codePool>
6
+ </Matt_Nopobox>
7
+ </modules>
8
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Mage_nopobox</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Eliminate orders marked for shipping to a PO Box .</summary>
10
+ <description>The extension updates the Magento checkout fields to prevent shipping to a PO box. It also provides error messaging to inform customers that they must enter a valid shipping address before proceeding through checkout.</description>
11
+ <notes>Magento No PO Box Shipping Extension 1.0.0 Release!</notes>
12
+ <authors><author><name>Matt</name><user>caijiamx</user><email>caijiamx@gmail.com</email></author></authors>
13
+ <date>2012-01-03</date>
14
+ <time>08:42:24</time>
15
+ <contents><target name="mageetc"><dir name="modules"><file name="Matt_Nopobox.xml" hash="e01e563e58420a463f630ccf0cc11c20"/></dir></target><target name="magelocal"><dir name="Matt"><dir name="Nopobox"><dir name="Model"><dir name="Quote"><file name="Address.php" hash="b46fe60f8cc863e0dc3dcfed58a5412a"/></dir></dir><dir name="etc"><file name="config.xml" hash="f208f6bd28ccdfbfe499eb36c407b838"/></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>