コード例 #1
0
        public ActionResult AddToBasket(AddToBasketButtonAddToBasketViewModel viewModel)
        {
            var cartServiceProvider = new CartServiceProvider();

            var    contactFactory = new ContactFactory();
            string userId         = contactFactory.GetContact();

            var createCartRequest = new CreateOrResumeCartRequest(Context.GetSiteName(), userId);

            var cart = cartServiceProvider.CreateOrResumeCart(createCartRequest).Cart;

            var cartProduct = new CartProduct
            {
                ProductId = viewModel.ProductSku,
                Price     = new Price(Convert.ToDecimal(viewModel.Price), cart.CurrencyCode)
            };

            cartProduct.Properties.Add("VariantSku", viewModel.VariantSku);

            var cartLines = new ReadOnlyCollection <CartLine>(
                new Collection <CartLine> {
                new CartLine
                {
                    Product  = cartProduct,
                    Quantity = (uint)viewModel.Quantity
                }
            }
                );

            var request = new AddCartLinesRequest(cart, cartLines);

            cartServiceProvider.AddCartLines(request);

            return(Json(_miniBasketService.Refresh(), JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public PipelineExecutionResult Execute(IPipelineArgs <AddToBasketRequest, AddToBasketResponse> subject)
        {
            if (subject.Request.Properties.ContainsKey("FromUCommerce"))
            {
                if (!(bool)subject.Request.Properties["FromUCommerce"])
                {
                    return(PipelineExecutionResult.Success);
                }
            }

            //var cart = MappingLibrary.MapPurchaseOrderToCart(subject.Request.PurchaseOrder);

            var    contactFactory = new ContactFactory();
            string userId         = contactFactory.GetContact();

            var cartServiceProvider = new CartServiceProvider();
            var createCartRequest   = new CreateOrResumeCartRequest(Context.GetSiteName(), userId);

            //should it do anything???
            var cart = cartServiceProvider.CreateOrResumeCart(createCartRequest).Cart;

            var cartLine = MappingLibrary.MapOrderLineToCartLine(subject.Response.OrderLine);

            var request = new AddCartLinesRequest(cart, new Collection <CartLine> {
                cartLine
            });

            request.Properties["FromUCommerce"] = true;

            var serviceProvider = new CartServiceProvider();

            serviceProvider.AddCartLines(request);

            return(PipelineExecutionResult.Success);
        }
コード例 #3
0
        public override void Process(ServicePipelineArgs args)
        {
            UpdateCartLinesRequest request;
            CartResult             result;

            CheckParametersAndSetupRequestAndResult(args, out request, out result);

            using (new DisposableThreadLifestyleScope())
            {
                if (IsFromUcommerce(request) || !args.Result.Success)
                {
                    return;
                }

                var basket = _basketService.GetBasketByCartExternalId(request.Cart.ExternalId);

                var existingOrderLines = basket.PurchaseOrder.OrderLines;

                var query = (from updatedOrderLine in request.Lines
                             join originalOrderLine in existingOrderLines
                             on updatedOrderLine.ExternalCartLineId equals originalOrderLine.OrderLineId.ToString() into matchedOrderLines
                             from matchedOrderLine in matchedOrderLines.DefaultIfEmpty()
                             where matchedOrderLine == null
                             select new { NewCartLine = updatedOrderLine }).ToList();

                if (!query.Any())
                {
                    return;
                }

                var newCartLines = query.Select(x => x.NewCartLine).ToList();

                var cartServiceProvider = new CartServiceProvider();
                var addCartLinesRequest = new AddCartLinesRequest(request.Cart, newCartLines);
                cartServiceProvider.AddCartLines(addCartLinesRequest);
            }
        }
コード例 #4
0
        public ActionResult Index()
        {
            var loadCartRequest = new LoadCartRequest("CommerceEngineDefaultStorefront", "Default", "1234");
            var loadCartResult  = _cartServiceProvider.LoadCart(loadCartRequest);

            var cart = loadCartResult.Cart as CommerceCart;

            var lines    = new List <CartLine>();
            var cartLine = new CommerceCartLine("Habitat_Master", "6042567", "56042567", 1.0M);

            lines.Add(cartLine);

            var addLinesRequest = new AddCartLinesRequest(cart, lines);
            var addLinesResult  = _cartServiceProvider.AddCartLines(addLinesRequest);

            // Add a shipping address
            CommerceParty shippingAddress = new CommerceParty();

            shippingAddress.ExternalId    = "Shipping";
            shippingAddress.PartyId       = shippingAddress.ExternalId;
            shippingAddress.Name          = "Shipping";
            shippingAddress.Address1      = "Barbara Strozzilaan 201";
            shippingAddress.Company       = "Sitecore";
            shippingAddress.Country       = "Canada";
            shippingAddress.State         = "ON"; // State is checked by commerce engine: you can configure it in Commerce
            shippingAddress.CountryCode   = "CA"; // Country is checked by commerce engine
            shippingAddress.LastName      = "Werkman";
            shippingAddress.FirstName     = "Erwin";
            shippingAddress.City          = "Amsterdam";
            shippingAddress.ZipPostalCode = "1030AC";

            var cartParties = cart.Parties.ToList();

            cartParties.Add(shippingAddress);
            cart.Parties = cartParties;

            ShippingOptionType shippingOptionType = ShippingOptionType.ShipToAddress;

            ICollection <CommerceShippingInfo> shippingInfoList = new List <CommerceShippingInfo>();

            var commerceShippingInfo = new CommerceShippingInfo();

            commerceShippingInfo.ShippingOptionType = ShippingOptionType.ShipToAddress;
            commerceShippingInfo.PartyID            = shippingAddress.ExternalId;
            commerceShippingInfo.ShippingMethodID   = "B146622D-DC86-48A3-B72A-05EE8FFD187A"; // Ship Items > Ground
            commerceShippingInfo.ShippingMethodName =
                "Ground";                                                                     // Method id and name have to match what is configured in Sitecore Commerce Control Panel

            shippingInfoList.Add(commerceShippingInfo);

            var csShippingInfoList = new List <ShippingInfo>();

            foreach (var shippingInfo in shippingInfoList)
            {
                csShippingInfoList.Add(shippingInfo);
            }

            // Add a shipping address and shipping method
            var addShippingInfoRequest =
                new Sitecore.Commerce.Engine.Connect.Services.Carts.AddShippingInfoRequest(cart, csShippingInfoList,
                                                                                           shippingOptionType);
            var result = _cartServiceProvider.AddShippingInfo(addShippingInfoRequest);

            cart = result.Cart as CommerceCart;

            // Add billing address
            CommerceParty billingAddress = new CommerceParty();

            billingAddress.ExternalId =
                "Billing"; // This should correspond to the PartyId you are setting for the payment info
            billingAddress.PartyId       = billingAddress.ExternalId;
            billingAddress.Name          = "Billing";
            billingAddress.Address1      = "Dorpsstraat 50";
            billingAddress.Company       = "Sitecore";
            billingAddress.Country       = "Canada";
            billingAddress.State         = "ON"; // State is checked: you can configure it in Commerce
            billingAddress.CountryCode   = "CA";
            billingAddress.LastName      = "Werkman";
            billingAddress.FirstName     = "Erwin";
            billingAddress.City          = "Amsterdam";
            billingAddress.ZipPostalCode = "1234AK";

            cart.Parties.Add(billingAddress);

            // Add a payment address and payment method
            var payments = new List <PaymentInfo>();

            var simplePaymentInfo = new SimplePaymentInfo();

            simplePaymentInfo.PaymentMethodID = "9B110CC3-C7C8-4492-8FCF-0CDE5D3E0EB0";
            simplePaymentInfo.Amount          = cart.Total.Amount;

            payments.Add(simplePaymentInfo);

            /*
             * var giftCardPaymentInfo = new GiftCardPaymentInfo();
             * giftCardPaymentInfo.PaymentMethodID = "B5E5464E-C851-4C3C-8086-A4A874DD2DB0"; // GiftCard
             * giftCardPaymentInfo.Amount = cart.Total.Amount;
             * giftCardPaymentInfo.ExternalId = "GC1000000"; // This is the number of the giftcard
             * giftCardPaymentInfo.PartyID = billingAddress.ExternalId;    // For a gift card this is not really necessary and not recorded
             *
             * payments.Add(giftCardPaymentInfo);
             */

            var addPaymentInfoRequest = new AddPaymentInfoRequest(cart, payments);
            var addPaymentInfoResult  = _cartServiceProvider.AddPaymentInfo(addPaymentInfoRequest);


            return(View("Cart", addPaymentInfoResult));
        }