コード例 #1
0
        protected void Page_Init(object sender, EventArgs e)
        {
            _ProductId = AlwaysConvert.ToInt(Request.QueryString["ProductId"]);
            string optionList = EncryptionHelper.DecryptAES(Request.QueryString["Options"]);

            _OptionList = ProductVariantManager.ValidateOptionList(optionList);
            _Product    = ProductDataSource.Load(_ProductId);
            string productName = _Product.Name;

            _VariantManager = new ProductVariantManager(_ProductId);
            ProductVariant v = _VariantManager.GetVariantFromOptions(_OptionList);

            if (v != null)
            {
                productName = _Product.Name + " (" + v.VariantName + ")";
            }
            else
            {
                _OptionList = string.Empty;
            }
            Caption.Text             = string.Format(Caption.Text, productName);
            CancelButton.NavigateUrl = "DigitalGoods.aspx?ProductId=" + _ProductId.ToString();
            if (!Page.IsPostBack)
            {
                SearchResultsGrid.Visible = false;
            }
        }
コード例 #2
0
        protected void VariantGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string optionList;

            if (e.CommandName.Equals("AddToBasket"))
            {
                optionList = e.CommandArgument.ToString();
                ProductVariant variant = _VariantManager.GetVariantFromOptions(optionList);
                if (variant != null)
                {
                    BasketItem basketItem = GetBasketItem(optionList, e);
                    if (basketItem != null)
                    {
                        // DETERMINE IF THE LICENSE AGREEMENT MUST BE REQUESTED
                        IList <LicenseAgreement> basketItemLicenseAgreements = new List <LicenseAgreement>();
                        basketItemLicenseAgreements.BuildCollection(basketItem, LicenseAgreementMode.OnAddToBasket);
                        if ((basketItemLicenseAgreements.Count > 0))
                        {
                            // THESE AGREEMENTS MUST BE ACCEPTED TO ADD TO CART
                            List <BasketItem> basketItems = new List <BasketItem>();
                            basketItems.Add(basketItem);
                            string guidKey = Guid.NewGuid().ToString("N");
                            Cache.Add(guidKey, basketItems, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.NotRemovable, null);
                            string acceptUrl  = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("~/Basket.aspx"));
                            string declineUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Page.ResolveUrl(_Product.NavigateUrl)));
                            Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
                        }

                        //ADD ITEM TO BASKET
                        Basket basket = AbleContext.Current.User.Basket;
                        basket.Items.Add(basketItem);
                        basket.Save();

                        //Determine if there are associated Upsell products
                        if (basketItem.Product.GetUpsellProducts(basket).Count > 0)
                        {
                            //redirect to upsell page
                            string returnUrl = AbleCommerce.Code.NavigationHelper.GetEncodedReturnUrl();
                            Response.Redirect("~/ProductAccessories.aspx?ProductId=" + basketItem.Product.Id + "&ReturnUrl=" + returnUrl);
                        }

                        // IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
                        IBasketService     service  = AbleContext.Resolve <IBasketService>();
                        ValidationResponse response = service.Validate(basket);
                        if (!response.Success)
                        {
                            Session["BasketMessage"] = response.WarningMessages;
                            Response.Redirect(AbleCommerce.Code.NavigationHelper.GetBasketUrl());
                        }

                        //IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
                        if (!AbleCommerce.Code.PageHelper.HasBasketControl(this.Page))
                        {
                            Response.Redirect(AbleCommerce.Code.NavigationHelper.GetBasketUrl());
                        }
                    }
                }
            }
            if (e.CommandName.Equals("AddToWishlist"))
            {
                optionList = e.CommandArgument.ToString();
                ProductVariant variant = _VariantManager.GetVariantFromOptions(optionList);
                if (variant != null)
                {
                    BasketItem wishlistItem = GetBasketItem(optionList, e);
                    if (wishlistItem != null)
                    {
                        Wishlist wishlist = AbleContext.Current.User.PrimaryWishlist;
                        wishlist.WishlistItems.Add(wishlistItem);
                        wishlist.Save();
                        Response.Redirect("~/Members/MyWishlist.aspx");
                    }
                }
            }

            else if (e.CommandName.Equals("NotifyMe"))
            {
                HtmlTableRow row = (HtmlTableRow)((Control)e.CommandSource).Parent.Parent;
                optionList = e.CommandArgument.ToString();
                string         email             = GetInputControlValue(row, "EmailText");
                Label          subscribedMessage = (Label)PageHelper.RecursiveFindControl(row, "messageLabel");
                LinkButton     notifyLink        = (LinkButton)PageHelper.RecursiveFindControl(row, "NotificationLink");
                ProductVariant variant           = _VariantManager.GetVariantFromOptions(optionList);

                if (RestockNotifyDataSource.LoadVariantForEmail(variant.Id, email) == null)
                {
                    RestockNotify notification = new RestockNotify(_Product, variant, email);
                    notification.Save();

                    subscribedMessage.Text    = string.Format(subscribedMessage.Text, email);
                    subscribedMessage.Visible = true;
                    notifyLink.Visible        = false;
                }
                else if (!string.IsNullOrEmpty(email))
                {
                    subscribedMessage.Text    = string.Format(subscribedMessage.Text, email);
                    subscribedMessage.Visible = true;
                    notifyLink.Visible        = false;
                }
            }
        }
