private void UpdateCheckoutAttribute(PostBackModel model)
        {
            var cart = _workContext.CurrentCustomer.ShoppingCartItems
                       .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                       .LimitPerStore(_storeContext.CurrentStore.Id)
                       .ToList();

            var collectPoint = string.Format(@"{0}, {1} {2}, {3} {4}, {5}",
                                             model.CustomerPostalLocation, model.CustomerStreet, model.CustomerStreetNumber,
                                             model.CustomerPostalCode, model.CustomerCity, model.CustomerCountry);

            var checkoutAttributes = _checkoutAttributeService.GetAllCheckoutAttributes(_storeContext.CurrentStore.Id, !cart.RequiresShipping());
            //var attributesXml = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.CheckoutAttributes, _storeContext.CurrentStore.Id);
            var attributesXml = string.Empty;

            foreach (var attribute in checkoutAttributes)
            {
                if (attribute.Name == "CollectPoint")
                {
                    attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, collectPoint);
                }

                if (attribute.Name == "OrderReference")
                {
                    attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, model.OrderReference);
                }
            }

            _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.CheckoutAttributes, attributesXml, _storeContext.CurrentStore.Id);

            _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, CustomCustomerAttributeNames.DeliveryMethod, model.DeliveryMethod, _storeContext.CurrentStore.Id);
            _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, CustomCustomerAttributeNames.DeliveryMethodRate, model.DeliveryMethodPriceTotalEuro, _storeContext.CurrentStore.Id);
            _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, CustomCustomerAttributeNames.DeliveryMethodAddress, collectPoint, _storeContext.CurrentStore.Id);
        }
        public async Task Can_add_and_parse_checkoutAttributes()
        {
            string attributes = "";

            //color: green
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca1, cav1_1.Id.ToString());
            //custom option: option 1, option 2
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_1.Id.ToString());
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_2.Id.ToString());
            //custom text
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca3, "absolutely any value");

            var parsed_attributeValues = await _checkoutAttributeParser.ParseCheckoutAttributeValues(attributes);

            Assert.IsTrue(parsed_attributeValues.Contains(cav1_1));
            Assert.IsFalse(parsed_attributeValues.Contains(cav1_2)); //not inserted
            Assert.IsTrue(parsed_attributeValues.Contains(cav2_1));
            Assert.IsTrue(parsed_attributeValues.Contains(cav2_2));
            Assert.IsTrue(parsed_attributeValues.Contains(cav2_2));

            var parsedValues = _checkoutAttributeParser.ParseValues(attributes, ca3.Id);

            Assert.AreEqual(1, parsedValues.Count);
            Assert.IsTrue(parsedValues.Contains("absolutely any value"));
            Assert.IsFalse(parsedValues.Contains("it shouldn't be valid in any case"));
        }
        public void Can_add_and_parse_checkoutAttributes()
        {
            string attributes = "";

            //color: green
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca1, cav1_1.Id.ToString());
            //custom option: option 1, option 2
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_1.Id.ToString());
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_2.Id.ToString());
            //custom text
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca3, "Some custom text goes here");

            var parsed_pvaValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(attributes);

            parsed_pvaValues.Contains(cav1_1).ShouldEqual(true);
            parsed_pvaValues.Contains(cav1_2).ShouldEqual(false);
            parsed_pvaValues.Contains(cav2_1).ShouldEqual(true);
            parsed_pvaValues.Contains(cav2_2).ShouldEqual(true);
            parsed_pvaValues.Contains(cav2_2).ShouldEqual(true);

            var parsedValues = _checkoutAttributeParser.ParseValues(attributes, ca3.Id);

            parsedValues.Count.ShouldEqual(1);
            parsedValues.Contains("Some custom text goes here").ShouldEqual(true);
            parsedValues.Contains("Some other custom text").ShouldEqual(false);
        }
        protected virtual void SaveConditionAttributes(CheckoutAttribute checkoutAttribute, CheckoutAttributeModel model)
        {
            string attributesXml = null;

            if (model.ConditionModel.EnableCondition)
            {
                var attribute = _checkoutAttributeService.GetCheckoutAttributeById(model.ConditionModel.SelectedAttributeId);
                if (attribute != null)
                {
                    switch (attribute.AttributeControlType)
                    {
                        case AttributeControlType.DropdownList:
                        case AttributeControlType.RadioList:
                        case AttributeControlType.ColorSquares:
                        case AttributeControlType.ImageSquares:
                            {
                                var selectedAttribute = model.ConditionModel.ConditionAttributes
                                    .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                                var selectedValue = selectedAttribute?.SelectedValueId;

                                //for conditions we should empty values save even when nothing is selected
                                //otherwise "attributesXml" will be empty
                                //hence we won't be able to find a selected attribute
                                attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(null, attribute, string.IsNullOrEmpty(selectedValue) ? string.Empty : selectedValue);
                            }
                            break;
                        case AttributeControlType.Checkboxes:
                            {
                                var selectedAttribute = model.ConditionModel.ConditionAttributes
                                    .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                                var selectedValues = selectedAttribute?.Values
                                    .Where(x => x.Selected)
                                    .Select(x => x.Value)
                                    .ToList();

                                if (selectedValues?.Any() ?? false)
                                    foreach (var value in selectedValues)
                                        attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, value);
                                else
                                    attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(null, attribute, string.Empty);
                            }
                            break;
                        case AttributeControlType.ReadonlyCheckboxes:
                        case AttributeControlType.TextBox:
                        case AttributeControlType.MultilineTextbox:
                        case AttributeControlType.Datepicker:
                        case AttributeControlType.FileUpload:
                        default:
                            //these attribute types are not supported as conditions
                            break;
                    }
                }
            }

            checkoutAttribute.ConditionAttributeXml = attributesXml;
        }
示例#5
0
        public void Can_add_checkoutAttributes()
        {
            IList <CustomAttribute> attributes = new List <CustomAttribute>();

            //color: green
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca1, cav1_1.Id.ToString()).ToList();
            //custom option: option 1, option 2
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_1.Id.ToString()).ToList();
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_2.Id.ToString()).ToList();
            //custom text
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca3, "absolutely any value").ToList();

            Assert.IsTrue(attributes.Count == 4);
            Assert.IsTrue(attributes.Any(c => c.Key.Equals(ca1.Id)));
            Assert.IsTrue(attributes.Any(c => c.Key.Equals(ca2.Id)));
            Assert.IsTrue(attributes.Any(c => c.Key.Equals(ca3.Id)));
            attributes = _checkoutAttributeParser.RemoveCheckoutAttribute(attributes, ca1);
            Assert.IsTrue(attributes.Count == 3);
            Assert.IsFalse(attributes.Any(c => c.Key.Equals(ca1.Id)));
        }
        protected virtual void SaveConditionAttributes(CheckoutAttribute checkoutAttribute, CheckoutAttributeModel model)
        {
            string attributesXml = null;

            if (model.ConditionModel.EnableCondition)
            {
                var attribute = _checkoutAttributeService.GetCheckoutAttributeById(model.ConditionModel.SelectedAttributeId);
                if (attribute != null)
                {
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlType.DropdownList:
                    case AttributeControlType.RadioList:
                    case AttributeControlType.ColorSquares:
                    case AttributeControlType.ImageSquares:
                    {
                        var selectedAttribute = model.ConditionModel.ConditionAttributes
                                                .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                        var selectedValue = selectedAttribute != null ? selectedAttribute.SelectedValueId : null;
                        if (!String.IsNullOrEmpty(selectedValue))
                        {
                            attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, selectedValue);
                        }
                        else
                        {
                            attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, string.Empty);
                        }
                    }
                    break;

                    case AttributeControlType.Checkboxes:
                    {
                        var selectedAttribute = model.ConditionModel.ConditionAttributes
                                                .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                        var selectedValues = selectedAttribute != null?selectedAttribute.Values.Where(x => x.Selected).Select(x => x.Value) : null;

                        if (selectedValues.Any())
                        {
                            foreach (var value in selectedValues)
                            {
                                attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, value);
                            }
                        }
                        else
                        {
                            attributesXml = _checkoutAttributeParser.AddCheckoutAttribute(attributesXml, attribute, string.Empty);
                        }
                    }
                    break;

                    case AttributeControlType.ReadonlyCheckboxes:
                    case AttributeControlType.TextBox:
                    case AttributeControlType.MultilineTextbox:
                    case AttributeControlType.Datepicker:
                    case AttributeControlType.FileUpload:
                    default:
                        //these attribute types are not supported as conditions
                        break;
                    }
                }
            }
            checkoutAttribute.ConditionAttributeXml = attributesXml;
        }
        private void SyncCartWithKlarnaOrder(Customer customer, KlarnaCheckoutOrder klarnaCheckoutOrder)
        {
            var currentStoreId = _storeContext.CurrentStore.Id;

            var orderCurrency = _currencyService.GetCurrencyByCode(klarnaCheckoutOrder.PurchaseCurrency);

            if (orderCurrency != null)
            {
                _workContext.WorkingCurrency = orderCurrency;
            }

            ClearItemsInCart(customer, currentStoreId);
            ClearDiscountsAndShippingSelection(customer, currentStoreId);

            var physicalItems       = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypePhysical);
            var appliedCoupons      = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypeDiscount && x.HasOrderLevelDiscountCouponCode());
            var appliedGiftCards    = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypeDiscount && x.IsDiscountGiftCardCartItem());
            var appliedRewardPoints = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypeDiscount && x.IsRewardPointsCartItem());
            var checkoutAttributes  = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypeDiscount && x.IsCheckoutAttribtue());
            var shippingItems       = klarnaCheckoutOrder.Cart.Items.Where(x => x.Type == CartItem.TypeShippingFee);

            foreach (var physicalItem in physicalItems)
            {
                AddPhysicalItemToCart(customer, physicalItem, currentStoreId);
                var couponCodes = physicalItem.GetCouponCodesFromPhysicalCartItem();
                foreach (var couponCode in couponCodes)
                {
                    UseCouponCode(customer, couponCode);
                }
            }

            foreach (var coupon in appliedCoupons)
            {
                var couponCode = coupon.GetCouponCodeFromDiscountCouponCartItem();
                UseCouponCode(customer, couponCode);
            }

            foreach (var giftCardCartItem in appliedGiftCards)
            {
                var giftCardId = giftCardCartItem.GetGiftCardIdFromDiscountGiftCardCartItem();
                var giftCard   = _giftCardService.GetGiftCardById(giftCardId);
                customer.ApplyGiftCardCouponCode(giftCard.GiftCardCouponCode);
            }

            if (appliedRewardPoints.Any())
            {
                _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, true, currentStoreId);
            }

            var checkoutAttributesXml = GetSelectedCheckoutAttributesThatHasNotBeenSentToKlarna(customer, currentStoreId);

            foreach (var item in checkoutAttributes)
            {
                var ca = _checkoutAttributeService.GetCheckoutAttributeById(item.GetCheckoutAttributeId());
                checkoutAttributesXml = _checkoutAttributeParser.AddCheckoutAttribute(checkoutAttributesXml, ca, item.GetCheckoutAttributeValue());
            }
            _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CheckoutAttributes, checkoutAttributesXml, currentStoreId);

            foreach (var shippingItem in shippingItems)
            {
                AddShippingItemToCart(customer, shippingItem, currentStoreId);
                var couponCodes = shippingItem.GetCouponCodesFromShippingItem();
                foreach (var couponCode in couponCodes)
                {
                    UseCouponCode(customer, couponCode);
                }
            }
        }
示例#8
0
        public void CanAddAndParseCheckoutAttributes()
        {
            var attributes = string.Empty;

            //color: green
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, _ca1, _cav11.Id.ToString());
            //custom option: option 1, option 2
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, _ca2, _cav21.Id.ToString());
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, _ca2, _cav22.Id.ToString());
            //custom text
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, _ca3, "Some custom text goes here");

            var parsedAttributeValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(attributes).ToList();

            parsedAttributeValues.SelectMany(x => x.values.Select(p => p.Id)).Contains(_cav11.Id).Should().BeTrue();
            parsedAttributeValues.SelectMany(x => x.values.Select(p => p.Id)).Contains(_cav12.Id).Should().BeFalse();
            parsedAttributeValues.SelectMany(x => x.values.Select(p => p.Id)).Contains(_cav21.Id).Should().BeTrue();
            parsedAttributeValues.SelectMany(x => x.values.Select(p => p.Id)).Contains(_cav22.Id).Should().BeTrue();
            parsedAttributeValues.SelectMany(x => x.values.Select(p => p.Id)).Contains(_cav22.Id).Should().BeTrue();

            var parsedValues = _checkoutAttributeParser.ParseValues(attributes, _ca3.Id);

            parsedValues.Count.Should().Be(1);
            parsedValues.Contains("Some custom text goes here").Should().BeTrue();
            parsedValues.Contains("Some other custom text").Should().BeFalse();
        }
