Mage_Adminhtml_Model_Sales_Order_Create Class Reference

Inheritance diagram for Mage_Adminhtml_Model_Sales_Order_Create:

Varien_Object

List of all members.

Public Member Functions

 __construct ()
 initRuleData ()
 setRecollect ($flag)
 saveQuote ()
 getSession ()
 getQuote ()
 initFromOrder (Mage_Sales_Model_Order $order)
 initFromOrderItem (Mage_Sales_Model_Order_Item $orderItem, $qty=1)
 getCustomerWishlist ()
 getCustomerCart ()
 getCustomerCompareList ()
 getCustomerGroupId ()
 moveQuoteItem ($item, $moveTo, $qty)
 applySidebarData ($data)
 removeItem ($itemId, $from)
 removeQuoteItem ($item)
 addProduct ($product, $qty=1)
 addProducts (array $products)
 updateQuoteItems ($data)
 getShippingAddress ()
 setShippingAddress ($address)
 setShippingAsBilling ($flag)
 getBillingAddress ()
 setBillingAddress ($address)
 setShippingMethod ($method)
 resetShippingMethod ()
 collectShippingRates ()
 collectRates ()
 setPaymentMethod ($method)
 setPaymentData ($data)
 applyCoupon ($code)
 setAccountData ($accountData)
 importPostData ($data)
 createOrder ()

Protected Member Functions

 _getQuoteItem ($item)
 _initBillingAddressFromOrder (Mage_Sales_Model_Order $order)
 _initShippingAddressFromOrder (Mage_Sales_Model_Order $order)
 _parseOptions (Mage_Sales_Model_Quote_Item $item, $additionalOptions)
 _assignOptionsToItem (Mage_Sales_Model_Quote_Item $item, $options)
 _prepareOptionsForRequest ($item)
 _parseCustomPrice ($price)
 _validate ()
 _putCustomerIntoQuote ()
 _saveCustomerAfterOrder ($order)
 _saveCustomer ()
 _getNewCustomerEmail ($customer)

Protected Attributes

 $_session
 $_wishlist
 $_cart
 $_compareList
 $_needCollect
 $_customer


Detailed Description

Order create model

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file Create.php.


Constructor & Destructor Documentation

__construct (  ) 

Constructor

By default is looking for first argument as array and assignes it as object attributes This behaviour may change in child classes

Reimplemented from Varien_Object.

Definition at line 57 of file Create.php.

00058     {
00059         $this->_session = Mage::getSingleton('adminhtml/session_quote');
00060     }


Member Function Documentation

_assignOptionsToItem ( Mage_Sales_Model_Quote_Item item,
options 
) [protected]

Assign options to item

Parameters:
Mage_Sales_Model_Quote_Item $item
array $options

Definition at line 733 of file Create.php.

00734     {
00735         if ($optionIds = $item->getOptionByCode('option_ids')) {
00736             foreach (explode(',', $optionIds->getValue()) as $optionId) {
00737                 $item->removeOption('option_'.$optionId);
00738             }
00739             $item->removeOption('option_ids');
00740         }
00741         if ($item->getOptionByCode('additional_options')) {
00742             $item->removeOption('additional_options');
00743         }
00744         $item->save();
00745         if (!empty($options['options'])) {
00746             $item->addOption(new Varien_Object(
00747                 array(
00748                     'product' => $item->getProduct(),
00749                     'code' => 'option_ids',
00750                     'value' => implode(',', array_keys($options['options']))
00751                 )
00752             ));
00753 
00754             foreach ($options['options'] as $optionId => $optionValue) {
00755                 $item->addOption(new Varien_Object(
00756                     array(
00757                         'product' => $item->getProduct(),
00758                         'code' => 'option_'.$optionId,
00759                         'value' => $optionValue
00760                     )
00761                 ));
00762             }
00763         }
00764         if (!empty($options['additional_options'])) {
00765             $item->addOption(new Varien_Object(
00766                 array(
00767                     'product' => $item->getProduct(),
00768                     'code' => 'additional_options',
00769                     'value' => serialize($options['additional_options'])
00770                 )
00771             ));
00772         }
00773 
00774         return $this;
00775     }

_getNewCustomerEmail ( customer  )  [protected]

Retrieve new customer email

Parameters:
Mage_Customer_Model_Customer $customer
Returns:
string

Definition at line 1310 of file Create.php.

01311     {
01312         $email = $this->getData('account/email');
01313         if (empty($email)) {
01314             $host = $this->getSession()->getStore()->getConfig(Mage_Customer_Model_Customer::XML_PATH_DEFAULT_EMAIL_DOMAIN);
01315             $account = $customer->getIncrementId() ? $customer->getIncrementId() : time();
01316             $email = $account.'@'. $host;
01317         }
01318         return $email;
01319     }

_getQuoteItem ( item  )  [protected]

Retrieve quote item

Parameters:
mixed $item
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 68 of file Create.php.

00069     {
00070         if ($item instanceof Mage_Sales_Model_Quote_Item) {
00071             return $item;
00072         }
00073         elseif (is_numeric($item)) {
00074             return $this->getSession()->getQuote()->getItemById($item);
00075         }
00076         return false;
00077     }

_initBillingAddressFromOrder ( Mage_Sales_Model_Order order  )  [protected]

Definition at line 236 of file Create.php.

00237     {
00238         $this->getQuote()->getBillingAddress()->setCustomerAddressId('');
00239         Mage::helper('core')->copyFieldset(
00240             'sales_copy_order_billing_address',
00241             'to_order',
00242             $order->getBillingAddress(),
00243             $this->getQuote()->getBillingAddress()
00244         );
00245     }

