/// <summary> /// Adds an item to a cart in Magento and returns the CartItemResource created /// /// NOTE: /// Throws exception if invalid CartAddItemResource is passed in. CartAddItemResource is invalid /// if the qoute_id or cart item sku are not set. /// </summary> /// <param name="cartId">Cart to add item to</param> /// <param name="item">Item to add to cart</param> /// <returns>Cart Item that was added</returns> public CartItemResource AddItemToCart(int cartId, CartAddItemResource item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (string.IsNullOrEmpty(item.cartItem.quote_id) || string.IsNullOrEmpty(item.cartItem.sku)) { throw new Exception("Ensure that the body of the CartAddItemResource used is properly initialized. qoute_id and cart item sku must both be not empty"); } var endpoint = UrlFormatter.MagentoAddItemToCartUrl(cartId); var client = new RestClient(endpoint); var request = new RestRequest(Method.POST); request.AddHeader("Authorization", string.Format("Bearer {0}", AuthToken)); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(item); var response = client.Execute(request); //Ensure we get the right code CheckStatusCode(response.StatusCode); return(JsonConvert.DeserializeObject <CartItemResource>(response.Content)); }
/// <summary> /// Adds the items from an EA order to a Cart in Magento. /// If an item on the order cannot be found in Magento an exception is thrown. /// </summary> /// <param name="orderId">Order to get items for</param> /// <param name="cartId">Cart to add items to</param> public void AddOrderItemsToCart(string orderId, int cartId) { Guid result; if (!Guid.TryParse(orderId, out result)) { throw new ArgumentException("Argument must be a valid Guid.", nameof(orderId)); } IEnumerable <OrderItemResource> orderItems = _eaOrderController.GetOrderItems(orderId); foreach (var orderItem in orderItems) { CatalogItemResource catalogItem = _eaCatalogsController.GetCatalogItem(orderItem.ProductId); ProductResource product = _magentoProductController.SearchForProducts(ConfigReader.MappingCode, catalogItem.Slug, ConfigReader.MagentoEqualsCondition).items.FirstOrDefault(); if (product == null) { throw new Exception(string.Format("Magento product could not be found for product {0} on order {1} with mapping code {2}", orderItem.SKU, orderId, catalogItem.Slug)); } CartAddItemResource cartAddItem = new CartAddItemResource(cartId, product.sku, orderItem.Quantity); _magentoCartController.AddItemToCart(cartId, cartAddItem); } }