Example #1
0
        /// <summary>
        /// Performs the sync for Endless Aisle orders to Magento.
        ///
        /// For each of the EA orders since the last sync, a cart is made in Magento
        /// that has all of the order products added to it. The cart then has its
        /// shipping information set based on the location data in EA and an order is
        /// created from the cart.
        ///
        /// NOTE:
        ///		If the location data required cannot be found for the shipping and
        ///		billing information, the customer data will be used instead. If the
        ///		customer does not have the required fields set in Magento an error
        ///		will occur.
        /// </summary>
        /// <returns>If the sync was susscessful</returns>
        private static bool OrderSync()
        {
            try
            {
                var lastSync       = GetTimeForSync(Log.OrderSync);
                var ordersToCreate = _orderMapper.GetEaOrdersCreatedAfter(lastSync).ToList();

                if (!ordersToCreate.Any())
                {
                    Console.WriteLine("No orders to update.");
                }
                else
                {
                    foreach (var order in ordersToCreate)
                    {
                        lastSync = order.CreatedDateUtc > lastSync ? order.CreatedDateUtc : lastSync;
                        var cartId = _orderMapper.CreateCustomerCart();
                        _orderMapper.AddOrderItemsToCart(order.Id.ToString(), cartId);
                        _orderMapper.SetShippingAndBillingInformationForCart(cartId, _entityMapper.MagentoRegion, _entityMapper.EaLocation, _customerMapper.MagentoCustomer);
                        var orderCreatedId = _orderMapper.CreateOrderForCart(cartId);
                        Console.WriteLine("Order with ID {0} in Magento has been created from order {1} in Endless Aisle.", orderCreatedId, order.Id);
                    }
                    LogUtility.Write(Log.OrderSync, string.Format("Orders successfully synced. Last order synced was created at {0}", lastSync));
                }

                return(true);
            }
            catch (Exception ex)
            {
                LogException(ex);

                //Uncomment if you want exceptions thrown at runtime.
                //throw;
            }
            return(false);
        }