public static async Task WithListOfCartsWithSingleLineItem(IClient client, int cartsCount, int lineItemQuantity,
                                                                   Func <CartDraft, CartDraft> draftAction, Func <List <Cart>, Task> func)
        {
            var address      = TestingUtility.GetRandomAddress();
            var taxRateDraft = TestingUtility.GetTaxRateDraft(address);

            await WithTaxCategory(client, draft =>
                                  DefaultTaxCategoryDraftWithTaxRate(draft, taxRateDraft),
                                  async taxCategory =>
            {
                await WithProduct(client,
                                  productDraft =>
                                  DefaultProductDraftWithTaxCategory(productDraft, taxCategory),
                                  async product =>
                {
                    var lineItemDraft = new LineItemDraft
                    {
                        Sku      = product.MasterData.Staged.MasterVariant.Sku,
                        Quantity = lineItemQuantity
                    };
                    var cartDraft = DefaultCartDraftWithShippingAddress(new CartDraft(), address);
                    cartDraft     = DefaultCartDraftWithLineItem(cartDraft, lineItemDraft);

                    await WithListAsync(client, cartDraft, draftAction, func, cartsCount);
                });
            });
        }
 public static async Task WithUpdateableShoppingListWithLineItemWithCustomFields(IClient client, long quantity, Func <ShoppingList, Task <ShoppingList> > func)
 {
     await WithType(client, async type =>
     {
         await WithProduct(client, async product =>
         {
             var fields            = CreateNewFields();
             var customFieldsDraft = new CustomFieldsDraft
             {
                 Type   = type.ToKeyResourceIdentifier(),
                 Fields = fields
             };
             var lineItemDraft = new LineItemDraft
             {
                 Quantity  = quantity,
                 ProductId = product.Id,
                 Custom    = customFieldsDraft
             };
             var shoppingListDraft       = new ShoppingListDraft();
             shoppingListDraft.LineItems = new List <LineItemDraft> {
                 lineItemDraft
             };
             await WithUpdateableAsync(client, shoppingListDraft, DefaultShoppingListDraft, func);
         });
     });
 }
Exemplo n.º 3
0
        public LineItemDraft GetLineItemDraftBySku(string sku, int quantity = 1)
        {
            LineItemDraft lineItemDraft = new LineItemDraft();

            lineItemDraft.Sku      = sku;
            lineItemDraft.Quantity = quantity;
            return(lineItemDraft);
        }
        public static CartDraft DefaultCartDraftWithLineItem(CartDraft draft, LineItemDraft lineItemDraft)
        {
            var cartDraft = DefaultCartDraft(draft);

            cartDraft.LineItems = new List <LineItemDraft> {
                lineItemDraft
            };
            return(cartDraft);
        }
        public static ShoppingListDraft DefaultShoppingListDraftWithSingleLineItem(ShoppingListDraft draft, Product product)
        {
            var shoppingListDraft = DefaultShoppingListDraft(draft);
            var lineItemDraft1    = new LineItemDraft {
                ProductId = product.Id, Quantity = 1
            };

            shoppingListDraft.LineItems = new List <LineItemDraft>
            {
                lineItemDraft1
            };
            return(shoppingListDraft);
        }
 public static async Task WithUpdateableShoppingListWithLineItem(IClient client, long quantity, Func <ShoppingList, Task <ShoppingList> > func)
 {
     await WithProduct(client, async product =>
     {
         var lineItemDraft = new LineItemDraft
         {
             Quantity  = quantity,
             ProductId = product.Id
         };
         var shoppingListDraft       = new ShoppingListDraft();
         shoppingListDraft.LineItems = new List <LineItemDraft> {
             lineItemDraft
         };
         await WithUpdateableAsync(client, shoppingListDraft, DefaultShoppingListDraft, func);
     });
 }
 public static async Task WithUpdateableCartWithSingleLineItem(IClient client, int quantity,
                                                               Func <CartDraft, CartDraft> draftAction, Func <Cart, Task <Cart> > func)
 {
     await WithTaxCategory(client, async taxCategory =>
     {
         await WithProduct(client,
                           productDraft =>
                           DefaultProductDraftWithTaxCategory(productDraft, taxCategory),
                           async product =>
         {
             var lineItemDraft = new LineItemDraft
             {
                 Sku      = product.MasterData.Staged.MasterVariant.Sku,
                 Quantity = quantity
             };
             var cartDraft = DefaultCartDraftWithLineItem(new CartDraft(), lineItemDraft);
             await WithUpdateableAsync(client, cartDraft, draftAction, func);
         });
     });
 }
Exemplo n.º 8
0
        /// <summary>
        /// Get ShoppingList Draft
        /// </summary>
        /// <returns></returns>

        public ShoppingListDraft GetShoppingListDraft(bool withCustomer = true, bool withLineItem = false)
        {
            string            name = $"ShoppingList_{TestingUtility.RandomInt()}";
            ShoppingListDraft shoppingListDraft = new ShoppingListDraft
            {
                Key  = TestingUtility.RandomString(10),
                Slug = new LocalizedString()
                {
                    { "en", name }
                },
                Name = new LocalizedString()
                {
                    { "en", name }
                },
                DeleteDaysAfterLastModification = 30
            };

            if (withCustomer)
            {
                Customer customer = this.customerFixture.CreateCustomer();
                this.customerFixture.CustomersToDelete.Add(customer);
                shoppingListDraft.Customer = new ResourceIdentifier <Customer> {
                    Id = customer.Id
                };
            }
            if (withLineItem)
            {
                Product       product       = this.CreateProduct();
                LineItemDraft lineItemDraft =
                    this.GetLineItemDraftBySku(product.MasterData.Current.MasterVariant.Sku, 2);
                shoppingListDraft.LineItems = new List <LineItemDraft>()
                {
                    lineItemDraft
                };
            }

            return(shoppingListDraft);
        }
        /// <summary>
        /// Run Order example code.
        /// </summary>
        /// <param name="client">Client</param>
        /// <param name="project">Project</param>
        public async static Task Run(Client client, Project.Project project)
        {
            string currency = project.Currencies[0];
            string country  = project.Countries[0];
            string language = project.Languages[0];

            /*  CREATE ORDER
             *  ===================================================================================
             *  An order is created from an existing cart.  To create an order from a cart, the
             *  cart cannot be empty and it must have a shipping address set.
             */

            // Retrieve 2 products to add to a cart.
            Response <ProductProjectionQueryResult> productProjectionQueryResponse = await client.ProductProjections().QueryProductProjectionsAsync(limit: 2);

            List <ProductProjection> products       = new List <ProductProjection>();
            List <LineItemDraft>     lineItemDrafts = new List <LineItemDraft>();

            if (productProjectionQueryResponse.Success)
            {
                ProductProjectionQueryResult productProjectionQueryResult = productProjectionQueryResponse.Result;
                Console.WriteLine("Retrieved {0} products.", productProjectionQueryResult.Results.Count);

                foreach (ProductProjection product in productProjectionQueryResult.Results)
                {
                    LineItemDraft lineItemDraft = new LineItemDraft(product.Id, product.MasterVariant.Id)
                    {
                        Quantity = 1
                    };

                    lineItemDrafts.Add(lineItemDraft);
                }
            }
            else
            {
                Helper.WriteError <ProductProjectionQueryResult>(productProjectionQueryResponse);
            }

            // Create a shipping address.
            Address shippingAddress = new Address();

            shippingAddress.FirstName    = "John";
            shippingAddress.LastName     = "Doe";
            shippingAddress.StreetNumber = "123";
            shippingAddress.StreetName   = "Main St.";
            shippingAddress.PostalCode   = "11111";
            shippingAddress.City         = "City";
            shippingAddress.Country      = country;

            // Create the cart.
            Cart cart = null;

            if (lineItemDrafts.Count > 0)
            {
                CartDraft cartDraft = new CartDraft(currency)
                {
                    ShippingAddress = shippingAddress,
                    LineItems       = lineItemDrafts
                };

                Response <Cart> cartResponse = await client.Carts().CreateCartAsync(cartDraft);

                if (cartResponse.Success)
                {
                    cart = cartResponse.Result;
                    Console.WriteLine("Created new cart with ID {0}.", cart.Id);
                }
                else
                {
                    Helper.WriteError <Cart>(cartResponse);
                }
            }

            // Create the order from the cart.
            Response <Order> orderResponse = null;
            Order            order         = null;

            if (cart != null)
            {
                OrderFromCartDraft orderFromCartDraft = new OrderFromCartDraft(cart);
                orderResponse = await client.Orders().CreateOrderFromCartAsync(orderFromCartDraft);

                if (orderResponse.Success)
                {
                    order = orderResponse.Result;
                    Console.WriteLine("Created new order with ID {0}.", order.Id);

                    foreach (LineItem lineItem in order.LineItems)
                    {
                        Console.WriteLine("Order has a line iten named '{0}' with an ID of {1}.", lineItem.Name[language], lineItem.Id);
                    }
                }
                else
                {
                    Helper.WriteError <Order>(orderResponse);
                }
            }

            /*  GET ORDERS
             *  ===================================================================================
             */

            // Get 3 most recent orders.
            Response <OrderQueryResult> orderQueryResponse = await client.Orders().QueryOrdersAsync(sort: "createdAt desc", limit: 3);

            List <Order> orders = new List <Order>();

            if (orderQueryResponse.Success)
            {
                OrderQueryResult orderQueryResult = orderQueryResponse.Result;
                orders = orderQueryResult.Results;

                Console.WriteLine("Retrieved {0} orders.", orders.Count);
            }
            else
            {
                Helper.WriteError <OrderQueryResult>(orderQueryResponse);
            }

            // Get a single order by its ID.
            if (orders.Count > 0)
            {
                string orderId = orders[0].Id;
                orderResponse = await client.Orders().GetOrderByIdAsync(orderId);

                if (orderResponse.Success)
                {
                    order = orderResponse.Result;
                    Console.WriteLine("Retrieved order with ID {0}.", order.Id);
                }
                else
                {
                    Helper.WriteError <Order>(orderResponse);
                }
            }

            /*  UPDATE AN ORDER
             *  ===================================================================================
             *  Each change is made using its own update action object which maps to an update
             *  action call in the API. The list of update action objects are sent to the API using
             *  a single request. If there is an update action in the API that has not yet been
             *  implemented in the SDK, you can use the GenericAction class to make any request you
             *  want (as long as it is a valid update action supported by the API).
             */
            if (order != null)
            {
                ChangeOrderStateAction changeOrderStateAction = new ChangeOrderStateAction(OrderState.Confirmed);

                // Here is how you would make the setDescription request using a GenericAction object.
                GenericAction setOrderNumberAction = new GenericAction("setOrderNumber");
                setOrderNumberAction["orderNumber"] = "ABC123";

                List <UpdateAction> actions = new List <UpdateAction>()
                {
                    changeOrderStateAction, setOrderNumberAction
                };

                orderResponse = await client.Orders().UpdateOrderAsync(order, actions);

                if (orderResponse.Success)
                {
                    order = orderResponse.Result;
                    Console.WriteLine("Updated order with ID {0}.", order.Id);
                    Console.WriteLine("Order state: {0}", order.OrderState);
                    Console.WriteLine("Order number: {0}", order.OrderNumber);
                }
                else
                {
                    Helper.WriteError <Order>(orderResponse);
                }
            }

            /*  DELETE AN ORDER
             *  ===================================================================================
             *  Delete API requests return a generic response, but some return the object that was
             *  deleted. The Orders delete request returns a generic response.
             */
            if (order != null)
            {
                Response <JObject> deleteResponse = await client.Orders().DeleteOrderAsync(order);

                if (deleteResponse.Success)
                {
                    Console.WriteLine("Deleted order with ID {0}.", order.Id);
                }
                else
                {
                    Helper.WriteError <JObject>(deleteResponse);
                }
            }
        }