_initShippingAddressFromOrder ( Mage_Sales_Model_Order order  )  [protected]

Definition at line 247 of file Create.php.

00248     {
00249         $this->getQuote()->getShippingAddress()->setCustomerAddressId('');
00250         Mage::helper('core')->copyFieldset(
00251             'sales_copy_order_shipping_address',
00252             'to_order',
00253             $order->getShippingAddress(),
00254             $this->getQuote()->getShippingAddress()
00255         );
00256     }

_parseCustomPrice ( price  )  [protected]

Definition at line 801 of file Create.php.

00802     {
00803         $price = Mage::app()->getLocale()->getNumber($price);
00804         $price = $price>0 ? $price : 0;
00805         return $price;
00806     }

_parseOptions ( Mage_Sales_Model_Quote_Item item,
additionalOptions 
) [protected]

Parse additional options and sync them with product options

Parameters:
Mage_Sales_Model_Quote_Item $product
array $options

Definition at line 671 of file Create.php.

00672     {
00673         $productOptions = Mage::getSingleton('catalog/product_option_type_default')
00674             ->setProduct($item->getProduct())
00675             ->getProductOptions();
00676 
00677         $newOptions = array();
00678         $newAdditionalOptions = array();
00679 
00680         foreach (explode("\n", $additionalOptions) as $_additionalOption) {
00681             if (strlen(trim($_additionalOption))) {
00682                 try {
00683                     list($label,$value) = explode(':', $_additionalOption, 2);
00684                 } catch (Exception $e) {
00685                     Mage::throwException(Mage::helper('adminhtml')->__('One of options row has error'));
00686                 }
00687                 $label = trim($label);
00688                 $value = trim($value);
00689                 if (empty($value)) {
00690                     die($label);
00691                     continue;
00692                 }
00693 
00694                 if (array_key_exists($label, $productOptions)) {
00695                     $optionId = $productOptions[$label]['option_id'];
00696                     $option = $item->getProduct()->getOptionById($optionId);
00697 
00698                     $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
00699                         ->setOption($option)
00700                         ->setProduct($item->getProduct());
00701 
00702                     $parsedValue = $group->parseOptionValue($value, $productOptions[$label]['values']);
00703 
00704                     if ($parsedValue !== null) {
00705                         $newOptions[$optionId] = $parsedValue;
00706                     } else {
00707                         $newAdditionalOptions[] = array(
00708                             'label' => $label,
00709                             'value' => $value
00710                         );
00711                     }
00712                 } else {
00713                     $newAdditionalOptions[] = array(
00714                         'label' => $label,
00715                         'value' => $value
00716                     );
00717                 }
00718             }
00719         }
00720 
00721         return array(
00722             'options' => $newOptions,
00723             'additional_options' => $newAdditionalOptions
00724         );
00725     }

_prepareOptionsForRequest ( item  )  [protected]

Prepare options array for info buy request

Parameters:
Mage_Sales_Model_Quote_Item $item
Returns:
array

Definition at line 783 of file Create.php.

00784     {
00785         $newInfoOptions = array();
00786         if ($optionIds = $item->getOptionByCode('option_ids')) {
00787             foreach (explode(',', $optionIds->getValue()) as $optionId) {
00788                 $option = $item->getProduct()->getOptionById($optionId);
00789                 $optionValue = $item->getOptionByCode('option_'.$optionId)->getValue();
00790 
00791                 $group = Mage::getSingleton('catalog/product_option')->groupFactory($option->getType())
00792                     ->setOption($option)
00793                     ->setQuoteItem($item);
00794 
00795                 $newInfoOptions[$optionId] = $group->prepareOptionValueForRequest($optionValue);
00796             }
00797         }
00798         return $newInfoOptions;
00799     }

_putCustomerIntoQuote (  )  [protected]

Create customer model and assign it to quote

Definition at line 1153 of file Create.php.

01154     {
01155         if (!$this->getSession()->getCustomer()->getId()) {
01156             $customer = Mage::getModel('customer/customer');
01157             /* @var $customer Mage_Customer_Model_Customer*/
01158 
01159             $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01160 
01161             $customer->addData($billingAddress->getData())
01162                 ->addData($this->getData('account'))
01163                 ->setPassword($customer->generatePassword())
01164                 ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
01165                 ->setStoreId($this->getSession()->getStore()->getId())
01166                 ->addAddress($billingAddress);
01167 
01168             if (!$this->getShippingAddress()->getSameAsBilling()) {
01169                 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01170                 $customer->addAddress($shippingAddress);
01171             }
01172             else {
01173                 $shippingAddress = $billingAddress;
01174             }
01175 
01176             $customer->setEmail($this->_getNewCustomerEmail($customer))
01177                 ->setDefaultBilling($billingAddress->getId())
01178                 ->setDefaultShipping($shippingAddress->getId());
01179         }
01180         else {
01181             $customer = $this->getSession()->getCustomer();
01182             $customer->addData($this->getData('account'));
01183         }
01184         $this->getQuote()->setCustomer($customer);
01185         $this->_customer = $customer;
01186     }

_saveCustomer (  )  [protected]

Deprecated since 1.1.7

Returns:
unknown

don't save account information, use it only for order creation

Definition at line 1234 of file Create.php.

