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_Adminhtml_Model_Sales_Order_Create extends Varien_Object implements Mage_Checkout_Model_Cart_Interface
36: {
37: 38: 39: 40: 41:
42: protected $_session;
43:
44: 45: 46: 47: 48:
49: protected $_wishlist;
50:
51: 52: 53: 54: 55:
56: protected $_cart;
57:
58: 59: 60: 61: 62:
63: protected $_compareList;
64:
65: 66: 67: 68: 69:
70: protected $_needCollect;
71:
72: 73: 74: 75: 76:
77: protected $_needCollectCart = false;
78:
79: 80: 81: 82: 83:
84: protected $_isValidate = false;
85:
86: 87: 88: 89: 90:
91: protected $_customer;
92:
93: 94: 95: 96: 97:
98: protected $_customerAddressForm;
99:
100: 101: 102: 103: 104:
105: protected $_customerForm;
106:
107: 108: 109: 110: 111:
112: protected $_errors = array();
113:
114: 115: 116: 117: 118:
119: protected $_quote;
120:
121: public function __construct()
122: {
123: $this->_session = Mage::getSingleton('adminhtml/session_quote');
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function setIsValidate($flag)
133: {
134: $this->_isValidate = (bool)$flag;
135: return $this;
136: }
137:
138: 139: 140: 141: 142:
143: public function getIsValidate()
144: {
145: return $this->_isValidate;
146: }
147:
148: 149: 150: 151: 152: 153:
154: protected function _getQuoteItem($item)
155: {
156: if ($item instanceof Mage_Sales_Model_Quote_Item) {
157: return $item;
158: } elseif (is_numeric($item)) {
159: return $this->getSession()->getQuote()->getItemById($item);
160: }
161: return false;
162: }
163:
164: 165: 166: 167: 168:
169: public function initRuleData()
170: {
171: Mage::register('rule_data', new Varien_Object(array(
172: 'store_id' => $this->_session->getStore()->getId(),
173: 'website_id' => $this->_session->getStore()->getWebsiteId(),
174: 'customer_group_id' => $this->getCustomerGroupId(),
175: )));
176: return $this;
177: }
178:
179: 180: 181: 182: 183: 184:
185: public function setRecollect($flag)
186: {
187: $this->_needCollect = $flag;
188: return $this;
189: }
190:
191: 192: 193: 194: 195: 196:
197: public function recollectCart(){
198: if ($this->_needCollectCart === true) {
199: $this->getCustomerCart()
200: ->collectTotals()
201: ->save();
202: }
203: $this->setRecollect(true);
204: return $this;
205: }
206:
207: 208: 209: 210: 211:
212: public function saveQuote()
213: {
214: if (!$this->getQuote()->getId()) {
215: return $this;
216: }
217:
218: if ($this->_needCollect) {
219: $this->getQuote()->collectTotals();
220: }
221:
222: $this->getQuote()->save();
223: return $this;
224: }
225:
226: 227: 228: 229: 230:
231: public function getSession()
232: {
233: return $this->_session;
234: }
235:
236: 237: 238: 239: 240:
241: public function getQuote()
242: {
243: if (!$this->_quote) {
244: $this->_quote = $this->getSession()->getQuote();
245: }
246: return $this->_quote;
247: }
248:
249: 250: 251: 252: 253: 254:
255: public function setQuote(Mage_Sales_Model_Quote $quote)
256: {
257: $this->_quote = $quote;
258: return $this;
259: }
260:
261: 262: 263: 264: 265: 266:
267: public function initFromOrder(Mage_Sales_Model_Order $order)
268: {
269: if (!$order->getReordered()) {
270: $this->getSession()->setOrderId($order->getId());
271: } else {
272: $this->getSession()->setReordered($order->getId());
273: }
274:
275: 276: 277:
278: $this->getSession()->setCurrencyId($order->getOrderCurrencyCode());
279: if ($order->getCustomerId()) {
280: $this->getSession()->setCustomerId($order->getCustomerId());
281: } else {
282: $this->getSession()->setCustomerId(false);
283: }
284:
285: $this->getSession()->setStoreId($order->getStoreId());
286:
287: 288: 289:
290: $this->initRuleData();
291: foreach ($order->getItemsCollection(
292: array_keys(Mage::getConfig()->getNode('adminhtml/sales/order/create/available_product_types')->asArray()),
293: true
294: ) as $orderItem) {
295:
296: if (!$orderItem->getParentItem()) {
297: if ($order->getReordered()) {
298: $qty = $orderItem->getQtyOrdered();
299: } else {
300: $qty = $orderItem->getQtyOrdered() - $orderItem->getQtyShipped() - $orderItem->getQtyInvoiced();
301: }
302:
303: if ($qty > 0) {
304: $item = $this->initFromOrderItem($orderItem, $qty);
305: if (is_string($item)) {
306: Mage::throwException($item);
307: }
308: }
309: }
310: }
311:
312: $this->_initBillingAddressFromOrder($order);
313: $this->_initShippingAddressFromOrder($order);
314:
315: if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) {
316: $this->setShippingAsBilling(1);
317: }
318:
319: $this->setShippingMethod($order->getShippingMethod());
320: $this->getQuote()->getShippingAddress()->setShippingDescription($order->getShippingDescription());
321:
322: $this->getQuote()->getPayment()->addData($order->getPayment()->getData());
323:
324:
325: $orderCouponCode = $order->getCouponCode();
326: if ($orderCouponCode) {
327: $this->getQuote()->setCouponCode($orderCouponCode);
328: }
329:
330: if ($this->getQuote()->getCouponCode()) {
331: $this->getQuote()->collectTotals();
332: }
333:
334: Mage::helper('core')->copyFieldset(
335: 'sales_copy_order',
336: 'to_edit',
337: $order,
338: $this->getQuote()
339: );
340:
341: Mage::dispatchEvent('sales_convert_order_to_quote', array(
342: 'order' => $order,
343: 'quote' => $this->getQuote()
344: ));
345:
346: if (!$order->getCustomerId()) {
347: $this->getQuote()->setCustomerIsGuest(true);
348: }
349:
350: if ($this->getSession()->getUseOldShippingMethod(true)) {
351: 352: 353: 354: 355:
356: $this->collectShippingRates();
357: } else {
358: 359: 360: 361:
362: $this->collectRates();
363: }
364:
365:
366:
367:
368:
369: $this->getQuote()->save();
370:
371: return $this;
372: }
373:
374: protected function _initBillingAddressFromOrder(Mage_Sales_Model_Order $order)
375: {
376: $this->getQuote()->getBillingAddress()->setCustomerAddressId('');
377: Mage::helper('core')->copyFieldset(
378: 'sales_copy_order_billing_address',
379: 'to_order',
380: $order->getBillingAddress(),
381: $this->getQuote()->getBillingAddress()
382: );
383: }
384:
385: protected function _initShippingAddressFromOrder(Mage_Sales_Model_Order $order)
386: {
387: $this->getQuote()->getShippingAddress()->setCustomerAddressId('');
388: Mage::helper('core')->copyFieldset(
389: 'sales_copy_order_shipping_address',
390: 'to_order',
391: $order->getShippingAddress(),
392: $this->getQuote()->getShippingAddress()
393: );
394: }
395:
396: 397: 398: 399: 400: 401: 402:
403: public function initFromOrderItem(Mage_Sales_Model_Order_Item $orderItem, $qty = null)
404: {
405: if (!$orderItem->getId()) {
406: return $this;
407: }
408:
409: $product = Mage::getModel('catalog/product')
410: ->setStoreId($this->getSession()->getStoreId())
411: ->load($orderItem->getProductId());
412:
413: if ($product->getId()) {
414: $product->setSkipCheckRequiredOption(true);
415: $buyRequest = $orderItem->getBuyRequest();
416: if (is_numeric($qty)) {
417: $buyRequest->setQty($qty);
418: }
419: $item = $this->getQuote()->addProduct($product, $buyRequest);
420: if (is_string($item)) {
421: return $item;
422: }
423:
424: if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
425: $item->addOption(new Varien_Object(
426: array(
427: 'product' => $item->getProduct(),
428: 'code' => 'additional_options',
429: 'value' => serialize($additionalOptions)
430: )
431: ));
432: }
433:
434: Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
435: 'order_item' => $orderItem,
436: 'quote_item' => $item
437: ));
438: return $item;
439: }
440:
441: return $this;
442: }
443:
444: 445: 446: 447: 448: 449:
450: public function getCustomerWishlist($cacheReload = false)
451: {
452: if (!is_null($this->_wishlist) && !$cacheReload) {
453: return $this->_wishlist;
454: }
455:
456: if ($this->getSession()->getCustomer()->getId()) {
457: $this->_wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer(
458: $this->getSession()->getCustomer(), true
459: );
460: $this->_wishlist->setStore($this->getSession()->getStore())
461: ->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds());
462: } else {
463: $this->_wishlist = false;
464: }
465:
466: return $this->_wishlist;
467: }
468:
469: 470: 471: 472: 473:
474: public function getCustomerCart()
475: {
476: if (!is_null($this->_cart)) {
477: return $this->_cart;
478: }
479:
480: $this->_cart = Mage::getModel('sales/quote');
481:
482: if ($this->getSession()->getCustomer()->getId()) {
483: $this->_cart->setStore($this->getSession()->getStore())
484: ->loadByCustomer($this->getSession()->getCustomer()->getId());
485: if (!$this->_cart->getId()) {
486: $this->_cart->assignCustomer($this->getSession()->getCustomer());
487: $this->_cart->save();
488: }
489: }
490:
491: return $this->_cart;
492: }
493:
494: 495: 496: 497: 498:
499: public function getCustomerCompareList()
500: {
501: if (!is_null($this->_compareList)) {
502: return $this->_compareList;
503: }
504:
505: if ($this->getSession()->getCustomer()->getId()) {
506: $this->_compareList = Mage::getModel('catalog/product_compare_list');
507: } else {
508: $this->_compareList = false;
509: }
510: return $this->_compareList;
511: }
512:
513: public function getCustomerGroupId()
514: {
515: $groupId = $this->getQuote()->getCustomerGroupId();
516: if (!$groupId) {
517: $groupId = $this->getSession()->getCustomerGroupId();
518: }
519: return $groupId;
520: }
521:
522: 523: 524: 525: 526: 527: 528: 529:
530: public function moveQuoteItem($item, $moveTo, $qty)
531: {
532: $item = $this->_getQuoteItem($item);
533: if ($item) {
534: $removeItem = false;
535: $moveTo = explode('_', $moveTo);
536: switch ($moveTo[0]) {
537: case 'order':
538: $info = $item->getBuyRequest();
539: $info->setOptions($this->_prepareOptionsForRequest($item))
540: ->setQty($qty);
541:
542: $product = Mage::getModel('catalog/product')
543: ->setStoreId($this->getQuote()->getStoreId())
544: ->load($item->getProduct()->getId());
545:
546: $product->setSkipCheckRequiredOption(true);
547: $newItem = $this->getQuote()->addProduct($product, $info);
548:
549: if (is_string($newItem)) {
550: Mage::throwException($newItem);
551: }
552: $product->unsSkipCheckRequiredOption();
553: $newItem->checkData();
554: $this->_needCollectCart = true;
555: break;
556: case 'cart':
557: $cart = $this->getCustomerCart();
558: if ($cart && is_null($item->getOptionByCode('additional_options'))) {
559:
560: $product = Mage::getModel('catalog/product')
561: ->setStoreId($this->getQuote()->getStoreId())
562: ->load($item->getProduct()->getId());
563:
564: $info = $item->getOptionByCode('info_buyRequest');
565: if ($info) {
566: $info = new Varien_Object(
567: unserialize($info->getValue())
568: );
569: $info->setQty($qty);
570: $info->setOptions($this->_prepareOptionsForRequest($item));
571: } else {
572: $info = new Varien_Object(array(
573: 'product_id' => $product->getId(),
574: 'qty' => $qty,
575: 'options' => $this->_prepareOptionsForRequest($item)
576: ));
577: }
578:
579: $cartItem = $cart->addProduct($product, $info);
580: if (is_string($cartItem)) {
581: Mage::throwException($cartItem);
582: }
583: $cartItem->setPrice($item->getProduct()->getPrice());
584: $this->_needCollectCart = true;
585: $removeItem = true;
586: }
587: break;
588: case 'wishlist':
589: $wishlist = null;
590: if (!isset($moveTo[1])) {
591: $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer(
592: $this->getSession()->getCustomer(),
593: true
594: );
595: } else {
596: $wishlist = Mage::getModel('wishlist/wishlist')->load($moveTo[1]);
597: if (!$wishlist->getId()
598: || $wishlist->getCustomerId() != $this->getSession()->getCustomerId()
599: ) {
600: $wishlist = null;
601: }
602: }
603: if (!$wishlist) {
604: Mage::throwException(Mage::helper('wishlist')->__('Could not find wishlist'));
605: }
606: $wishlist->setStore($this->getSession()->getStore())
607: ->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds());
608:
609: if ($wishlist->getId() && $item->getProduct()->isVisibleInSiteVisibility()) {
610: $info = $item->getBuyRequest();
611: $info->setOptions($this->_prepareOptionsForRequest($item))
612: ->setQty($qty)
613: ->setStoreId($this->getSession()->getStoreId());
614: $wishlist->addNewItem($item->getProduct(), $info);
615: $removeItem = true;
616: }
617: break;
618: case 'remove':
619: $removeItem = true;
620: break;
621: default:
622: break;
623: }
624: if ($removeItem) {
625: $this->getQuote()->removeItem($item->getId());
626: }
627: $this->setRecollect(true);
628: }
629: return $this;
630: }
631:
632: 633: 634: 635: 636: 637:
638: public function ($data)
639: {
640: if (isset($data['add_order_item'])) {
641: foreach ($data['add_order_item'] as $orderItemId => $value) {
642:
643: $orderItem = Mage::getModel('sales/order_item')->load($orderItemId);
644: $item = $this->initFromOrderItem($orderItem);
645: if (is_string($item)) {
646: Mage::throwException($item);
647: }
648: }
649: }
650: if (isset($data['add_cart_item'])) {
651: foreach ($data['add_cart_item'] as $itemId => $qty) {
652: $item = $this->getCustomerCart()->getItemById($itemId);
653: if ($item) {
654: $this->moveQuoteItem($item, 'order', $qty);
655: $this->removeItem($itemId, 'cart');
656: }
657: }
658: }
659: if (isset($data['add_wishlist_item'])) {
660: foreach ($data['add_wishlist_item'] as $itemId => $qty) {
661: $item = Mage::getModel('wishlist/item')
662: ->loadWithOptions($itemId, 'info_buyRequest');
663: if ($item->getId()) {
664: $this->addProduct($item->getProduct(), $item->getBuyRequest()->toArray());
665: }
666: }
667: }
668: if (isset($data['add'])) {
669: foreach ($data['add'] as $productId => $qty) {
670: $this->addProduct($productId, array('qty' => $qty));
671: }
672: }
673: if (isset($data['remove'])) {
674: foreach ($data['remove'] as $itemId => $from) {
675: $this->removeItem($itemId, $from);
676: }
677: }
678: if (isset($data['empty_customer_cart']) && (int)$data['empty_customer_cart'] == 1) {
679: $this->getCustomerCart()->removeAllItems()->collectTotals()->save();
680: }
681: return $this;
682: }
683:
684: 685: 686: 687: 688: 689: 690:
691: public function removeItem($itemId, $from)
692: {
693: switch ($from) {
694: case 'quote':
695: $this->removeQuoteItem($itemId);
696: break;
697: case 'cart':
698: if ($cart = $this->getCustomerCart()) {
699: $cart->removeItem($itemId);
700: $cart->collectTotals()
701: ->save();
702: }
703: break;
704: case 'wishlist':
705: if ($wishlist = $this->getCustomerWishlist()) {
706: $item = Mage::getModel('wishlist/item')->load($itemId);
707: $item->delete();
708: }
709: break;
710: case 'compared':
711: $item = Mage::getModel('catalog/product_compare_item')
712: ->load($itemId)
713: ->delete();
714: break;
715: }
716: return $this;
717: }
718:
719: 720: 721: 722: 723: 724:
725: public function removeQuoteItem($item)
726: {
727: $this->getQuote()->removeItem($item);
728: $this->setRecollect(true);
729: return $this;
730: }
731:
732: 733: 734: 735: 736: 737: 738: 739: 740:
741: public function addProduct($product, $config = 1)
742: {
743: if (!is_array($config) && !($config instanceof Varien_Object)) {
744: $config = array('qty' => $config);
745: }
746: $config = new Varien_Object($config);
747:
748: if (!($product instanceof Mage_Catalog_Model_Product)) {
749: $productId = $product;
750: $product = Mage::getModel('catalog/product')
751: ->setStore($this->getSession()->getStore())
752: ->setStoreId($this->getSession()->getStoreId())
753: ->load($product);
754: if (!$product->getId()) {
755: Mage::throwException(
756: Mage::helper('adminhtml')->__('Failed to add a product to cart by id "%s".', $productId)
757: );
758: }
759: }
760:
761: $stockItem = $product->getStockItem();
762: if ($stockItem && $stockItem->getIsQtyDecimal()) {
763: $product->setIsQtyDecimal(1);
764: } else {
765: $config->setQty((int) $config->getQty());
766: }
767:
768: $product->setCartQty($config->getQty());
769: $item = $this->getQuote()->addProductAdvanced(
770: $product,
771: $config,
772: Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL
773: );
774: if (is_string($item)) {
775: if ($product->getTypeId() != Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
776: $item = $this->getQuote()->addProductAdvanced(
777: $product,
778: $config,
779: Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_LITE
780: );
781: }
782: if (is_string($item)) {
783: Mage::throwException($item);
784: }
785: }
786: $item->checkData();
787:
788: $this->setRecollect(true);
789: return $this;
790: }
791:
792: 793: 794: 795: 796: 797:
798: public function addProducts(array $products)
799: {
800: foreach ($products as $productId => $config) {
801: $config['qty'] = isset($config['qty']) ? (float)$config['qty'] : 1;
802: try {
803: $this->addProduct($productId, $config);
804: }
805: catch (Mage_Core_Exception $e){
806: $this->getSession()->addError($e->getMessage());
807: }
808: catch (Exception $e){
809: return $e;
810: }
811: }
812: return $this;
813: }
814:
815: 816: 817: 818: 819: 820:
821: public function updateQuoteItems($data)
822: {
823: if (is_array($data)) {
824: try {
825: foreach ($data as $itemId => $info) {
826: if (!empty($info['configured'])) {
827: $item = $this->getQuote()->updateItem($itemId, new Varien_Object($info));
828: $itemQty = (float)$item->getQty();
829: } else {
830: $item = $this->getQuote()->getItemById($itemId);
831: $itemQty = (float)$info['qty'];
832: }
833:
834: if ($item) {
835: if ($item->getProduct()->getStockItem()) {
836: if (!$item->getProduct()->getStockItem()->getIsQtyDecimal()) {
837: $itemQty = (int)$itemQty;
838: } else {
839: $item->setIsQtyDecimal(1);
840: }
841: }
842: $itemQty = $itemQty > 0 ? $itemQty : 1;
843: if (isset($info['custom_price'])) {
844: $itemPrice = $this->_parseCustomPrice($info['custom_price']);
845: } else {
846: $itemPrice = null;
847: }
848: $noDiscount = !isset($info['use_discount']);
849:
850: if (empty($info['action']) || !empty($info['configured'])) {
851: $item->setQty($itemQty);
852: $item->setCustomPrice($itemPrice);
853: $item->setOriginalCustomPrice($itemPrice);
854: $item->setNoDiscount($noDiscount);
855: $item->getProduct()->setIsSuperMode(true);
856: $item->getProduct()->unsSkipCheckRequiredOption();
857: $item->checkData();
858: } else {
859: $this->moveQuoteItem($item->getId(), $info['action'], $itemQty);
860: }
861: }
862: }
863: } catch (Mage_Core_Exception $e) {
864: $this->recollectCart();
865: throw $e;
866: } catch (Exception $e) {
867: Mage::logException($e);
868: }
869: $this->recollectCart();
870: }
871: return $this;
872: }
873:
874: 875: 876: 877: 878: 879:
880: protected function _parseOptions(Mage_Sales_Model_Quote_Item $item, $additionalOptions)
881: {
882: $productOptions = Mage::getSingleton('catalog/product_option_type_default')
883: ->setProduct($item->getProduct())
884: ->getProductOptions();
885:
886: $newOptions = array();
887: $newAdditionalOptions = array();
888:
889: foreach (explode("\n", $additionalOptions) as $_additionalOption) {
890: if (strlen(trim($_additionalOption))) {
891: try {
892: if (strpos($_additionalOption, ':') === false) {
893: Mage::throwException(
894: Mage::helper('adminhtml')->__('There is an error in one of the option rows.')
895: );
896: }
897: list($label,$value) = explode(':', $_additionalOption, 2);
898: } catch (Exception $e) {
899: Mage::throwException(Mage::helper('adminhtml')->__('There is an error in one of the option rows.'));
900: }
901: $label = trim($label);
902: $value = trim($value);
903: if (empty($value)) {
904: continue;
905: }
906:
907: if (array_key_exists($label, $productOptions)) {
908: $optionId = $productOptions[$label]['option_id'];
909: $option = $item->getProduct()->getOptionById($optionId);
910:
911: $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
912: ->setOption($option)
913: ->setProduct($item->getProduct());
914:
915: $parsedValue = $group->parseOptionValue($value, $productOptions[$label]['values']);
916:
917: if ($parsedValue !== null) {
918: $newOptions[$optionId] = $parsedValue;
919: } else {
920: $newAdditionalOptions[] = array(
921: 'label' => $label,
922: 'value' => $value
923: );
924: }
925: } else {
926: $newAdditionalOptions[] = array(
927: 'label' => $label,
928: 'value' => $value
929: );
930: }
931: }
932: }
933:
934: return array(
935: 'options' => $newOptions,
936: 'additional_options' => $newAdditionalOptions
937: );
938: }
939:
940: 941: 942: 943: 944: 945:
946: protected function _assignOptionsToItem(Mage_Sales_Model_Quote_Item $item, $options)
947: {
948: if ($optionIds = $item->getOptionByCode('option_ids')) {
949: foreach (explode(',', $optionIds->getValue()) as $optionId) {
950: $item->removeOption('option_'.$optionId);
951: }
952: $item->removeOption('option_ids');
953: }
954: if ($item->getOptionByCode('additional_options')) {
955: $item->removeOption('additional_options');
956: }
957: $item->save();
958: if (!empty($options['options'])) {
959: $item->addOption(new Varien_Object(
960: array(
961: 'product' => $item->getProduct(),
962: 'code' => 'option_ids',
963: 'value' => implode(',', array_keys($options['options']))
964: )
965: ));
966:
967: foreach ($options['options'] as $optionId => $optionValue) {
968: $item->addOption(new Varien_Object(
969: array(
970: 'product' => $item->getProduct(),
971: 'code' => 'option_'.$optionId,
972: 'value' => $optionValue
973: )
974: ));
975: }
976: }
977: if (!empty($options['additional_options'])) {
978: $item->addOption(new Varien_Object(
979: array(
980: 'product' => $item->getProduct(),
981: 'code' => 'additional_options',
982: 'value' => serialize($options['additional_options'])
983: )
984: ));
985: }
986:
987: return $this;
988: }
989:
990: 991: 992: 993: 994: 995:
996: protected function _prepareOptionsForRequest($item)
997: {
998: $newInfoOptions = array();
999: if ($optionIds = $item->getOptionByCode('option_ids')) {
1000: foreach (explode(',', $optionIds->getValue()) as $optionId) {
1001: $option = $item->getProduct()->getOptionById($optionId);
1002: $optionValue = $item->getOptionByCode('option_'.$optionId)->getValue();
1003:
1004: $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
1005: ->setOption($option)
1006: ->setQuoteItem($item);
1007:
1008: $newInfoOptions[$optionId] = $group->prepareOptionValueForRequest($optionValue);
1009: }
1010: }
1011: return $newInfoOptions;
1012: }
1013:
1014: protected function _parseCustomPrice($price)
1015: {
1016: $price = Mage::app()->getLocale()->getNumber($price);
1017: $price = $price>0 ? $price : 0;
1018: return $price;
1019: }
1020:
1021: 1022: 1023: 1024: 1025:
1026: public function getShippingAddress()
1027: {
1028: return $this->getQuote()->getShippingAddress();
1029: }
1030:
1031: 1032: 1033: 1034: 1035:
1036: protected function _getCustomerForm()
1037: {
1038: if (is_null($this->_customerForm)) {
1039: $this->_customerForm = Mage::getModel('customer/form')
1040: ->setFormCode('adminhtml_checkout')
1041: ->ignoreInvisible(false);
1042: }
1043: return $this->_customerForm;
1044: }
1045:
1046: 1047: 1048: 1049: 1050:
1051: protected function _getCustomerAddressForm()
1052: {
1053: if (is_null($this->_customerAddressForm)) {
1054: $this->_customerAddressForm = Mage::getModel('customer/form')
1055: ->setFormCode('adminhtml_customer_address')
1056: ->ignoreInvisible(false);
1057: }
1058: return $this->_customerAddressForm;
1059: }
1060:
1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068:
1069: protected function _setQuoteAddress(Mage_Sales_Model_Quote_Address $address, array $data)
1070: {
1071: $addressForm = $this->_getCustomerAddressForm()
1072: ->setEntity($address)
1073: ->setEntityType(Mage::getSingleton('eav/config')->getEntityType('customer_address'))
1074: ->setIsAjaxRequest(!$this->getIsValidate());
1075:
1076:
1077:
1078: if ($address->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) {
1079: $requestData = array('order' => array('shipping_address' => $data));
1080: $requestScope = 'order/shipping_address';
1081: } else {
1082: $requestData = array('order' => array('billing_address' => $data));
1083: $requestScope = 'order/billing_address';
1084: }
1085: $request = $addressForm->prepareRequest($requestData);
1086: $addressData = $addressForm->extractData($request, $requestScope);
1087: if ($this->getIsValidate()) {
1088: $errors = $addressForm->validateData($addressData);
1089: if ($errors !== true) {
1090: if ($address->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) {
1091: $typeName = Mage::helper('adminhtml')->__('Shipping Address: ');
1092: } else {
1093: $typeName = Mage::helper('adminhtml')->__('Billing Address: ');
1094: }
1095: foreach ($errors as $error) {
1096: $this->_errors[] = $typeName . $error;
1097: }
1098: $addressForm->restoreData($addressData);
1099: } else {
1100: $addressForm->compactData($addressData);
1101: }
1102: } else {
1103: $addressForm->restoreData($addressData);
1104: }
1105:
1106: return $this;
1107: }
1108:
1109: public function setShippingAddress($address)
1110: {
1111: if (is_array($address)) {
1112: $address['save_in_address_book'] = isset($address['save_in_address_book'])
1113: && !empty($address['save_in_address_book']);
1114: $shippingAddress = Mage::getModel('sales/quote_address')
1115: ->setData($address)
1116: ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
1117: if (!$this->getQuote()->isVirtual()) {
1118: $this->_setQuoteAddress($shippingAddress, $address);
1119: }
1120: $shippingAddress->implodeStreetAddress();
1121: }
1122: if ($address instanceof Mage_Sales_Model_Quote_Address) {
1123: $shippingAddress = $address;
1124: }
1125:
1126: $this->setRecollect(true);
1127: $this->getQuote()->setShippingAddress($shippingAddress);
1128: return $this;
1129: }
1130:
1131: public function setShippingAsBilling($flag)
1132: {
1133: if ($flag) {
1134: $tmpAddress = clone $this->getBillingAddress();
1135: $tmpAddress->unsAddressId()
1136: ->unsAddressType();
1137: $data = $tmpAddress->getData();
1138: $data['save_in_address_book'] = 0;
1139: $this->getShippingAddress()->addData($data);
1140: }
1141: $this->getShippingAddress()->setSameAsBilling($flag);
1142: $this->setRecollect(true);
1143: return $this;
1144: }
1145:
1146: 1147: 1148: 1149: 1150:
1151: public function getBillingAddress()
1152: {
1153: return $this->getQuote()->getBillingAddress();
1154: }
1155:
1156: public function setBillingAddress($address)
1157: {
1158: if (is_array($address)) {
1159: $address['save_in_address_book'] = isset($address['save_in_address_book']) ? 1 : 0;
1160: $billingAddress = Mage::getModel('sales/quote_address')
1161: ->setData($address)
1162: ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING);
1163: $this->_setQuoteAddress($billingAddress, $address);
1164: $billingAddress->implodeStreetAddress();
1165: }
1166:
1167: if ($this->getShippingAddress()->getSameAsBilling()) {
1168: $shippingAddress = clone $billingAddress;
1169: $shippingAddress->setSameAsBilling(true);
1170: $shippingAddress->setSaveInAddressBook(false);
1171: $address['save_in_address_book'] = 0;
1172: $this->setShippingAddress($address);
1173: }
1174:
1175: $this->getQuote()->setBillingAddress($billingAddress);
1176: return $this;
1177: }
1178:
1179: public function setShippingMethod($method)
1180: {
1181: $this->getShippingAddress()->setShippingMethod($method);
1182: $this->setRecollect(true);
1183: return $this;
1184: }
1185:
1186: public function resetShippingMethod()
1187: {
1188: $this->getShippingAddress()->setShippingMethod(false);
1189: $this->getShippingAddress()->removeAllShippingRates();
1190: return $this;
1191: }
1192:
1193: 1194: 1195:
1196: public function collectShippingRates()
1197: {
1198: $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
1199: $this->collectRates();
1200: return $this;
1201: }
1202:
1203: public function collectRates()
1204: {
1205: $this->getQuote()->collectTotals();
1206: }
1207:
1208: public function setPaymentMethod($method)
1209: {
1210: $this->getQuote()->getPayment()->setMethod($method);
1211: return $this;
1212: }
1213:
1214: public function setPaymentData($data)
1215: {
1216: if (!isset($data['method'])) {
1217: $data['method'] = $this->getQuote()->getPayment()->getMethod();
1218: }
1219: $this->getQuote()->getPayment()->importData($data);
1220: return $this;
1221: }
1222:
1223: public function applyCoupon($code)
1224: {
1225: $code = trim((string)$code);
1226: $this->getQuote()->setCouponCode($code);
1227: $this->setRecollect(true);
1228: return $this;
1229: }
1230:
1231: public function setAccountData($accountData)
1232: {
1233: $customer = $this->getQuote()->getCustomer();
1234: $form = $this->_getCustomerForm();
1235: $form->setEntity($customer);
1236:
1237:
1238: $request = $form->prepareRequest($accountData);
1239: $data = $form->extractData($request);
1240: $form->restoreData($data);
1241:
1242: $data = array();
1243: foreach ($form->getAttributes() as $attribute) {
1244: $code = sprintf('customer_%s', $attribute->getAttributeCode());
1245: $data[$code] = $customer->getData($attribute->getAttributeCode());
1246: }
1247:
1248: if (isset($data['customer_group_id'])) {
1249: $groupModel = Mage::getModel('customer/group')->load($data['customer_group_id']);
1250: $data['customer_tax_class_id'] = $groupModel->getTaxClassId();
1251: $this->setRecollect(true);
1252: }
1253:
1254: $this->getQuote()->addData($data);
1255: return $this;
1256: }
1257:
1258: 1259: 1260: 1261: 1262: 1263:
1264: public function importPostData($data)
1265: {
1266: if (is_array($data)) {
1267: $this->addData($data);
1268: } else {
1269: return $this;
1270: }
1271:
1272: if (isset($data['account'])) {
1273: $this->setAccountData($data['account']);
1274: }
1275:
1276: if (isset($data['comment'])) {
1277: $this->getQuote()->addData($data['comment']);
1278: if (empty($data['comment']['customer_note_notify'])) {
1279: $this->getQuote()->setCustomerNoteNotify(false);
1280: } else {
1281: $this->getQuote()->setCustomerNoteNotify(true);
1282: }
1283: }
1284:
1285: if (isset($data['billing_address'])) {
1286: $this->setBillingAddress($data['billing_address']);
1287: }
1288:
1289: if (isset($data['shipping_address'])) {
1290: $this->setShippingAddress($data['shipping_address']);
1291: }
1292:
1293: if (isset($data['shipping_method'])) {
1294: $this->setShippingMethod($data['shipping_method']);
1295: }
1296:
1297: if (isset($data['payment_method'])) {
1298: $this->setPaymentMethod($data['payment_method']);
1299: }
1300:
1301: if (isset($data['coupon']['code'])) {
1302: $this->applyCoupon($data['coupon']['code']);
1303: }
1304: return $this;
1305: }
1306:
1307: 1308: 1309: 1310: 1311: 1312:
1313: protected function _customerIsInStore($store)
1314: {
1315: $customer = $this->getSession()->getCustomer();
1316: if ($customer->getWebsiteId() == $store->getWebsiteId()) {
1317: return true;
1318: }
1319: return $customer->isInStore($store);
1320: }
1321:
1322: 1323: 1324: 1325: 1326: 1327:
1328: protected function _setCustomerData(Mage_Customer_Model_Customer $customer)
1329: {
1330: $form = $this->_getCustomerForm();
1331: $form->setEntity($customer);
1332:
1333:
1334: $request = $form->prepareRequest(array('order' => $this->getData()));
1335: $data = $form->extractData($request, 'order/account');
1336: if ($this->getIsValidate()) {
1337: $errors = $form->validateData($data);
1338: if ($errors !== true) {
1339: foreach ($errors as $error) {
1340: $this->_errors[] = $error;
1341: }
1342: $form->restoreData($data);
1343: } else {
1344: $form->compactData($data);
1345: }
1346: } else {
1347: $form->restoreData($data);
1348: }
1349:
1350: return $this;
1351: }
1352:
1353: 1354: 1355: 1356: 1357:
1358: public function _prepareCustomer()
1359: {
1360:
1361: $quote = $this->getQuote();
1362: if ($quote->getCustomerIsGuest()) {
1363: return $this;
1364: }
1365:
1366:
1367: $customer = $this->getSession()->getCustomer();
1368:
1369: $store = $this->getSession()->getStore();
1370:
1371: $customerIsInStore = $this->_customerIsInStore($store);
1372: $customerBillingAddress = null;
1373: $customerShippingAddress = null;
1374:
1375: if ($customer->getId()) {
1376:
1377: if (!$customerIsInStore) {
1378: $customer->setId(null)
1379: ->setStore($store)
1380: ->setDefaultBilling(null)
1381: ->setDefaultShipping(null)
1382: ->setPassword($customer->generatePassword());
1383: $this->_setCustomerData($customer);
1384: }
1385:
1386: if ($this->getBillingAddress()->getSaveInAddressBook()) {
1387:
1388: $customerBillingAddress = $this->getBillingAddress()->exportCustomerAddress();
1389: $customerAddressId = $this->getBillingAddress()->getCustomerAddressId();
1390: if ($customerAddressId && $customer->getId()) {
1391: $customer->getAddressItemById($customerAddressId)->addData($customerBillingAddress->getData());
1392: } else {
1393: $customer->addAddress($customerBillingAddress);
1394: }
1395: }
1396:
1397: if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) {
1398:
1399: $customerShippingAddress = $this->getShippingAddress()->exportCustomerAddress();
1400: $customerAddressId = $this->getShippingAddress()->getCustomerAddressId();
1401: if ($customerAddressId && $customer->getId()) {
1402: $customer->getAddressItemById($customerAddressId)->addData($customerShippingAddress->getData());
1403: } elseif (!empty($customerAddressId)
1404: && $customerBillingAddress !== null
1405: && $this->getBillingAddress()->getCustomerAddressId() == $customerAddressId
1406: ) {
1407: $customerBillingAddress->setIsDefaultShipping(true);
1408: } else {
1409: $customer->addAddress($customerShippingAddress);
1410: }
1411: }
1412:
1413: if (is_null($customer->getDefaultBilling()) && $customerBillingAddress) {
1414: $customerBillingAddress->setIsDefaultBilling(true);
1415: }
1416:
1417: if (is_null($customer->getDefaultShipping())) {
1418: if ($this->getShippingAddress()->getSameAsBilling() && $customerBillingAddress) {
1419: $customerBillingAddress->setIsDefaultShipping(true);
1420: } elseif ($customerShippingAddress) {
1421: $customerShippingAddress->setIsDefaultShipping(true);
1422: }
1423: }
1424: } else {
1425:
1426:
1427: $customerBillingAddress = $this->getBillingAddress()->exportCustomerAddress();
1428: $customer->addData($customerBillingAddress->getData())
1429: ->setPassword($customer->generatePassword())
1430: ->setStore($store);
1431: $customer->setEmail($this->_getNewCustomerEmail($customer));
1432: $this->_setCustomerData($customer);
1433:
1434: if ($this->getBillingAddress()->getSaveInAddressBook()) {
1435: $customerBillingAddress->setIsDefaultBilling(true);
1436: $customer->addAddress($customerBillingAddress);
1437: }
1438:
1439:
1440: $shippingAddress = $this->getShippingAddress();
1441: if (!$this->getQuote()->isVirtual()
1442: && !$shippingAddress->getSameAsBilling()
1443: && $shippingAddress->getSaveInAddressBook()
1444: ) {
1445:
1446: $customerShippingAddress = $shippingAddress->exportCustomerAddress();
1447: $customerShippingAddress->setIsDefaultShipping(true);
1448: $customer->addAddress($customerShippingAddress);
1449: } else {
1450: $customerBillingAddress->setIsDefaultShipping(true);
1451: }
1452: }
1453:
1454:
1455: $this->_setCustomerData($customer);
1456:
1457:
1458: $quote->setCustomer($customer);
1459:
1460:
1461: $form = $this->_getCustomerForm()->setEntity($customer);
1462: foreach ($form->getUserAttributes() as $attribute) {
1463: $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode());
1464: $quote->setData($quoteCode, $customer->getData($attribute->getAttributeCode()));
1465: }
1466:
1467: if ($customer->getId()) {
1468:
1469: $this->_getCustomerForm()
1470: ->setEntity($customer)
1471: ->resetEntityData();
1472: } else {
1473: $quote->setCustomerId(true);
1474: }
1475:
1476: return $this;
1477: }
1478:
1479: 1480: 1481:
1482: protected function _prepareQuoteItems()
1483: {
1484: foreach ($this->getQuote()->getAllItems() as $item) {
1485: $options = array();
1486: $productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
1487: if ($productOptions) {
1488: $productOptions['info_buyRequest']['options'] = $this->_prepareOptionsForRequest($item);
1489: $options = $productOptions;
1490: }
1491: $addOptions = $item->getOptionByCode('additional_options');
1492: if ($addOptions) {
1493: $options['additional_options'] = unserialize($addOptions->getValue());
1494: }
1495: $item->setProductOrderOptions($options);
1496: }
1497: return $this;
1498: }
1499:
1500: 1501: 1502: 1503: 1504:
1505: public function createOrder()
1506: {
1507: $this->_prepareCustomer();
1508: $this->_validate();
1509: $quote = $this->getQuote();
1510: $this->_prepareQuoteItems();
1511:
1512: $service = Mage::getModel('sales/service_quote', $quote);
1513: if ($this->getSession()->getOrder()->getId()) {
1514: $oldOrder = $this->getSession()->getOrder();
1515: $originalId = $oldOrder->getOriginalIncrementId();
1516: if (!$originalId) {
1517: $originalId = $oldOrder->getIncrementId();
1518: }
1519: $orderData = array(
1520: 'original_increment_id' => $originalId,
1521: 'relation_parent_id' => $oldOrder->getId(),
1522: 'relation_parent_real_id' => $oldOrder->getIncrementId(),
1523: 'edit_increment' => $oldOrder->getEditIncrement()+1,
1524: 'increment_id' => $originalId.'-'.($oldOrder->getEditIncrement()+1)
1525: );
1526: $quote->setReservedOrderId($orderData['increment_id']);
1527: $service->setOrderData($orderData);
1528: }
1529:
1530: $order = $service->submit();
1531: if ((!$quote->getCustomer()->getId() || !$quote->getCustomer()->isInStore($this->getSession()->getStore()))
1532: && !$quote->getCustomerIsGuest()
1533: ) {
1534: $quote->getCustomer()->setCreatedAt($order->getCreatedAt());
1535: $quote->getCustomer()
1536: ->save()
1537: ->sendNewAccountEmail('registered', '', $quote->getStoreId());;
1538: }
1539: if ($this->getSession()->getOrder()->getId()) {
1540: $oldOrder = $this->getSession()->getOrder();
1541:
1542: $this->getSession()->getOrder()->setRelationChildId($order->getId());
1543: $this->getSession()->getOrder()->setRelationChildRealId($order->getIncrementId());
1544: $this->getSession()->getOrder()->cancel()
1545: ->save();
1546: $order->save();
1547: }
1548: if ($this->getSendConfirmation()) {
1549: $order->sendNewOrderEmail();
1550: }
1551:
1552: Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));
1553:
1554: return $order;
1555: }
1556:
1557: 1558: 1559: 1560: 1561:
1562: protected function _validate()
1563: {
1564: $customerId = $this->getSession()->getCustomerId();
1565: if (is_null($customerId)) {
1566: Mage::throwException(Mage::helper('adminhtml')->__('Please select a customer.'));
1567: }
1568:
1569: if (!$this->getSession()->getStore()->getId()) {
1570: Mage::throwException(Mage::helper('adminhtml')->__('Please select a store.'));
1571: }
1572: $items = $this->getQuote()->getAllItems();
1573:
1574: if (count($items) == 0) {
1575: $this->_errors[] = Mage::helper('adminhtml')->__('You need to specify order items.');
1576: }
1577:
1578: foreach ($items as $item) {
1579: $messages = $item->getMessage(false);
1580: if ($item->getHasError() && is_array($messages) && !empty($messages)) {
1581: $this->_errors = array_merge($this->_errors, $messages);
1582: }
1583: }
1584:
1585: if (!$this->getQuote()->isVirtual()) {
1586: if (!$this->getQuote()->getShippingAddress()->getShippingMethod()) {
1587: $this->_errors[] = Mage::helper('adminhtml')->__('Shipping method must be specified.');
1588: }
1589: }
1590:
1591: if (!$this->getQuote()->getPayment()->getMethod()) {
1592: $this->_errors[] = Mage::helper('adminhtml')->__('Payment method must be specified.');
1593: } else {
1594: $method = $this->getQuote()->getPayment()->getMethodInstance();
1595: if (!$method) {
1596: $this->_errors[] = Mage::helper('adminhtml')->__('Payment method instance is not available.');
1597: } else {
1598: if (!$method->isAvailable($this->getQuote())) {
1599: $this->_errors[] = Mage::helper('adminhtml')->__('Payment method is not available.');
1600: } else {
1601: try {
1602: $method->validate();
1603: } catch (Mage_Core_Exception $e) {
1604: $this->_errors[] = $e->getMessage();
1605: }
1606: }
1607: }
1608: }
1609:
1610: if (!empty($this->_errors)) {
1611: foreach ($this->_errors as $error) {
1612: $this->getSession()->addError($error);
1613: }
1614: Mage::throwException('');
1615: }
1616: return $this;
1617: }
1618:
1619: 1620: 1621: 1622: 1623: 1624:
1625: protected function _getNewCustomerEmail($customer)
1626: {
1627: $email = $this->getData('account/email');
1628: if (empty($email)) {
1629: $host = $this->getSession()
1630: ->getStore()
1631: ->getConfig(Mage_Customer_Model_Customer::XML_PATH_DEFAULT_EMAIL_DOMAIN);
1632: $account = $customer->getIncrementId() ? $customer->getIncrementId() : time();
1633: $email = $account.'@'. $host;
1634: $account = $this->getData('account');
1635: $account['email'] = $email;
1636: $this->setData('account', $account);
1637: }
1638: return $email;
1639: }
1640:
1641: 1642: 1643: 1644:
1645: protected function _putCustomerIntoQuote()
1646: {
1647: if (!$this->getSession()->getCustomer()->getId()) {
1648:
1649: $customer = Mage::getModel('customer/customer');
1650:
1651: $customer->addData($this->getBillingAddress()->exportCustomerAddress()->getData())
1652: ->addData($this->getData('account'))
1653: ->setPassword($customer->generatePassword())
1654: ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
1655: ->setStoreId($this->getSession()->getStore()->getId())
1656: ->setEmail($this->_getNewCustomerEmail($customer));
1657: } elseif (($customer = $this->getSession()->getCustomer())
1658: && $customer->getId()
1659: && !$this->getSession()->getCustomer(true, true)->getId()
1660: ) {
1661: $customer = clone $customer;
1662: $customer->setStore($this->getSession()->getStore())
1663: ->save();
1664: $this->getSession()->setCustomer($customer);
1665: $customer->addData($this->getData('account'));
1666: } else {
1667: $customer = $this->getSession()->getCustomer();
1668: $customer->addData($this->getData('account'));
1669: }
1670: $this->getQuote()->setCustomer($customer);
1671: $this->_customer = $customer;
1672: }
1673:
1674: 1675: 1676: 1677: 1678: 1679:
1680: protected function _saveCustomerAfterOrder($order)
1681: {
1682: if ($this->_customer) {
1683: if (! $this->_customer->getId()) {
1684: $billing = $this->getBillingAddress();
1685: $customerBilling = $billing->exportCustomerAddress();
1686: $shipping = $this->getShippingAddress();
1687: $customerShipping = $shipping->exportCustomerAddress();
1688:
1689: $this->_customer->addAddress($customerBilling);
1690:
1691: if (! $shipping->getSameAsBilling()) {
1692: $this->_customer->addAddress($customerShipping);
1693: }
1694:
1695: $this->_customer->save();
1696:
1697: $defShipping = $shipping->getSameAsBilling() ? $customerBilling->getId() : $customerShipping->getId();
1698: $this->_customer
1699: ->setDefaultBilling($customerBilling->getId())
1700: ->setDefaultShipping($defShipping)
1701: ->save();
1702:
1703: $order->setCustomerId($this->_customer->getId());
1704: $billing->setCustomerId($this->_customer->getId());
1705: $shipping->setCustomerId($this->_customer->getId());
1706: $this->_customer->sendNewAccountEmail('registered', '', $order->getStoreId());
1707: } else {
1708: $saveCusstomerAddress = false;
1709:
1710: if ($this->getBillingAddress()->getSaveInAddressBook()) {
1711: $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
1712: if ($this->getBillingAddress()->getCustomerAddressId()) {
1713: $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
1714: }
1715: $this->_customer->addAddress($billingAddress);
1716: $saveCusstomerAddress = true;
1717: }
1718: if ($this->getShippingAddress()->getSaveInAddressBook()) {
1719: $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
1720: if ($this->getShippingAddress()->getCustomerAddressId()) {
1721: $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
1722: }
1723: $this->_customer->addAddress($shippingAddress);
1724: $saveCusstomerAddress = true;
1725: }
1726: if ($saveCusstomerAddress) {
1727: $this->_customer->save();
1728: }
1729: }
1730: }
1731: }
1732:
1733: 1734: 1735: 1736:
1737: protected function _saveCustomer()
1738: {
1739: if (!$this->getSession()->getCustomer()->getId()) {
1740: $customer = Mage::getModel('customer/customer');
1741:
1742:
1743: $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
1744:
1745: $customer->addData($billingAddress->getData())
1746: ->addData($this->getData('account'))
1747: ->setPassword($customer->generatePassword())
1748: ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
1749: ->setStoreId($this->getSession()->getStore()->getId())
1750: ->addAddress($billingAddress);
1751:
1752: if (!$this->getShippingAddress()->getSameAsBilling()) {
1753: $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
1754: $customer->addAddress($shippingAddress);
1755: } else {
1756: $shippingAddress = $billingAddress;
1757: }
1758: $customer->save();
1759:
1760:
1761: $customer->setEmail($this->_getNewCustomerEmail($customer))
1762: ->setDefaultBilling($billingAddress->getId())
1763: ->setDefaultShipping($shippingAddress->getId())
1764: ->save();
1765:
1766: $this->getBillingAddress()->setCustomerId($customer->getId());
1767: $this->getShippingAddress()->setCustomerId($customer->getId());
1768:
1769: $customer->sendNewAccountEmail('registered', '', $customer->getStoreId());
1770: } else {
1771: $customer = $this->getSession()->getCustomer();
1772:
1773: $saveCusstomerAddress = false;
1774:
1775: if ($this->getBillingAddress()->getSaveInAddressBook()) {
1776: $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
1777: if ($this->getBillingAddress()->getCustomerAddressId()) {
1778: $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
1779: }
1780: $customer->addAddress($billingAddress);
1781: $saveCusstomerAddress = true;
1782: }
1783: if ($this->getShippingAddress()->getSaveInAddressBook()) {
1784: $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
1785: if ($this->getShippingAddress()->getCustomerAddressId()) {
1786: $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
1787: }
1788: $customer->addAddress($shippingAddress);
1789: $saveCusstomerAddress = true;
1790: }
1791: if ($saveCusstomerAddress) {
1792: $customer->save();
1793: }
1794:
1795: $customer->addData($this->getData('account'));
1796: 1797: 1798:
1799:
1800: }
1801: $this->getQuote()->setCustomer($customer);
1802: return $this;
1803: }
1804: }
1805: