예제 #1
0
        public void AddProductToCartTest(string proId, int amount, int expectedProductCount, int expectedTotalAmout)
        {
            var svc = new CartController();

            CartController.CartProducts = MockProductInCart;
            CartController.Products     = MockProduct;

            svc.AddProductToCart(proId, amount);

            var result = svc.GetProductInCart().Value.ToList();

            Assert.Equal(result.Count, expectedProductCount);

            var haveCorrectAmout = result.Any(it => it.Product.id == proId && it.Amount == expectedTotalAmout);

            Assert.True(haveCorrectAmout);
        }
예제 #2
0
        protected void btnAddToCart_Click(object sender, EventArgs e)
        {
            bool IsAvailableForPurchase = product.IsPurchaseableByUser;

            if (IsAvailableForPurchase)
            {
                int productId = product.Id.Value;
                int quantity  = WA.Parser.ToInt(txtQuantity.Text).GetValueOrDefault(1);

                if ((product.InventoryIsEnabled == true && quantity < product.InventoryQtyInStock.Value) || product.InventoryIsEnabled == false)
                {
                    string jsonProductFieldData = "";
                    List <JsonProductFieldData> productFieldsData = GetPostedWidgetValues(Request);
                    if (productFieldsData.Count > 0)
                    {
                        jsonProductFieldData = Newtonsoft.Json.JsonConvert.SerializeObject(productFieldsData);
                    }

                    CartController cartController = new CartController(StoreContext);
                    cartController.AddProductToCart(productId, quantity, jsonProductFieldData);

                    //var checkoutOrderInfo = Session[StoreContext.SessionKeys.CheckoutOrderInfo] as CheckoutOrderInfo ?? new CheckoutOrderInfo() { Cart = cartController.GetCart(false) };
                    //checkoutOrderInfo.ReCalculateOrderTotals();
                    //Session[StoreContext.SessionKeys.CheckoutOrderInfo] = checkoutOrderInfo;

                    Response.Redirect(StoreUrls.Cart(string.Format(@"""{0}"" has been added to your cart", product.Name)));
                }
                else
                {
                    ShowFlash("This product is out of stock");
                }
            }
            else
            {
                ShowFlash("This product is no longer available for purchase");
            }
        }
예제 #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadResourceFileSettings();

            cartController = new CartController(StoreContext);

            checkoutOrderInfo = Session[StoreContext.SessionKeys.CheckoutOrderInfo] as CheckoutOrderInfo ?? new CheckoutOrderInfo()
            {
                Cart = cartController.GetCart(true)
            };
            checkoutOrderInfo.Cart = cartController.GetCart(true);
            checkoutOrderInfo.ReCalculateOrderTotals();

            var payPalStandard = new PayPalStandardProvider(StoreContext.CurrentStore.GetPaymentProviderConfig(PaymentProviderName.PayPalStandard));

            collectPayPalStandardShipping = payPalStandard.ShippingLogic == "Store" && !checkoutOrderInfo.HasOnlyDownloadableProducts;

            if (!IsPostBack)
            {
                int?removeCartItem = WA.Parser.ToInt(Request.QueryString["remove"]);
                if (removeCartItem.HasValue)
                {
                    RemoveCartItemFromCart(removeCartItem.Value);
                    UpdateCheckoutSession();

                    // redirect so that the "mini-cart" updates on the page also...
                    Response.Redirect(StoreUrls.Cart());
                }
                // FEATURE: Add product to the cart via URL/QueryString
                string addProductSlug = Request.QueryString["add"] ?? string.Empty;
                if (!string.IsNullOrEmpty(addProductSlug))
                {
                    var productToAdd = Product.GetBySlug(StoreContext.CurrentStore.Id.Value, addProductSlug);
                    if (productToAdd != null)
                    {
                        //bool IsAvailableForPurchase = productToAdd.IsAvailableForPurchase.GetValueOrDefault(true) && WA.Parser.ToBool(StoreContext.CurrentStore.GetSetting(StoreSettingNames.EnableCheckout)).GetValueOrDefault(true);
                        bool IsAvailableForPurchase = productToAdd.IsPurchaseableByUser;
                        if (IsAvailableForPurchase)
                        {
                            int quantityToAdd = Request.QueryString["q"] != null?Convert.ToInt32(Request.QueryString["q"]) : 1;

                            cartController.AddProductToCart(productToAdd.Id.Value, quantityToAdd, string.Empty);
                        }
                        checkoutOrderInfo.Cart = cartController.GetCart(false);
                        checkoutOrderInfo.ReCalculateOrderTotals();

                        bool redirectBackToReferrer = WA.Parser.ToBool(Request.QueryString["redirect"]).GetValueOrDefault(false);
                        if (redirectBackToReferrer && (Request.UrlReferrer != null))
                        {
                            string redirectUrl = Request.UrlReferrer.ToString();


                            // Remove previous flash message from querystring when redirecting
                            if (redirectUrl.ToLower().Contains("flash"))
                            {
                                int flashIndex = redirectUrl.IndexOf("flash");
                                redirectUrl = redirectUrl.Substring(0, flashIndex - 1);
                            }

                            bool referrerIsOnsite = (Request.UrlReferrer.Host == Request.Url.Host);
                            if (referrerIsOnsite)
                            {
                                // redirect and add a 'flash' message to notify customer that product was added to cart
                                if (IsAvailableForPurchase)
                                {
                                    redirectUrl = redirectUrl.AddUrlParam("flash", HttpUtility.UrlPathEncode(string.Format(@"""{0}"" has been added to your cart", productToAdd.Name)));
                                }
                                else
                                {
                                    redirectUrl = redirectUrl.AddUrlParam("flash", HttpUtility.UrlPathEncode(string.Format(@"""{0}"" is not available for purchase.", productToAdd.Name)));
                                }
                            }
                            //Response.Redirect(redirectUrl, true);
                            Response.Redirect(redirectUrl);
                        }
                        else
                        {
                            if (IsAvailableForPurchase)
                            {
                                flash.InnerHtml = string.Format(@"""{0}"" has been added to your cart", productToAdd.Name);
                            }
                            else
                            {
                                flash.InnerHtml = string.Format(@"""{0}"" is not available for purchase", productToAdd.Name);
                            }
                            flash.Visible = true;
                        }
                    }
                }

                DataBindCartItems();

                var store = StoreContext.CurrentStore;

                //---- checkout buttons
                bool payLaterPaymentEnabled        = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.PayLater);
                bool cardCaptureOnlyPaymentEnabled = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.CardCaptureOnly);
                bool authorizeNetAimEnabled        = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.AuthorizeNetAim);
                bool payPalDirectEnabled           = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.PayPalDirectPayment);
                bool onsitePaymentProviderEnabled  = payLaterPaymentEnabled || cardCaptureOnlyPaymentEnabled || authorizeNetAimEnabled || payPalDirectEnabled;

                btnCheckoutOnsite.Visible         = onsitePaymentProviderEnabled;
                btnCheckoutPayPalStandard.Visible = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.PayPalStandard);
                ibtnPayPalExpressCheckout.Visible = StoreContext.CurrentStore.IsPaymentProviderEnabled(PaymentProviderName.PayPalExpressCheckout);
                if (btnCheckoutOnsite.Visible && (btnCheckoutPayPalStandard.Visible || ibtnPayPalExpressCheckout.Visible))
                {
                    spnOr.Visible = true;
                }

                //collectPayPalStandardShipping = payPalStandard.ShippingLogic == "Store";
                //payPalStandard = new PayPalStandardProvider(StoreContext.CurrentStore.GetPaymentProviderConfig(PaymentProviderName.PayPalStandard));)
                if (collectPayPalStandardShipping)
                {
                    string        storeCountry         = store.GetSetting(StoreSettingNames.DefaultCountryCode);
                    var           countries            = DnnHelper.GetCountryListAdoNet();
                    StringBuilder countriesOptionsHtml = new StringBuilder(countries.Count);
                    foreach (var c in countries)
                    {
                        countriesOptionsHtml.AppendFormat(@"<option value=""{0}"" {2}>{1}</option>", c.CountryCode, c.Name, c.CountryCode == storeCountry ? "selected=selected" : string.Empty);
                    }
                    litCountryOptionsHtml.Text = countriesOptionsHtml.ToString();
                }

                //IShippingProvider fedExProvider = ShippingProviderFactory.Get(StoreContext.CurrentStore.Id.Value, ShippingProviderType.FedEx);
                //pnlShippingQuoteForm.Visible = fedExProvider.IsEnabled;
                var shippingServices = StoreContext.CurrentStore.GetEnabledShippingProviders(null, checkoutOrderInfo.Cart.Id);
                if (shippingServices.Count > 0)
                {
                    pnlShippingQuoteForm.Visible = true;
                }

                //----------Show / Hide Coupon Box and Shipping Estimate Boxes

                pnlShippingQuoteForm.Visible = WA.Parser.ToBool(store.GetSetting(StoreSettingNames.ShowShippingEstimate)).GetValueOrDefault(true);
                pnlCouponCodeForm.Visible    = WA.Parser.ToBool(store.GetSetting(StoreSettingNames.ShowCouponBox)).GetValueOrDefault(true);
            }
        }