01235     {
01236         if (!$this->getSession()->getCustomer()->getId()) {
01237             $customer = Mage::getModel('customer/customer');
01238             /* @var $customer Mage_Customer_Model_Customer*/
01239 
01240             $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01241 
01242             $customer->addData($billingAddress->getData())
01243                 ->addData($this->getData('account'))
01244                 ->setPassword($customer->generatePassword())
01245                 ->setWebsiteId($this->getSession()->getStore()->getWebsiteId())
01246                 ->setStoreId($this->getSession()->getStore()->getId())
01247                 ->addAddress($billingAddress);
01248 
01249             if (!$this->getShippingAddress()->getSameAsBilling()) {
01250                 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01251                 $customer->addAddress($shippingAddress);
01252             }
01253             else {
01254                 $shippingAddress = $billingAddress;
01255             }
01256             $customer->save();
01257 
01258 
01259             $customer->setEmail($this->_getNewCustomerEmail($customer))
01260                 ->setDefaultBilling($billingAddress->getId())
01261                 ->setDefaultShipping($shippingAddress->getId())
01262                 ->save();
01263 
01264             $this->getBillingAddress()->setCustomerId($customer->getId());
01265             $this->getShippingAddress()->setCustomerId($customer->getId());
01266 
01267             $customer->sendNewAccountEmail();
01268         }
01269         else {
01270             $customer = $this->getSession()->getCustomer();
01271 
01272             $saveCusstomerAddress = false;
01273 
01274             if ($this->getBillingAddress()->getSaveInAddressBook()) {
01275                 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01276                 if ($this->getBillingAddress()->getCustomerAddressId()) {
01277                     $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
01278                 }
01279                 $customer->addAddress($billingAddress);
01280                 $saveCusstomerAddress = true;
01281             }
01282             if ($this->getShippingAddress()->getSaveInAddressBook()) {
01283                 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01284                 if ($this->getShippingAddress()->getCustomerAddressId()) {
01285                     $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
01286                 }
01287                 $customer->addAddress($shippingAddress);
01288                 $saveCusstomerAddress = true;
01289             }
01290             if ($saveCusstomerAddress) {
01291                 $customer->save();
01292             }
01293 
01294             $customer->addData($this->getData('account'));
01295             /**
01296              * don't save account information, use it only for order creation
01297              */
01298             //$customer->save();
01299         }
01300         $this->getQuote()->setCustomer($customer);
01301         return $this;
01302     }

_saveCustomerAfterOrder ( order  )  [protected]

Save customer

Parameters:
Mage_Customer_Model_Customer $order

Definition at line 1193 of file Create.php.

01194     {
01195         if ($this->_customer) {
01196             if (!$this->_customer->getId()) {
01197                 $this->_customer->save();
01198                 $order->setCustomerId($this->_customer->getId());
01199                 $this->getBillingAddress()->setCustomerId($this->_customer->getId());
01200                 $this->getShippingAddress()->setCustomerId($this->_customer->getId());
01201                 $this->_customer->sendNewAccountEmail();
01202             }
01203             else {
01204                 $saveCusstomerAddress = false;
01205 
01206                 if ($this->getBillingAddress()->getSaveInAddressBook()) {
01207                     $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
01208                     if ($this->getBillingAddress()->getCustomerAddressId()) {
01209                         $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
01210                     }
01211                     $this->_customer->addAddress($billingAddress);
01212                     $saveCusstomerAddress = true;
01213                 }
01214                 if ($this->getShippingAddress()->getSaveInAddressBook()) {
01215                     $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
01216                     if ($this->getShippingAddress()->getCustomerAddressId()) {
01217                         $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
01218                     }
01219                     $this->_customer->addAddress($shippingAddress);
01220                     $saveCusstomerAddress = true;
01221                 }
01222                 if ($saveCusstomerAddress) {
01223                     $this->_customer->save();
01224                 }
01225             }
01226         }
01227     }

_validate (  )  [protected]

Validate quote data before order creation

Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 1114 of file Create.php.

01115     {
01116         $customerId = $this->getSession()->getCustomerId();
01117         if (is_null($customerId)) {
01118             Mage::throwException(Mage::helper('adminhtml')->__('Please select a custmer'));
01119         }
01120 
01121         if (!$this->getSession()->getStore()->getId()) {
01122             Mage::throwException(Mage::helper('adminhtml')->__('Please select a store'));
01123         }
01124         $items = $this->getQuote()->getAllItems();
01125 
01126         $errors = array();
01127         if (count($items) == 0) {
01128             $errors[] = Mage::helper('adminhtml')->__('You need to specify order items');
01129         }
01130 
01131         if (!$this->getQuote()->isVirtual()) {
01132             if (!$this->getQuote()->getShippingAddress()->getShippingMethod()) {
01133                 $errors[] = Mage::helper('adminhtml')->__('Shipping method must be specified');
01134             }
01135 
01136             if (!$this->getQuote()->getPayment()->getMethod()) {
01137                 $errors[] = Mage::helper('adminhtml')->__('Payment method must be specified');
01138             }
01139         }
01140 
01141         if (!empty($errors)) {
01142             foreach ($errors as $error) {
01143                 $this->getSession()->addError($error);
01144             }
01145             Mage::throwException('');
01146         }
01147         return $this;
01148     }

addProduct ( product,
qty = 1 
)

Add product to current order quote

Parameters:
mixed $product
mixed $qty
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 555 of file Create.php.

00556     {
00557         $qty = (int) $qty;
00558         if (!($product instanceof Mage_Catalog_Model_Product)) {
00559             $productId = $product;
00560             $product = Mage::getModel('catalog/product')
00561                 ->setStore($this->getSession()->getStore())
00562                 ->setStoreId($this->getSession()->getStoreId())
00563                 ->load($product);
00564             if (!$product->getId()) {
00565                 Mage::throwException(Mage::helper('adminhtml')->__('Failed to add a product to cart by id "%s"', $productId));
00566             }
00567         }
00568 
00569         if ($item = $this->getQuote()->getItemByProduct($product)) {
00570             $item->setQty($item->getQty()+$qty);
00571         }
00572         else {
00573             $product->setSkipCheckRequiredOption(true);
00574             $item = $this->getQuote()->addProduct($product, $qty);
00575             $product->unsSkipCheckRequiredOption();
00576             $item->checkData();
00577         }
00578 
00579         $this->setRecollect(true);
00580         return $this;
00581     }