示例#9
0
        public void Can_add_and_parse_checkoutAttributes()
        {
            var attributes = "";

            //color: green
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca1, cav1_1.Id.ToString());
            //custom option: option 1, option 2
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_1.Id.ToString());
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca2, cav2_2.Id.ToString());
            //custom text
            attributes = _checkoutAttributeParser.AddCheckoutAttribute(attributes, ca3, "Some custom text goes here");

            RunWithTestServiceProvider(() =>
            {
                var parsedAttributeValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(attributes).ToList();
                parsedAttributeValues.SelectMany(x => x.values).Contains(cav1_1).Should().BeTrue();
                parsedAttributeValues.SelectMany(x => x.values).Contains(cav1_2).Should().BeFalse();
                parsedAttributeValues.SelectMany(x => x.values).Contains(cav2_1).Should().BeTrue();
                parsedAttributeValues.SelectMany(x => x.values).Contains(cav2_2).Should().BeTrue();
                parsedAttributeValues.SelectMany(x => x.values).Contains(cav2_2).Should().BeTrue();

                var parsedValues = _checkoutAttributeParser.ParseValues(attributes, ca3.Id);
                parsedValues.Count.Should().Be(1);
                parsedValues.Contains("Some custom text goes here").Should().BeTrue();
                parsedValues.Contains("Some other custom text").Should().BeFalse();
            });
        }
        public async Task <IList <CustomAttribute> > Handle(SaveCheckoutAttributesCommand request, CancellationToken cancellationToken)
        {
            if (request.Cart == null)
            {
                throw new ArgumentNullException("cart");
            }

            if (request.Form == null)
            {
                throw new ArgumentNullException("form");
            }

            var customAttributes   = new List <CustomAttribute>();
            var checkoutAttributes = await _checkoutAttributeService.GetAllCheckoutAttributes(request.Store.Id, !request.Cart.RequiresShipping());

            foreach (var attribute in checkoutAttributes)
            {
                string controlId = string.Format("checkout_attribute_{0}", attribute.Id);
                switch (attribute.AttributeControlType)
                {
                case AttributeControlType.DropdownList:
                case AttributeControlType.RadioList:
                case AttributeControlType.ColorSquares:
                case AttributeControlType.ImageSquares:
                {
                    var ctrlAttributes = request.Form[controlId];
                    if (!string.IsNullOrEmpty(ctrlAttributes))
                    {
                        customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
                                                                                         attribute, ctrlAttributes).ToList();
                    }
                }
                break;

                case AttributeControlType.Checkboxes:
                {
                    var cblAttributes = request.Form[controlId].ToString();
                    if (!string.IsNullOrEmpty(cblAttributes))
                    {
                        foreach (var item in cblAttributes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, item).ToList();
                        }
                    }
                }
                break;

                case AttributeControlType.ReadonlyCheckboxes:
                {
                    //load read-only (already server-side selected) values
                    var attributeValues = attribute.CheckoutAttributeValues;
                    foreach (var selectedAttributeId in attributeValues
                             .Where(v => v.IsPreSelected)
                             .Select(v => v.Id)
                             .ToList())
                    {
                        customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
                                                                                         attribute, selectedAttributeId.ToString()).ToList();
                    }
                }
                break;

                case AttributeControlType.TextBox:
                case AttributeControlType.MultilineTextbox:
                {
                    var ctrlAttributes = request.Form[controlId].ToString();
                    if (!string.IsNullOrEmpty(ctrlAttributes))
                    {
                        string enteredText = ctrlAttributes.Trim();
                        customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
                                                                                         attribute, enteredText).ToList();
                    }
                }
                break;

                case AttributeControlType.Datepicker:
                {
                    var      date         = request.Form[controlId + "_day"];
                    var      month        = request.Form[controlId + "_month"];
                    var      year         = request.Form[controlId + "_year"];
                    DateTime?selectedDate = null;
                    try
                    {
                        selectedDate = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(date));
                    }
                    catch { }
                    if (selectedDate.HasValue)
                    {
                        customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
                                                                                         attribute, selectedDate.Value.ToString("D")).ToList();
                    }
                }
                break;

                case AttributeControlType.FileUpload:
                {
                    Guid downloadGuid;
                    Guid.TryParse(request.Form[controlId], out downloadGuid);
                    var download = await _downloadService.GetDownloadByGuid(downloadGuid);

                    if (download != null)
                    {
                        customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
                                                                                         attribute, download.DownloadGuid.ToString()).ToList();
                    }
                }
                break;

                default:
                    break;
                }
            }

            //save checkout attributes
            //validate conditional attributes (if specified)
            foreach (var attribute in checkoutAttributes)
            {
                var conditionMet = await _checkoutAttributeParser.IsConditionMet(attribute, customAttributes);

                if (conditionMet.HasValue && !conditionMet.Value)
                {
                    customAttributes = _checkoutAttributeParser.RemoveCheckoutAttribute(customAttributes, attribute).ToList();
                }
            }
            await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.CheckoutAttributes, customAttributes, request.Store.Id);

            return(customAttributes);
        }