Home Communication board WiKi Contact us

Magento import orders from X-Cart

This script can be used for the X-Cart orders import to Magento. It should be placed in the X-Cart root dir and run in browser.

<?php
error_reporting(E_ALL ^ E_NOTICE);
include "./auth.php";

include $xcart_dir.'/magento/app/Mage.php';
Mage::app('admin');
func_flush('Import orders');
$orders = func_query("select * from $sql_tbl[orders] where 1 order by orderid limit 5000, 500");
if ($orders)
foreach($orders as $index=>$order) {
    $quote = Mage::getModel('sales/quote')->load($order['orderid']);
    if ($quote->getId()) continue;

	$items = $quote->getAllItems();
	$quote->reserveOrderId();

    $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($order['email']);
    $quote->setCustomer($customer);
 
    $storeId = 0;
    $store = $quote->getStore()->load($storeId);
    $quote->setStore($store);
 
    $billAddress = $quote->getBillingAddress();
    $billAddress->setFirstname($order['b_firstname'])
        ->setLastname($order['b_lastname'])
        ->setStreet($order['b_address'])
        ->setCity($order['b_city'])
        ->setPostcode($order['b_zipcode'])
        ->setCountryId($order['b_country'])
        ->setTelephone($order['phone']);
    unset($billingAddress['address_id']);
    $billAddress->addData($billingAddress);
    $billAddress->implodeStreetAddress();
 
    $shipping = $quote->getShippingAddress();
    $shipping->setSameAsBilling(0);
    $shipping->addData($billingAddress);
    $shipping->setFirstname($order['s_firstname'])
        ->setLastname($order['s_lastname'])
        ->setStreet($order['s_address'])
        ->setCity($order['s_city'])
        ->setPostcode($order['s_zipcode'])
        ->setCountryId($order['s_country'])
        ->setTelephone($order['phone']);
    $shipping->implodeStreetAddress();
 
    $products = func_query("select * from $sql_tbl[order_details] where orderid='$order[orderid]'");
    $product_model  = Mage::getModel('catalog/product');

    $shipping = func_query_first_cell("select shipping from $sql_tbl[shipping] where shippingid='$order[shippingid]'");
    $quote->getShippingAddress()
//        ->setNumber($order['tracking'])
        ->setShippingMethod("flatrate_flatrate")
        ->setShippingDescription($shipping);
    $quote->getShippingAddress()->setCollectShippingRates(false);
 
    $quote->save();
    $quoteId = $quote->getId();
 
    $items = $quote->getShippingAddress()->getAllItems();
    $quote->collectTotals();
    $quote->reserveOrderId();
 
    $quotePaymentObj = $quote->getPayment();
    $quotePaymentObj->setMethod('purchaseorder');
    $quote->setPayment($quotePaymentObj);
 
    $convertquote=Mage::getSingleton('sales/convert_quote');
    $orderObj = $convertquote->addressToOrder($quote->getShippingAddress());
    $orderObj->setBillingAddress($convertquote->addressToOrderAddress($quote->getBillingAddress()));
    $orderObj->setShippingAddress($convertquote->addressToOrderAddress($quote->getShippingAddress()));
    $orderObj->setPayment($convertquote->paymentToOrderPayment($quote->getPayment()));
 
    foreach ($products as $pm) {
        $pm['extra_data'] = unserialize($pm['extra_data']);

        $orderItem = Mage::getModel('sales/order_item');
        $orderItem->setName(trim($pm['product'].' '.$pm['product_options']));
        $orderItem->setSku($pm['productcode']);
        $orderItem->setPrice($pm['price']);
        $orderItem->setQtyOrdered($pm['amount']);
        $orderItem->setOriginalPrice($pm['price']);
        $orderItem->setRowTotal($pm['price']*$pm['amount']);
        $orderItem->setSubTotal($pm['price']*$pm['amount']);
        $orderObj->addItem($orderItem);
    }
    $orderObj->setCanShipPartiallyItem(true);
 
    $orderObj->setCustomerNote($order['customer_notes']);
    $orderObj->setSubtotal($order['subtotal']);
    $orderObj->setBaseSubtotal($order['subtotal']);

    $orderObj->setShippingAmount($order['shipping_cost']);
    $orderObj->setBaseShippingAmount($order['shipping_cost']);
    $orderObj->setDiscount($order['discount']);
    $orderObj->setTaxAmount($order['tax']);
    $orderObj->setBaseTaxAmount($order['tax']);
    $orderObj->setGrandTotal($order['total']);
    $orderObj->setBaseGrandTotal($order['total']);
    $orderObj->setTotalPaid($order['total']);
//    $orderObj->setState('complete', true);
    $orderObj->setIncrementId($order['orderid']);
    $orderObj->place();
    $orderObj->setData('created_at', $date);
    $orderObj->setUpdatedAt($date);
//    $orderObj->setStoreId(0);

    $orderObj->save();


    $status = in_array($order['status'], array('Q', 'P', 'C'))?'completed':'cancelled';
    $db = Mage::getSingleton('core/resource')->getConnection('core_write');
    $db->query('update sales_flat_order set created_at=FROM_UNIXTIME('.$order['date'].') , updated_at=FROM_UNIXTIME('.$order['date'].'), status="'.$status.'", state="'.$status.'" where entity_id='.$orderObj->getId());
    $db->query('update sales_flat_order_item set created_at=FROM_UNIXTIME('.$order['date'].') , updated_at=FROM_UNIXTIME('.$order['date'].') where order_id='.$orderObj->getId());

    $quote->setIsActive(false);
    $quote->delete();

    if ($index % 100 == 0) func_flush("<br>\n");
    func_flush('.');
}
func_flush("<br/>\nOrders - ok<br/>\n");

Please note a few things. Firstly you should modify it slightly - because the Magento “Mage.php” should be included in this line, and you have to set the appropriate path:

include $xcart_dir.'/magento/app/Mage.php';

It's required because with this line we are getting the access to the Magento classes.

Also you can note that this script is not create the invoice or shipping. The order status is set directly in the Magento database instead. It's allow to simplify the import process and use all of the Magento orders features (like statistic), because the order is really evaluated as the “completed” or “cancelled”.

PS: You can import x-cart users also.

 
Home About us Privacy statement Terms & Conditions Refund policy © 2007–2019 ArsCommunity