addProducts ( array products  ) 

Add multiple products to current order quote

Parameters:
array $products
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 589 of file Create.php.

00590     {
00591         foreach ($products as $productId => $data) {
00592             $qty = isset($data['qty']) ? (int)$data['qty'] : 1;
00593             try {
00594                 $this->addProduct($productId, $qty);
00595             }
00596             catch (Mage_Core_Exception $e){
00597                 $this->getSession()->addError($e->getMessage());
00598             }
00599             catch (Exception $e){
00600                 return $e;
00601             }
00602         }
00603         return $this;
00604     }

applyCoupon ( code  ) 

Definition at line 921 of file Create.php.

00922     {
00923         $code = trim((string)$code);
00924         $this->getQuote()->setCouponCode($code);
00925         $this->setRecollect(true);
00926         return $this;
00927     }

applySidebarData ( data  ) 

Definition at line 468 of file Create.php.

00469     {
00470         if (isset($data['add'])) {
00471             foreach ($data['add'] as $productId => $qty) {
00472                 $this->addProduct($productId, $qty);
00473             }
00474         }
00475         if (isset($data['reorder'])) {
00476             foreach ($data['reorder'] as $orderItemId=>$value) {
00477                 $orderItem = Mage::getModel('sales/order_item')->load($orderItemId);
00478                 $item = $this->initFromOrderItem($orderItem);
00479                 if (is_string($item)) {
00480                     Mage::throwException($item);
00481                 }
00482             }
00483         }
00484         if (isset($data['cartItem'])) {
00485             foreach ($data['cartItem'] as $itemId => $qty) {
00486                 if ($item = $this->getCustomerCart()->getItemById($itemId)) {
00487                     $this->moveQuoteItem($item, 'order', $qty);
00488 //                    $this->removeItem($itemId, 'cart');
00489                 }
00490             }
00491         }
00492         if (isset($data['remove'])) {
00493             foreach ($data['remove'] as $itemId => $from) {
00494                 $this->removeItem($itemId, $from);
00495             }
00496         }
00497         return $this;
00498     }

collectRates (  ) 

Definition at line 901 of file Create.php.

00902     {
00903         $this->getQuote()->collectTotals();
00904     }

collectShippingRates (  ) 

Definition at line 893 of file Create.php.

00894     {
00895         $this->collectRates();
00896         $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
00897         $this->getQuote()->getShippingAddress()->collectShippingRates();
00898         return $this;
00899     }

createOrder (  ) 

Create new order

Returns:
Mage_Sales_Model_Order

Definition at line 997 of file Create.php.

00998     {
00999         $this->_validate();
01000 
01001         if (!$this->getQuote()->getCustomerIsGuest()) {
01002             $this->_putCustomerIntoQuote();
01003         }
01004 
01005         $quoteConvert = Mage::getModel('sales/convert_quote');
01006 
01007         /* @var $quoteConvert Mage_Sales_Model_Convert_Quote */
01008 
01009         $quote = $this->getQuote();
01010         if (!$this->getSession()->getOrder()->getId()) {
01011             $quote->reserveOrderId();
01012         }
01013 
01014         if ($this->getQuote()->getIsVirtual()) {
01015             $order = $quoteConvert->addressToOrder($quote->getBillingAddress());
01016         }
01017         else {
01018             $order = $quoteConvert->addressToOrder($quote->getShippingAddress());
01019         }
01020         $order->setBillingAddress($quoteConvert->addressToOrderAddress($quote->getBillingAddress()))
01021             ->setPayment($quoteConvert->paymentToOrderPayment($quote->getPayment()));
01022         if (!$this->getQuote()->getIsVirtual()) {
01023             $order->setShippingAddress($quoteConvert->addressToOrderAddress($quote->getShippingAddress()));
01024         }
01025 
01026         if (!$this->getQuote()->getIsVirtual()) {
01027             foreach ($quote->getShippingAddress()->getAllItems() as $item) {
01028                 /* @var $item Mage_Sales_Model_Quote_Item */
01029                 $orderItem = $quoteConvert->itemToOrderItem($item);
01030                 $options = array();
01031                 if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
01032                     $productOptions['info_buyRequest']['options'] = $this->_prepareOptionsForRequest($item);
01033                     $options = $productOptions;
01034                 }
01035                 if ($addOptions = $item->getOptionByCode('additional_options')) {
01036                     $options['additional_options'] = unserialize($addOptions->getValue());
01037                 }
01038                 if ($options) {
01039                     $orderItem->setProductOptions($options);
01040                 }
01041 
01042                 if ($item->getParentItem()) {
01043                     $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
01044                 }
01045 
01046                 $order->addItem($orderItem);
01047             }
01048         }
01049         if ($this->getQuote()->hasVirtualItems()) {
01050             foreach ($quote->getBillingAddress()->getAllItems() as $item) {
01051                 /* @var $item Mage_Sales_Model_Quote_Item */
01052                 $orderItem = $quoteConvert->itemToOrderItem($item);
01053                 $options = array();
01054                 if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
01055                     $productOptions['info_buyRequest']['options'] = $this->_prepareOptionsForRequest($item);
01056                     $options = $productOptions;
01057                 }
01058                 if ($addOptions = $item->getOptionByCode('additional_options')) {
01059                     $options['additional_options'] = unserialize($addOptions->getValue());
01060                 }
01061                 if ($options) {
01062                     $orderItem->setProductOptions($options);
01063                 }
01064 
01065                 if ($item->getParentItem()) {
01066                     $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
01067                 }
01068 
01069                 $order->addItem($orderItem);
01070             }
01071         }
01072 
01073         if ($this->getSendConfirmation()) {
01074             $order->setEmailSent(true);
01075         }
01076 
01077         if ($this->getSession()->getOrder()->getId()) {
01078             $oldOrder = $this->getSession()->getOrder();
01079 
01080             $originalId = $oldOrder->getOriginalIncrementId() ? $oldOrder->getOriginalIncrementId() : $oldOrder->getIncrementId();
01081             $order->setOriginalIncrementId($originalId);
01082             $order->setRelationParentId($oldOrder->getId());
01083             $order->setRelationParentRealId($oldOrder->getIncrementId());
01084             $order->setEditIncrement($oldOrder->getEditIncrement()+1);
01085             $order->setIncrementId($originalId.'-'.$order->getEditIncrement());
01086         }
01087 
01088         $order->place();
01089         $this->_saveCustomerAfterOrder($order);
01090         $order->save();
01091 
01092         if ($this->getSession()->getOrder()->getId()) {
01093             $oldOrder = $this->getSession()->getOrder();
01094 
01095             $this->getSession()->getOrder()->setRelationChildId($order->getId());
01096             $this->getSession()->getOrder()->setRelationChildRealId($order->getIncrementId());
01097             $this->getSession()->getOrder()->cancel()
01098                 ->save();
01099             $order->save();
01100         }
01101 
01102         if ($this->getSendConfirmation()) {
01103             $order->sendNewOrderEmail();
01104         }
01105 
01106         return $order;
01107     }