コード例 #3
0
        private static IList <Variant> GetAvailableVariants(Product product)
        {
            IList <Variant> variants = new List <Variant>();
            List <Option>   relatedOptions = new List <Option>();
            int             lastIndex = -1, colorOptionIndex = -1, sizeOptionIndex = -1, materialOptionIndex = -1, patternOptionIndex = -1;
            Option          colorOption = null, sizeOption = null, materialOption = null, patternOption = null;
            string          optionListPattern = string.Empty;

            if (product.ProductOptions.Count > 0)
            {
                // check variants and options
                IList <ProductOption> options = product.ProductOptions;
                foreach (ProductOption pOption in options)
                {
                    if (!string.IsNullOrEmpty(optionListPattern))
                    {
                        optionListPattern += ",";
                    }
                    string optionName = pOption.Option.Name.ToLowerInvariant();

                    if (optionName == "color")
                    {
                        colorOption = pOption.Option; colorOptionIndex = ++lastIndex; relatedOptions.Add(colorOption); optionListPattern += "{C}";
                    }
                    else if (optionName == "size")
                    {
                        sizeOption = pOption.Option; sizeOptionIndex = ++lastIndex; relatedOptions.Add(sizeOption); optionListPattern += "{S}";
                    }
                    else if (optionName == "material")
                    {
                        materialOption = pOption.Option; materialOptionIndex = ++lastIndex; relatedOptions.Add(materialOption); optionListPattern += "{M}";
                    }
                    else if (optionName == "pattern")
                    {
                        patternOption = pOption.Option; patternOptionIndex = ++lastIndex; relatedOptions.Add(patternOption); optionListPattern += "{P}";
                    }
                    else
                    {
                        optionListPattern += "0";  // fill the options list pattern with zero
                    }
                }
            }

            if (relatedOptions.Count > 0)
            {
                Store  store    = AbleContext.Current.Store;
                string storeUrl = store.StoreUrl;
                if (!storeUrl.EndsWith("/"))
                {
                    storeUrl += "/";
                }

                // calculate all valid combinations using the option choices
                List <int[]>          choiceCombinations = GetAllChoicesCombinations(relatedOptions);
                ProductVariantManager variantManager     = new ProductVariantManager(product.Id);
                IList <string>        imageUrlList       = new List <string>(); // require to keep track of unique images for variants

                foreach (int[] choicesCombination in choiceCombinations)
                {
                    // collect the choice id values
                    int colorChoiceId = 0, sizeChoiceId = 0, materialChoiceId = 0, patternChoiceId = 0;
                    if (colorOptionIndex >= 0)
                    {
                        colorChoiceId = choicesCombination[colorOptionIndex];
                    }
                    if (sizeOptionIndex >= 0)
                    {
                        sizeChoiceId = choicesCombination[sizeOptionIndex];
                    }
                    if (materialOptionIndex >= 0)
                    {
                        materialChoiceId = choicesCombination[materialOptionIndex];
                    }
                    if (patternOptionIndex >= 0)
                    {
                        patternChoiceId = choicesCombination[patternOptionIndex];
                    }

                    // build option list, fill the color, size, material, and pattern choice id values while for rest of options fill in zero value
                    string optionList = optionListPattern;
                    optionList = optionList.Replace("{C}", colorChoiceId.ToString());
                    optionList = optionList.Replace("{S}", sizeChoiceId.ToString());
                    optionList = optionList.Replace("{M}", materialChoiceId.ToString());
                    optionList = optionList.Replace("{P}", patternChoiceId.ToString());

                    ProductVariant productVariant = variantManager.GetVariantFromOptions(optionList);
                    if (productVariant != null)
                    {
                        // check availability
                        if (!productVariant.Available)
                        {
                            continue;
                        }

                        Variant variant = new Variant();
                        variant.Product = product;

                        string variantNamePart    = string.Empty;
                        string variantoptionsList = string.Empty;

                        if (colorChoiceId > 0)
                        {
                            variant.Color       = EntityLoader.Load <OptionChoice>(colorChoiceId).Name;
                            variantNamePart    += string.IsNullOrEmpty(variantNamePart) ? variant.Color : "," + variant.Color;
                            variantoptionsList += string.IsNullOrEmpty(variantoptionsList) ? colorChoiceId.ToString() : "," + colorChoiceId.ToString();
                        }
                        if (sizeChoiceId > 0)
                        {
                            variant.Size        = EntityLoader.Load <OptionChoice>(sizeChoiceId).Name;
                            variantNamePart    += string.IsNullOrEmpty(variantNamePart) ? variant.Size : "," + variant.Size;
                            variantoptionsList += string.IsNullOrEmpty(variantoptionsList) ? sizeChoiceId.ToString() : "," + sizeChoiceId.ToString();
                        }
                        if (materialChoiceId > 0)
                        {
                            variant.Material    = EntityLoader.Load <OptionChoice>(materialChoiceId).Name;
                            variantNamePart    += string.IsNullOrEmpty(variantNamePart) ? variant.Material : "," + variant.Material;
                            variantoptionsList += string.IsNullOrEmpty(variantoptionsList) ? materialChoiceId.ToString() : "," + materialChoiceId.ToString();
                        }
                        if (patternChoiceId > 0)
                        {
                            variant.Pattern     = EntityLoader.Load <OptionChoice>(patternChoiceId).Name;
                            variantNamePart    += string.IsNullOrEmpty(variantNamePart) ? variant.Pattern : "," + variant.Pattern;
                            variantoptionsList += string.IsNullOrEmpty(variantoptionsList) ? patternChoiceId.ToString() : "," + patternChoiceId.ToString();
                        }

                        variant.Name = string.Format("{0} - {1}", product.Name, variantNamePart);
                        variant.Link = ResolveUrl(product.NavigateUrl, storeUrl) + string.Format("?Options={0}", variantoptionsList);

                        // WE CAN ONLY USE ALPHANUMERIC CHARACTERS FOR ID, CREATE ID VALUE USING THE APAREL OPTIONS
                        // [Product_Id]C[COLOR_CHOICE_ID]S[SIZE_CHOICE_ID]M[MATERIAL_CHOICE_ID]P[PATTERN_CHOICE_ID]
                        variant.Id = string.Format("{0}C{1}S{2}M{3}P{4}",
                                                   product.Id,
                                                   (colorOptionIndex > -1 ? colorChoiceId.ToString() : string.Empty),
                                                   (sizeOptionIndex > -1 ? sizeChoiceId.ToString() : string.Empty),
                                                   (materialOptionIndex > -1 ? materialChoiceId.ToString() : string.Empty),
                                                   (patternOptionIndex > -1 ? patternChoiceId.ToString() : string.Empty));

                        // VERIFY UNIQUE IMAGE LINK CONSIDERING FOLLOWING GOOGLE FEED REQUIREMENTS
                        // For products that fall under “Apparel & Accessories” and all corresponding sub-categories in feeds targeting the US, UK, DE, FR, and JP:
                        // If you are submitting product variants that differ in ‘‘color’, or ‘pattern’, or ‘material’,
                        // we require you to submit specific images corresponding to each of these variants.
                        // If you do not have the correct image for the variation of the product, you may not submit that variant.
                        // We recommend specific images for ‘size’ variants too. However, if these are not available you may submit the same image URL for items that differ in ‘size’.
                        string imageUrl = string.Empty;
                        if (!string.IsNullOrEmpty(productVariant.ImageUrl))
                        {
                            imageUrl = productVariant.ImageUrl.ToLowerInvariant();
                        }
                        if (string.IsNullOrEmpty(imageUrl) && !string.IsNullOrEmpty(product.ImageUrl))
                        {
                            imageUrl = product.ImageUrl.ToLowerInvariant();                                                                            // use product image if no image
                        }
                        if (string.IsNullOrEmpty(imageUrl))
                        {
                            continue;                                 // skip if no image
                        }
                        if (!IsAbsoluteURL(imageUrl))
                        {
                            imageUrl = ResolveUrl(imageUrl, storeUrl);
                        }

                        // verify unique image for each combination of ‘color’, ‘pattern’, and ‘material’
                        if (imageUrlList.Contains(imageUrl))
                        {
                            bool isUniqueImage = true;
                            // MAKE SURE OTHER VARIANTS ONLY DIFFER BY SIZE, OTHERWISE SKIP THIS
                            foreach (Variant otherVariant in variants)
                            {
                                if (imageUrl == otherVariant.ImageUrl)
                                {
                                    if (!(variant.Color == otherVariant.Color &&
                                          variant.Pattern == otherVariant.Pattern &&
                                          variant.Material == otherVariant.Material &&
                                          variant.Size != otherVariant.Size))
                                    {
                                        isUniqueImage = false;
                                        break;
                                    }
                                }
                            }
                            // SKIP THIS VARIANT, AS WE DO NOT HAVE A UNIQUE IMAGE FOR THIS VARIANT AS THIS IMAGE ALREADY USED FOR ANOTHER VARIANT, WHICH DIFFERS FOR MORE THEN SIZE
                            if (!isUniqueImage)
                            {
                                continue;
                            }
                        }

                        // else add to variant image dictionary to keep track of rest of variants
                        imageUrlList.Add(imageUrl);
                        variant.ImageUrl = imageUrl;

                        // use product id as item group id, this must be same for all variants of same product, but must be unique for variants of each product
                        variant.ItemGroupId = product.Id.ToString();

                        ProductCalculator productCalculator = ProductCalculator.LoadForProduct(product.Id, 1, productVariant.OptionList, string.Empty, 0);
                        variant.Weight = productCalculator.Weight;
                        variant.Price  = productCalculator.Price;
                        variant.Sku    = productCalculator.Sku;

                        string gtin = productVariant.GTIN;
                        if (string.IsNullOrEmpty(gtin))
                        {
                            // for compatibility with AC7 data check if SKU is a UPC
                            gtin = IsUpcCode(productVariant.Sku) ? productVariant.Sku : string.Empty;
                        }
                        variant.GTIN        = gtin;
                        variant.ModelNumber = productVariant.ModelNumber;
                        variant.InStock     = productVariant.InStock;
                        variants.Add(variant);
                    }
                }
            }

            return(variants);
        }