Exemplo n.º 1
0
        public virtual IActionResult SubscribePopupPOST(int productId)
        {
            var product = _productService.GetProductById(productId);

            if (product == null || product.Deleted)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            if (!_customerService.IsRegistered(_workContext.CurrentCustomer))
            {
                return(Content(_localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                _productService.GetTotalStockQuantity(product) <= 0)
            {
                //out of stock
                var subscription = _backInStockSubscriptionService
                                   .FindSubscription(_workContext.CurrentCustomer.Id, product.Id, _storeContext.CurrentStore.Id);
                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    _backInStockSubscriptionService.DeleteSubscription(subscription);

                    _notificationService.SuccessNotification(_localizationService.GetResource("BackInStockSubscriptions.Notification.Unsubscribed"));
                    return(new OkResult());
                }

                //subscription does not exist
                //subscribe
                if (_backInStockSubscriptionService
                    .GetAllSubscriptionsByCustomerId(_workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id, 0, 1)
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        result = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }
                subscription = new BackInStockSubscription
                {
                    CustomerId   = _workContext.CurrentCustomer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    CreatedOnUtc = DateTime.UtcNow
                };
                _backInStockSubscriptionService.InsertSubscription(subscription);

                _notificationService.SuccessNotification(_localizationService.GetResource("BackInStockSubscriptions.Notification.Subscribed"));
                return(new OkResult());
            }

            //subscription not possible
            return(Content(_localizationService.GetResource("BackInStockSubscriptions.NotAllowed")));
        }
        public virtual async Task <IActionResult> SubscribePopupPOST(string productId)
        {
            var product = await _productService.GetProductById(productId);

            if (product == null)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            var customer = _workContext.CurrentCustomer;

            if (!customer.IsRegistered())
            {
                return(Content(_localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.GetTotalStockQuantity(warehouseId: _storeContext.CurrentStore.DefaultWarehouseId) <= 0)
            {
                //out of stock
                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, _storeContext.CurrentStore.Id, product.UseMultipleWarehouses?_storeContext.CurrentStore.DefaultWarehouseId : "");

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Content("Unsubscribed"));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Content(string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                }
                subscription = new BackInStockSubscription
                {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = product.UseMultipleWarehouses ? _storeContext.CurrentStore.DefaultWarehouseId : "",
                    CreatedOnUtc = DateTime.UtcNow
                };
                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Content("Subscribed"));
            }

            //subscription not possible
            return(Content(_localizationService.GetResource("BackInStockSubscriptions.NotAllowed")));
        }
        public ActionResult BackInStockSubscribePopup(int id /* productId */, FormCollection form)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted || product.IsSystemProduct)
            {
                throw new ArgumentException(T("Products.NotFound", id));
            }

            if (!_services.WorkContext.CurrentCustomer.IsRegistered())
            {
                return(Content(T("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.StockQuantity <= 0)
            {
                var customer = _services.WorkContext.CurrentCustomer;
                var store    = _services.StoreContext.CurrentStore;

                // Out of stock.
                var subscription = _backInStockSubscriptionService.FindSubscription(customer.Id, product.Id, store.Id);
                if (subscription != null)
                {
                    // Unsubscribe.
                    _backInStockSubscriptionService.DeleteSubscription(subscription);
                    return(Content("Unsubscribed"));
                }
                else
                {
                    if (_backInStockSubscriptionService.GetAllSubscriptionsByCustomerId(customer.Id, store.Id, 0, 1).TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                    {
                        return(Content(string.Format(T("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                    }

                    // Subscribe.
                    subscription = new BackInStockSubscription
                    {
                        Customer     = customer,
                        Product      = product,
                        StoreId      = store.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };

                    _backInStockSubscriptionService.InsertSubscription(subscription);
                    return(Content("Subscribed"));
                }
            }
            else
            {
                return(Content(T("BackInStockSubscriptions.NotAllowed")));
            }
        }
Exemplo n.º 4
0
        public ActionResult BackInStockSubscribePopupPOST(int id /* productId */)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            if (!_services.WorkContext.CurrentCustomer.IsRegistered())
            {
                return(Content(T("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.StockQuantity <= 0)
            {
                //out of stock
                var subscription = _backInStockSubscriptionService
                                   .FindSubscription(_services.WorkContext.CurrentCustomer.Id, product.Id, _services.StoreContext.CurrentStore.Id);
                if (subscription != null)
                {
                    //unsubscribe
                    _backInStockSubscriptionService.DeleteSubscription(subscription);
                    return(Content("Unsubscribed"));
                }
                else
                {
                    if (_backInStockSubscriptionService
                        .GetAllSubscriptionsByCustomerId(_services.WorkContext.CurrentCustomer.Id, _services.StoreContext.CurrentStore.Id, 0, 1)
                        .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                    {
                        return(Content(string.Format(T("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                    }

                    //subscribe
                    subscription = new BackInStockSubscription()
                    {
                        Customer     = _services.WorkContext.CurrentCustomer,
                        Product      = product,
                        StoreId      = _services.StoreContext.CurrentStore.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };
                    _backInStockSubscriptionService.InsertSubscription(subscription);
                    return(Content("Subscribed"));
                }
            }
            else
            {
                return(Content(T("BackInStockSubscriptions.NotAllowed")));
            }
        }
Exemplo n.º 5
0
        public virtual async Task <IActionResult> SubscribePopup(string productId, IFormCollection form)
        {
            var product = await _productService.GetProductById(productId);

            if (product == null)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            var customer = _workContext.CurrentCustomer;

            string warehouseId = _shoppingCartSettings.AllowToSelectWarehouse ?
                                 form["WarehouseId"].ToString() :
                                 product.UseMultipleWarehouses ? _storeContext.CurrentStore.DefaultWarehouseId :
                                 (string.IsNullOrEmpty(_storeContext.CurrentStore.DefaultWarehouseId) ? product.WarehouseId : _storeContext.CurrentStore.DefaultWarehouseId);

            if (!customer.IsRegistered())
            {
                return(Json(new
                {
                    subscribe = false,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")
                }));
            }

            if ((product.ManageInventoryMethod == ManageInventoryMethod.ManageStock) &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.GetTotalStockQuantity(warehouseId: warehouseId) <= 0)
            {
                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, null, _storeContext.CurrentStore.Id, warehouseId);

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = _localizationService.GetResource("BackInStockSubscriptions.Unsubscribed")
                    }));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }
                subscription = new BackInStockSubscription {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = warehouseId,
                    CreatedOnUtc = DateTime.UtcNow
                };
                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Json(new
                {
                    subscribe = true,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.DeleteNotifyWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.Subscribed")
                }));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions)
            {
                var attributes = await _mediator.Send(new GetParseProductAttributes()
                {
                    Product = product, Form = form
                });

                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, attributes, _storeContext.CurrentStore.Id, warehouseId);

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = _localizationService.GetResource("BackInStockSubscriptions.Unsubscribed")
                    }));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }

                subscription = new BackInStockSubscription {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    Attributes   = attributes,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = warehouseId,
                    CreatedOnUtc = DateTime.UtcNow
                };

                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Json(new
                {
                    subscribe = true,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.DeleteNotifyWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.Subscribed")
                }));
            }

            return(Json(new
            {
                subscribe = false,
                buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                resource = _localizationService.GetResource("BackInStockSubscriptions.NotAllowed")
            }));
        }