getBillingAddress (  ) 

Retrieve quote billing address

Returns:
Mage_Sales_Model_Quote_Address

Definition at line 853 of file Create.php.

00854     {
00855         return $this->getQuote()->getBillingAddress();
00856     }

getCustomerCart (  ) 

Retrieve customer cart quote object model

Returns:
Mage_Sales_Model_Quote

Definition at line 333 of file Create.php.

00334     {
00335         if (!is_null($this->_cart)) {
00336             return $this->_cart;
00337         }
00338 
00339         $this->_cart = Mage::getModel('sales/quote');
00340 
00341         if ($this->getSession()->getCustomer()->getId()) {
00342             $this->_cart->setStore($this->getSession()->getStore())
00343                 ->loadByCustomer($this->getSession()->getCustomer()->getId());
00344             if (!$this->_cart->getId()) {
00345                 $this->_cart->assignCustomer($this->getSession()->getCustomer());
00346                 $this->_cart->save();
00347             }
00348         }
00349 
00350         return $this->_cart;
00351     }

getCustomerCompareList (  ) 

Retrieve customer compare list model object

Returns:
Mage_Catalog_Model_Product_Compare_List

Definition at line 358 of file Create.php.

00359     {
00360         if (!is_null($this->_compareList)) {
00361             return $this->_compareList;
00362         }
00363 
00364         if ($this->getSession()->getCustomer()->getId()) {
00365             $this->_compareList = Mage::getModel('catalog/product_compare_list');
00366         }
00367         else {
00368             $this->_compareList = false;
00369         }
00370         return $this->_compareList;
00371     }

getCustomerGroupId (  ) 

Definition at line 373 of file Create.php.

00374     {
00375         $groupId = $this->getQuote()->getCustomerGroupId();
00376         if (!$groupId) {
00377             $groupId = $this->getSession()->getCustomerGroupId();
00378         }
00379         return $groupId;
00380     }

getCustomerWishlist (  ) 

Retrieve customer wishlist model object

Returns:
Mage_Wishlist_Model_Wishlist

Definition at line 308 of file Create.php.

00309     {
00310         if (!is_null($this->_wishlist)) {
00311             return $this->_wishlist;
00312         }
00313 
00314         if ($this->getSession()->getCustomer()->getId()) {
00315             $this->_wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer(
00316                 $this->getSession()->getCustomer(), true
00317             );
00318             $this->_wishlist->setStore($this->getSession()->getStore())
00319                 ->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds());
00320         }
00321         else {
00322             $this->_wishlist = false;
00323         }
00324 
00325         return $this->_wishlist;
00326     }

getQuote (  ) 

Retrieve quote object model

Returns:
Mage_Sales_Model_Quote

Definition at line 139 of file Create.php.

00140     {
00141         return $this->getSession()->getQuote();
00142     }

getSession (  ) 

Retrieve session model object of quote

Returns:
Mage_Adminhtml_Model_Session_Quote

Definition at line 129 of file Create.php.

00130     {
00131         return $this->_session;
00132     }

getShippingAddress (  ) 

Retrieve oreder quote shipping address

Returns:
Mage_Sales_Model_Quote_Address

Definition at line 813 of file Create.php.

00814     {
00815         return $this->getQuote()->getShippingAddress();
00816     }

importPostData ( data  ) 

Parse data retrieved from request

Parameters:
array $data
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 952 of file Create.php.

