Hello,
We am using Magento EE 1.14.1 to built a online clothes store.
I created a module to split the pre-order items and regular items into two different orders which is working fine. However, when checking on the order detail in Sales->orders->view in back-end, and the client's order history, they all are missing the product selected options(eg: size, color etc.). This feature was created by rewriting the onepage.php, SaveOrder class, please see the code below. Can anyone please help me with the code to check what did I miss? Thanks in advance.
Onepage.php
class Namespace_Splitorder_Model_Checkout_Type_Onepage extends Mage_Checkout_Model_Type_Onepage{
protected $_subOrders = array();
protected $_initialPaymentMethod = '';
public function saveOrder()
{
$this->_checkoutSession->unsetData('suborders');
$quote = $this->getQuote();
$backUpProducts = array();
$configurableType = Mage::getModel('catalog/product_type_configurable');
$sortedItems = array();
foreach ($quote->getAllVisibleItems() as $item) {
$product = Mage::getModel('catalog/product')->load($item->getProductId());
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$attributes = $configurableType->getConfigurableAttributesAsArray($product);
} else {
$attributes = array();
}
$optionsArray = $item->getBuyRequest()->getData();
$simpleItems = array();
if (count($item->getChildren())) {
$superAttributes = array();
foreach ($item->getChildren() as $childrenItem) {
foreach ($attributes as $attribute) {
$childrenProduct = Mage::getModel('catalog/product')->load($childrenItem->getProductId());
$superAttributes[$attribute['attribute_id']] = $childrenProduct->getData($attribute['attribute_code']);
}
$optionsArray['super_attribute'] = $superAttributes;
$simpleItems[] = $childrenItem;
}
} else {
$simpleItems[] = $item;
}
$backUpProducts[] = $optionsArray;
$parentProduct = Mage::getModel('catalog/product')->load($item->getProductId());
if ($parentProduct->getId()) {
$orderSeparator = $parentProduct->getData('pre_order_date');
if ($parentProduct->isVirtual()) {
$orderSeparator = 'virtual';
}
}
foreach ($simpleItems as $simpleItem) {
$productId = $simpleItem->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
if ($product->getId()) {
$orderSeparator = $orderSeparator ?: $product->getData('pre_order_date');
if (!$orderSeparator) {
$orderSeparator = 'order';
}
if ($product->isVirtual()) {
$orderSeparator = 'virtual';
}
if (!array_key_exists($orderSeparator, $sortedItems)) {
$sortedItems[$orderSeparator] = array();
}
if (!in_array($simpleItem, $sortedItems[$orderSeparator])) {
if ($simpleItem->getParentItem()) {
$sortedItems[$orderSeparator][] = $simpleItem->getParentItem();
} else {
$sortedItems[$orderSeparator][] = $simpleItem;
}
}
}
}
}
$this->_initialPaymentMethod = $quote->getPayment()->getData();
$_initialPaymentMethod = $quote->getPayment()->getMethod();
$customerBalanceUsed = $quote->getData('customer_balance_amount_used');
$useCustomerBalance = $quote->getData('use_customer_balance');
if (count($sortedItems) > 1) {
foreach ($sortedItems as $orderSeparator => $items) {
$quote->getPayment()->importData($this->_initialPaymentMethod);
// Empty quote
$quote->removeAllItems();
Mage::log('items count:'.count($quote->getAllItems()),Zend_Log::DEBUG,"split_order.log", true);
Mage::log('items :'.PHP_EOL.var_export($quote->getAllItems(),true),Zend_Log::DEBUG,"split_order.log", true);
$newItems = array();
foreach ($items as $item) {
if ($item->getChildren()) {
foreach ($item->getChildren() as $childrenItem) {
$newChildren = $quote->addProduct($childrenItem->getProduct(),
$childrenItem->getBuyRequest())->save();
$newItems[] = $newChildren;
}
} else {
$newItem = $quote->addProduct($item->getProduct(), $item->getBuyRequest())->save();
$newItems[] = $newItem;
}
}
if($orderSeparator === 'virtual'){
$quote->setIsVirtual(true);
}else{
$quote->setIsVirtual(false);
}
$quote->getShippingAddress()->unsetData('cached_items_all');
$quote->getShippingAddress()->unsetData('cached_items_nominal');
$quote->getShippingAddress()->unsetData('cached_items_nonnominal');
$quote->getBillingAddress()->unsetData('cached_items_all');
$quote->getBillingAddress()->unsetData('cached_items_nominal');
$quote->getBillingAddress()->unsetData('cached_items_nonnominal');
$quote->getBillingAddress()->setCollectShippingRates(true)->collectShippingRates();
$quote->getBillingAddress()->collectTotals();
if ($useCustomerBalance) {
$quote->setData('use_customer_balance', true);
$storeCredit = min($customerBalanceUsed, $quote->getData('grand_total'));
$quote->setData('customer_balance_amount_used', $storeCredit);
$quote->setData('base_customer_balance_amount_used', $storeCredit);
$customerBalanceUsed = max(0, $customerBalanceUsed - $storeCredit);
$useCustomerBalance = $customerBalanceUsed ? true : false;
$quote->save();
}
$quote->setTotalsCollectedFlag(false)->collectTotals()->save();
$quote->getPayment()->importData($this->_initialPaymentMethod);
if ($orderSeparator === 'order' || $orderSeparator === 'virtual') {
$quote->getPayment()->setMethod($_initialPaymentMethod);
} else {
if ($_initialPaymentMethod !== 'free' && $quote->getGrandTotal() > 0) {
$quote->getPayment();
} else {
$quote->getPayment()->setMethod($_initialPaymentMethod);
}
}
try {
parent::saveOrder();
} catch (Exception $e) {
foreach ($quote->getAllItems() as $item) {
$quote->deleteItem($item)->save();
$quote->getItemsCollection()->removeItemByKey($item->getId());
}
$quote->getShippingAddress()->unsetData('cached_items_all');
$quote->getShippingAddress()->unsetData('cached_items_nominal');
$quote->getShippingAddress()->unsetData('cached_items_nonnominal');
$quote->save();
foreach (Mage::getSingleton('checkout/session')->getQuote()->getAllItems() as $item) {
Mage::getSingleton('checkout/session')->getQuote()->deleteItem($item);
}
Mage::getSingleton('checkout/session')->getQuote()->save();
Mage::getSingleton('checkout/cart')->truncate()->save();
foreach ($this->_subOrders as $savedOrderId) {
$savedOrder = Mage::getModel('sales/order')->load($savedOrderId, 'increment_id');
$savedOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, '', true)->save();
}
$session = Mage::getSingleton('customer/session');
$cart = Mage::getSingleton('checkout/cart');
$newQuote = Mage::getModel('sales/quote');
$cart->init();
$cart->setQuote($newQuote);
foreach ($backUpProducts as $productRequest) {
$product = Mage::getModel('catalog/product')->load($productRequest['product']);
$cart->addProduct($product, $productRequest);
}
$session->setCartWasUpdated(true);
$cart->save();
Mage::getSingleton('checkout/session')->setQuoteId($cart->getQuote()->getId());
throw $e;
}
$this->_subOrders[] = $this->getLastOrderId();
}
} else {
$orderSeparator = key($sortedItems);
if ($orderSeparator !== 'order' && $orderSeparator !== 'virtual') {
$paymentMethod = $quote->getPayment()->getMethod();
if ($paymentMethod !== 'free') {
$quote->getPayment();
}
}
parent::saveOrder();
$this->_subOrders[] = $this->getLastOrderId();
}
if (count($this->_subOrders) > 1) {
$this->_checkoutSession->addData(array('suborders' => $this->_subOrders));
}
return $this;
}
}