00953     {
00954         if (is_array($data)) {
00955             $this->addData($data);
00956         }
00957         else {
00958             return $this;
00959         }
00960 
00961         if (isset($data['account'])) {
00962             $this->setAccountData($data['account']);
00963         }
00964 
00965         if (isset($data['comment'])) {
00966             $this->getQuote()->addData($data['comment']);
00967         }
00968 
00969         if (isset($data['billing_address'])) {
00970             $this->setBillingAddress($data['billing_address']);
00971         }
00972 
00973         if (isset($data['shipping_address'])) {
00974             $this->setShippingAddress($data['shipping_address']);
00975         }
00976 
00977         if (isset($data['shipping_method'])) {
00978             $this->setShippingMethod($data['shipping_method']);
00979         }
00980 
00981         if (isset($data['payment_method'])) {
00982             $this->setPaymentMethod($data['payment_method']);
00983         }
00984 
00985         if (isset($data['coupon']['code'])) {
00986             $this->applyCoupon($data['coupon']['code']);
00987         }
00988 
00989         return $this;
00990     }

initFromOrder ( Mage_Sales_Model_Order order  ) 

Initialize creation data from existing order

Parameters:
Mage_Sales_Model_Order $order
Returns:
unknown

Definition at line 150 of file Create.php.

00151     {
00152         if (!$order->getReordered()) {
00153             $this->getSession()->setOrderId($order->getId());
00154         } else {
00155             $this->getSession()->setReordered($order->getId());
00156         }
00157 
00158         $this->getSession()->setCurrencyId($order->getOrderCurrencyCode());
00159         $this->getSession()->setCustomerId($order->getCustomerId());
00160         $this->getSession()->setStoreId($order->getStoreId());
00161 
00162         foreach ($order->getItemsCollection(
00163             array_keys(Mage::getConfig()->getNode('adminhtml/sales/order/create/available_product_types')->asArray()),
00164             true
00165             ) as $orderItem) {
00166             /* @var $orderItem Mage_Sales_Model_Order_Item */
00167             if (!$orderItem->getParentItem()) {
00168                 if ($order->getReordered()) {
00169                     $qty = $orderItem->getQtyOrdered();
00170                 }
00171                 else {
00172                     $qty = $orderItem->getQtyOrdered() - $orderItem->getQtyShipped() - $orderItem->getQtyInvoiced();
00173                 }
00174 
00175                 if ($qty > 0) {
00176                     $item = $this->initFromOrderItem($orderItem, $qty);
00177                     if (is_string($item)) {
00178                         Mage::throwException($item);
00179                     }
00180                 }
00181             }
00182         }
00183 
00184         $this->_initBillingAddressFromOrder($order);
00185         $this->_initShippingAddressFromOrder($order);
00186 
00187         $this->setShippingMethod($order->getShippingMethod());
00188         $this->getQuote()->getShippingAddress()->setShippingDescription($order->getShippingDescription());
00189 
00190         $this->getQuote()->getPayment()->addData($order->getPayment()->getData());
00191 
00192         if ($this->getQuote()->getCouponCode()) {
00193             $this->getQuote()->collectTotals();
00194         }
00195 
00196         Mage::helper('core')->copyFieldset(
00197             'sales_copy_order',
00198             'to_edit',
00199             $order,
00200             $this->getQuote()
00201         );
00202 
00203         Mage::dispatchEvent('sales_convert_order_to_quote', array(
00204             'order' => $order,
00205             'quote' => $this->getQuote()
00206         ));
00207 
00208         if (!$order->getCustomerId()) {
00209             $this->getQuote()->setCustomerIsGuest(true);
00210         }
00211 
00212         if ($this->getSession()->getUseOldShippingMethod(true)) {
00213             /*
00214              * if we are making reorder or editing old order
00215              * we need to show old shipping as preselected
00216              * so for this we need to collect shipping rates
00217              */
00218             $this->collectShippingRates();
00219         } else {
00220             /*
00221              * if we are creating new order then we don't need to collect
00222              * shipping rates before customer hit appropriate button
00223              */
00224             $this->collectRates();
00225         }
00226 
00227         // Make collect rates when user click "Get shipping methods and rates" in order creating
00228         // $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
00229         // $this->getQuote()->getShippingAddress()->collectShippingRates();
00230 
00231         $this->getQuote()->save();
00232 
00233         return $this;
00234     }

initFromOrderItem ( Mage_Sales_Model_Order_Item orderItem,
qty = 1 
)

Initialize creation data from existing order Item

Parameters:
Mage_Sales_Model_Order_Item $orderItem
Returns:
Mage_Sales_Model_Quote_Item | string

Definition at line 264 of file Create.php.

00265     {
00266         if (!$orderItem->getId()) {
00267             return $this;
00268         }
00269 
00270         $product = Mage::getModel('catalog/product')
00271             ->setStoreId($this->getSession()->getStoreId())
00272             ->load($orderItem->getProductId());
00273 
00274         if ($product->getId()) {
00275             $info = $orderItem->getProductOptionByCode('info_buyRequest');
00276             $info = new Varien_Object($info);
00277             $product->setSkipCheckRequiredOption(true);
00278             $item = $this->getQuote()->addProduct($product,$info);
00279             if (is_string($item)) {
00280                 return $item;
00281             }
00282             $item->setQty($qty);
00283             if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
00284                 $item->addOption(new Varien_Object(
00285                     array(
00286                         'product' => $item->getProduct(),
00287                         'code' => 'additional_options',
00288                         'value' => serialize($additionalOptions)
00289                     )
00290                 ));
00291             }
00292             Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
00293                 'order_item' => $orderItem,
00294                 'quote_item' => $item
00295             ));
00296 
00297             return $item;
00298         }
00299 
00300         return $this;
00301     }

initRuleData (  ) 

Initialize data for prise rules

Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 84 of file Create.php.

00085     {
00086         Mage::register('rule_data', new Varien_Object(array(
00087             'store_id'  => $this->_session->getStore()->getId(),
00088             'website_id'  => $this->_session->getStore()->getWebsiteId(),
00089             'customer_group_id' => $this->getCustomerGroupId(),
00090         )));
00091         return $this;
00092     }

moveQuoteItem ( item,
moveTo,
qty 
)

Move quote item to another items store

Parameters:
mixed $item
string $mogeTo
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 389 of file Create.php.

00390     {
00391         if ($item = $this->_getQuoteItem($item)) {
00392             switch ($moveTo) {
00393                 case 'order':
00394                     $info = array();
00395                     if ($info = $item->getOptionByCode('info_buyRequest')) {
00396                         $info = new Varien_Object(
00397                             unserialize($info->getValue())
00398                         );
00399                         $info->setOptions($this->_prepareOptionsForRequest($item));
00400                     }
00401 
00402                     $product = Mage::getModel('catalog/product')
00403                         ->setStoreId($this->getQuote()->getStoreId())
00404                         ->load($item->getProduct()->getId());
00405 
00406                     $product->setSkipCheckRequiredOption(true);
00407 
00408                     $newItem = $this->getQuote()->addProduct($product, $info);
00409                     if (is_string($newItem)) {
00410                         Mage::throwException($newItem);
00411                     }
00412                     $product->unsSkipCheckRequiredOption();
00413                     $newItem->checkData();
00414                     $newItem->setQty($qty);
00415                     $this->getQuote()->collectTotals()
00416                         ->save();
00417                     break;
00418                 case 'cart':
00419                     if (($cart = $this->getCustomerCart()) && is_null($item->getOptionByCode('additional_options'))) {
00420                         //options and info buy request
00421                         $product = Mage::getModel('catalog/product')
00422                             ->setStoreId($this->getQuote()->getStoreId())
00423                             ->load($item->getProduct()->getId());
00424                         $product->setSkipCheckRequiredOption(true);
00425 
00426                         $info = array();
00427                         if ($info = $item->getOptionByCode('info_buyRequest')) {
00428                             $info = new Varien_Object(
00429                                 unserialize($info->getValue())
00430                             );
00431                             $info->setOptions($this->_prepareOptionsForRequest($item));
00432                         } else {
00433                             $info = new Varien_Object(array(
00434                                 'product_id' => $product->getId(),
00435                                 'qty' => $qty,
00436                                 'options' => $this->_prepareOptionsForRequest($item)
00437                             ));
00438                         }
00439 
00440                         $cartItem = $cart->addProduct($product, $info);
00441                         if (is_string($cartItem)) {
00442                             Mage::throwException($cartItem);
00443                         }
00444                         $product->unsSkipCheckRequiredOption();
00445                         $cartItem->setQty($qty);
00446                         $cartItem->setPrice($item->getProduct()->getPrice());
00447                         $cart->collectTotals()
00448                             ->save();
00449                     }
00450                     break;
00451                 case 'wishlist':
00452                     if ($wishlist = $this->getCustomerWishlist()) {
00453                         $wishlist->addNewItem($item->getProduct()->getId());
00454                     }
00455                     break;
00456                 case 'comparelist':
00457 
00458                     break;
00459                 default:
00460                     break;
00461             }
00462             $this->getQuote()->removeItem($item->getId());
00463             $this->setRecollect(true);
00464         }
00465         return $this;
00466     }

removeItem ( itemId,
from 
)

Remove item from some of customer items storage (shopping cart, wishlist etc.)

Parameters:
int $itemId
string $from
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 507 of file Create.php.

00508     {
00509         switch ($from) {
00510             case 'quote':
00511                 $this->removeQuoteItem($itemId);
00512                 break;
00513             case 'cart':
00514                 if ($cart = $this->getCustomerCart()) {
00515                     $cart->removeItem($itemId);
00516                     $cart->collectTotals()
00517                         ->save();
00518                 }
00519                 break;
00520             case 'wishlist':
00521                 if ($wishlist = $this->getCustomerWishlist()) {
00522                     $item = Mage::getModel('wishlist/item')->load($itemId);
00523                     $item->delete();
00524                 }
00525                 break;
00526             case 'compared':
00527                 $item = Mage::getModel('catalog/product_compare_item')
00528                     ->load($itemId)
00529                     ->delete();
00530                 break;
00531         }
00532         return $this;
00533     }

removeQuoteItem ( item  ) 

Remove quote item

Parameters:
int $item
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 541 of file Create.php.

00542     {
00543         $this->getQuote()->removeItem($item);
00544         $this->setRecollect(true);
00545         return $this;
00546     }

resetShippingMethod (  ) 

Definition at line 886 of file Create.php.

00887     {
00888         $this->getShippingAddress()->setShippingMethod(false);
00889         $this->getShippingAddress()->removeAllShippingRates();
00890         return $this;
00891     }

saveQuote (  ) 

Quote saving

Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 111 of file Create.php.

00112     {
00113         if (!$this->getQuote()->getId()) {
00114             return $this;
00115         }
00116 
00117         if ($this->_needCollect) {
00118             $this->getQuote()->collectTotals();
00119         }
00120         $this->getQuote()->save();
00121         return $this;
00122     }

setAccountData ( accountData  ) 

Definition at line 929 of file Create.php.

00930     {
00931         $data = array();
00932         foreach ($accountData as $key => $value) {
00933             $data['customer_'.$key] = $value;
00934         }
00935 
00936         if (isset($data['customer_group_id'])) {
00937             $groupModel = Mage::getModel('customer/group')->load($data['customer_group_id']);
00938             $data['customer_tax_class_id'] = $groupModel->getTaxClassId();
00939             $this->setRecollect(true);
00940         }
00941 
00942         $this->getQuote()->addData($data);
00943         return $this;
00944     }

setBillingAddress ( address  ) 

Definition at line 858 of file Create.php.

00859     {
00860         if (is_array($address)) {
00861             $address['save_in_address_book'] = isset($address['save_in_address_book']) ? 1 : 0;
00862             $billingAddress = Mage::getModel('sales/quote_address')
00863                 ->setData($address);
00864             $billingAddress->implodeStreetAddress();
00865         }
00866 
00867         if ($this->getShippingAddress()->getSameAsBilling()) {
00868             $shippingAddress = clone $billingAddress;
00869             $shippingAddress->setSameAsBilling(true);
00870             $shippingAddress->setSaveInAddressBook(false);
00871             $address['save_in_address_book'] = 0;
00872             $this->setShippingAddress($address);
00873         }
00874 
00875         $this->getQuote()->setBillingAddress($billingAddress);
00876         return $this;
00877     }

setPaymentData ( data  ) 

Definition at line 912 of file Create.php.

00913     {
00914         if (!isset($data['method'])) {
00915             $data['method'] = $this->getQuote()->getPayment()->getMethod();
00916         }
00917         $this->getQuote()->getPayment()->importData($data);
00918         return $this;
00919     }

setPaymentMethod ( method  ) 

Definition at line 906 of file Create.php.

00907     {
00908         $this->getQuote()->getPayment()->setMethod($method);
00909         return $this;
00910     }

setRecollect ( flag  ) 

Set collect totals flag for quote

Parameters:
bool $flag
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 100 of file Create.php.

00101     {
00102         $this->_needCollect = $flag;
00103         return $this;
00104     }

setShippingAddress ( address  ) 

Definition at line 818 of file Create.php.

00819     {
00820         if (is_array($address)) {
00821             $address['save_in_address_book'] = isset($address['save_in_address_book']) ? (empty($address['save_in_address_book']) ? 0 : 1) : 0;
00822             $shippingAddress = Mage::getModel('sales/quote_address')
00823                 ->setData($address);
00824             $shippingAddress->implodeStreetAddress();
00825         }
00826         if ($address instanceof Mage_Sales_Model_Quote_Address) {
00827             $shippingAddress = $address;
00828         }
00829 
00830         $this->setRecollect(true);
00831         $this->getQuote()->setShippingAddress($shippingAddress);
00832         return $this;
00833     }

setShippingAsBilling ( flag  ) 

Definition at line 835 of file Create.php.

00836     {
00837         if ($flag) {
00838             $tmpAddress = clone $this->getBillingAddress();
00839             $tmpAddress->unsAddressId()
00840                 ->unsAddressType();
00841             $this->getShippingAddress()->addData($tmpAddress->getData());
00842         }
00843         $this->getShippingAddress()->setSameAsBilling($flag);
00844         $this->setRecollect(true);
00845         return $this;
00846     }

setShippingMethod ( method  ) 

Definition at line 879 of file Create.php.

00880     {
00881         $this->getShippingAddress()->setShippingMethod($method);
00882         $this->setRecollect(true);
00883         return $this;
00884     }

updateQuoteItems ( data  ) 

Update quantity of order quote items

Parameters:
array $data
Returns:
Mage_Adminhtml_Model_Sales_Order_Create

Definition at line 612 of file Create.php.

00613     {
00614         if (is_array($data)) {
00615             foreach ($data as $itemId => $info) {
00616                 $itemQty    = (int) $info['qty'];
00617                 $itemQty    = $itemQty>0 ? $itemQty : 1;
00618                 if (isset($info['custom_price'])) {
00619                     $itemPrice  = $this->_parseCustomPrice($info['custom_price']);
00620                 }
00621                 else {
00622                     $itemPrice = null;
00623                 }
00624                 $noDiscount = !isset($info['use_discount']);
00625 
00626 //                if ($item = $this->getQuote()->getItemById($itemId)) {
00627 //                    $this->_assignOptionsToItem(
00628 //                        $item,
00629 //                        $this->_parseOptions($item, $info['options'])
00630 //                    );
00631 //                    if (empty($info['action'])) {
00632 //                        $item->setQty($itemQty);
00633 //                        $item->setCustomPrice($itemPrice);
00634 //                        $item->setNoDiscount($noDiscount);
00635 //                    }
00636 //                    else {
00637 //                        $this->moveQuoteItem($item, $info['action'], $itemQty);
00638 //                    }
00639 //                }
00640 
00641                 if (empty($info['action'])) {
00642                     if ($item = $this->getQuote()->getItemById($itemId)) {
00643 
00644                         $item->setQty($itemQty);
00645                         $item->setCustomPrice($itemPrice);
00646                         $item->setNoDiscount($noDiscount);
00647                         $item->getProduct()->setIsSuperMode(true);
00648 
00649                         $this->_assignOptionsToItem(
00650                             $item,
00651                             $this->_parseOptions($item, $info['options'])
00652                         );
00653                         $item->checkData();
00654                     }
00655                 }
00656                 else {
00657                     $this->moveQuoteItem($itemId, $info['action'], $itemQty);
00658                 }
00659             }
00660             $this->setRecollect(true);
00661         }
00662         return $this;
00663     }


Member Data Documentation

$_cart [protected]

Definition at line 47 of file Create.php.

$_compareList [protected]

Definition at line 48 of file Create.php.

$_customer [protected]

Definition at line 55 of file Create.php.

$_needCollect [protected]

Definition at line 50 of file Create.php.

$_session [protected]

Definition at line 39 of file Create.php.

$_wishlist [protected]

Definition at line 46 of file Create.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:13 2009 for Magento by  doxygen 1.5.8