Пример #1
0
        public void Can_save_and_load_backInStockSubscription()
        {
            var backInStockSubscription = new BackInStockSubscription()
            {
                Product  = GetTestProduct(),
                Customer = new Customer
                {
                    CustomerGuid        = Guid.NewGuid(),
                    AdminComment        = "some comment here",
                    Active              = true,
                    Deleted             = false,
                    CreatedOnUtc        = new DateTime(2010, 01, 01),
                    LastActivityDateUtc = new DateTime(2010, 01, 02)
                },
                CreatedOnUtc = new DateTime(2010, 01, 02)
            };

            var fromDb = SaveAndLoadEntity(backInStockSubscription);

            fromDb.ShouldNotBeNull();

            fromDb.Product.ShouldNotBeNull();
            fromDb.Customer.ShouldNotBeNull();

            fromDb.CreatedOnUtc.ShouldEqual(new DateTime(2010, 01, 02));
        }
        public virtual async Task <IActionResult> SubscribePopupPOST(int productId)
        {
            var product = await _productService.GetProductByIdAsync(productId);

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

            if (!await _customerService.IsRegisteredAsync(await _workContext.GetCurrentCustomerAsync()))
            {
                return(Content(await _localizationService.GetResourceAsync("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                await _productService.GetTotalStockQuantityAsync(product) <= 0)
            {
                //out of stock
                var subscription = await _backInStockSubscriptionService
                                   .FindSubscriptionAsync((await _workContext.GetCurrentCustomerAsync()).Id, product.Id, (await _storeContext.GetCurrentStoreAsync()).Id);

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

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

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerIdAsync((await _workContext.GetCurrentCustomerAsync()).Id, (await _storeContext.GetCurrentStoreAsync()).Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        result = string.Format(await _localizationService.GetResourceAsync("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }
                subscription = new BackInStockSubscription
                {
                    CustomerId   = (await _workContext.GetCurrentCustomerAsync()).Id,
                    ProductId    = product.Id,
                    StoreId      = (await _storeContext.GetCurrentStoreAsync()).Id,
                    CreatedOnUtc = DateTime.UtcNow
                };
                await _backInStockSubscriptionService.InsertSubscriptionAsync(subscription);

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

            //subscription not possible
            return(Content(await _localizationService.GetResourceAsync("BackInStockSubscriptions.NotAllowed")));
        }
Пример #3
0
        public virtual void AddBackInStockTokens(IList <Token> tokens, BackInStockSubscription subscription)
        {
            tokens.Add(new Token("BackInStockSubscription.ProductName", subscription.ProductVariant.FullProductName));

            //event notification
            _eventPublisher.EntityTokensAdded(subscription, tokens);
        }
Пример #4
0
        /// <summary>
        /// Sends a 'Back in stock' notification message to a customer
        /// </summary>
        /// <param name="subscription">Subscription</param>
        /// <param name="languageId">Message language identifier</param>
        /// <returns>Queued email identifier</returns>
        public virtual int SendBackInStockNotification(BackInStockSubscription subscription, int languageId)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            languageId = EnsureLanguageIsActive(languageId);

            var messageTemplate = GetLocalizedActiveMessageTemplate("Customer.BackInStock", languageId);

            if (messageTemplate == null)
            {
                return(0);
            }

            var subscriptionTokens = GenerateTokens(subscription);

            var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId);
            var customer     = subscription.Customer;
            var toEmail      = customer.Email;
            var toName       = customer.GetFullName();

            return(SendNotification(messageTemplate, emailAccount,
                                    languageId, subscriptionTokens,
                                    toEmail, toName));
        }
Пример #5
0
        public void AddBackInStockTokens(LiquidObject liquidObject, BackInStockSubscription subscription)
        {
            var liquidBackInStockSubscription = new LiquidBackInStockSubscription(subscription);

            liquidObject.BackInStockSubscription = liquidBackInStockSubscription;

            _eventPublisher.EntityTokensAdded(subscription, liquidBackInStockSubscription, liquidObject);
        }
        public LiquidBackInStockSubscription(BackInStockSubscription backInStockSubscription)
        {
            this._storeService            = EngineContext.Current.Resolve <IStoreService>();
            this._backInStockSubscription = backInStockSubscription;
            this._product = EngineContext.Current.Resolve <IProductService>().GetProductById(_backInStockSubscription.ProductId);

            AdditionalTokens = new Dictionary <string, string>();
        }
        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")));
        }
Пример #8
0
        private IList <Token> GenerateTokens(BackInStockSubscription stockSubscription)
        {
            var tokens = new List <Token>();

            _messageTokenProvider.AddStoreTokens(tokens);
            _messageTokenProvider.AddCustomerTokens(tokens, stockSubscription.Customer);
            _messageTokenProvider.AddBackInStockTokens(tokens, stockSubscription);
            return(tokens);
        }
        public LiquidBackInStockSubscription(Product product, BackInStockSubscription backInStockSubscription, Store store, Language language)
        {
            this._backInStockSubscription = backInStockSubscription;
            this._product  = product;
            this._store    = store;
            this._language = language;

            AdditionalTokens = new Dictionary <string, string>();
        }
 /// <summary>
 /// Sends a 'Back in stock' notification message to a customer
 /// </summary>
 /// <param name="subscription">Subscription</param>
 /// <param name="languageId">Message language identifier</param>
 /// <returns>Queued email identifier</returns>
 public override int SendBackInStockNotification(BackInStockSubscription subscription, int languageId)
 {
     return(SendNotification(subscription.StoreId, languageId, "Customer.BackInStock",
                             new TokenModel {
         Customer = subscription.Customer,
         BillingAddress = subscription.Customer.BillingAddress, Subscription = subscription
     },
                             subscription.Customer.Email, subscription.Customer.GetFullName()));
 }
        /// <summary>
        /// Delete a back in stock subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual void DeleteSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            _backInStockSubscriptionRepository.Delete(subscription);
        }
Пример #12
0
        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")));
            }
        }
Пример #13
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")));
            }
        }
Пример #14
0
        public virtual void AddBackInStockTokens(IList <Token> tokens, BackInStockSubscription subscription)
        {
            tokens.Add(new Token("BackInStockSubscription.ProductName", subscription.Product.Name));
            //TODO add a method for getting URL (use routing because it handles all SEO friendly URLs)
            var productUrl = string.Format("{0}{1}", _webHelper.GetStoreLocation(false), subscription.Product.GetSeName());

            tokens.Add(new Token("BackInStockSubscription.ProductUrl", productUrl, true));

            //event notification
            _eventPublisher.EntityTokensAdded(subscription, tokens);
        }
        /// <summary>
        /// Updates subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual void UpdateSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            _backInStockSubscriptionRepository.Update(subscription);

            //_unitOfWork.Commit();
        }
Пример #16
0
        public void InsertSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            _backInStockSubscriptionRepository.Insert(subscription);

            //event notification
            _eventPublisher.EntityInserted(subscription);
        }
        /// <summary>
        /// Delete a back in stock subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual async Task DeleteSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            await _backInStockSubscriptionRepository.DeleteAsync(subscription);

            //event notification
            await _mediator.EntityDeleted(subscription);
        }
Пример #18
0
        /// <summary>
        /// Updates subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual async Task UpdateSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            await _backInStockSubscriptionRepository.UpdateAsync(subscription);

            //event notification
            await _eventPublisher.EntityUpdated(subscription);
        }
Пример #19
0
        /// <summary>
        /// Delete a back in stock subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual void DeleteSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            _backInStockSubscriptionRepository.Delete(subscription);

            //event notification
            _eventPublisher.EntityDeleted(subscription);
        }
Пример #20
0
        /// <summary>
        /// Updates subscription
        /// </summary>
        /// <param name="subscription">Subscription</param>
        public virtual void UpdateSubscription(BackInStockSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("subscription");
            }

            _backInStockSubscriptionRepository.Update(subscription);

            //event notification
            _eventPublisher.EntityUpdated(subscription);
        }
        public void Can_save_and_load_backInStockSubscription()
        {
            var backInStockSubscription = new BackInStockSubscription()
            {
                Store = new Store
                {
                    Name = "Store 1",
                    Url  = "http://www.yourstore.com",
                },
                ProductVariant = new ProductVariant
                {
                    Name         = "Product variant name 1",
                    CreatedOnUtc = new DateTime(2010, 01, 03),
                    UpdatedOnUtc = new DateTime(2010, 01, 04),
                    Product      = new Product()
                    {
                        Name         = "Name 1",
                        Published    = true,
                        Deleted      = false,
                        CreatedOnUtc = new DateTime(2010, 01, 01),
                        UpdatedOnUtc = new DateTime(2010, 01, 02)
                    }
                },
                Customer = new Customer
                {
                    CustomerGuid        = Guid.NewGuid(),
                    AdminComment        = "some comment here",
                    Active              = true,
                    Deleted             = false,
                    CreatedOnUtc        = new DateTime(2010, 01, 01),
                    LastActivityDateUtc = new DateTime(2010, 01, 02)
                },
                CreatedOnUtc = new DateTime(2010, 01, 02)
            };

            var fromDb = SaveAndLoadEntity(backInStockSubscription);

            fromDb.ShouldNotBeNull();

            fromDb.Store.ShouldNotBeNull();

            fromDb.ProductVariant.ShouldNotBeNull();
            fromDb.ProductVariant.Name.ShouldEqual("Product variant name 1");

            fromDb.Customer.ShouldNotBeNull();

            fromDb.CreatedOnUtc.ShouldEqual(new DateTime(2010, 01, 02));
        }
Пример #22
0
        protected virtual object CreateModelPart(BackInStockSubscription part, MessageContext messageContext)
        {
            Guard.NotNull(messageContext, nameof(messageContext));
            Guard.NotNull(part, nameof(part));

            var m = new Dictionary <string, object>
            {
                { "StoreId", part.StoreId },
                { "CustomerId", part.CustomerId },
                { "ProductId", part.ProductId },
                { "CreatedOn", ToUserDate(part.CreatedOnUtc, messageContext) }
            };

            PublishModelPartCreatedEvent <BackInStockSubscription>(part, m);

            return(m);
        }
        /// <summary>
        /// Sends a 'Back in stock' notification message to a customer
        /// </summary>
        public static CreateMessageResult SendBackInStockNotification(this IMessageFactory factory, BackInStockSubscription subscription)
        {
            Guard.NotNull(subscription, nameof(subscription));

            var customer   = subscription.Customer;
            var languageId = customer.GetAttribute <int>(SystemCustomerAttributeNames.LanguageId);

            return(factory.CreateMessage(MessageContext.Create(MessageTemplateNames.BackInStockCustomer, languageId, subscription.StoreId, customer), true, subscription.Product));
        }
 /// <summary>
 /// Delete a back in stock subscription
 /// </summary>
 /// <param name="subscription">Subscription</param>
 /// <returns>A task that represents the asynchronous operation</returns>
 public virtual async Task DeleteSubscriptionAsync(BackInStockSubscription subscription)
 {
     await _backInStockSubscriptionRepository.DeleteAsync(subscription);
 }
 /// <summary>
 /// Inserts subscription
 /// </summary>
 /// <param name="subscription">Subscription</param>
 /// <returns>A task that represents the asynchronous operation</returns>
 public virtual async Task InsertSubscriptionAsync(BackInStockSubscription subscription)
 {
     await _backInStockSubscriptionRepository.InsertAsync(subscription);
 }
        public async Task AddBackInStockTokens(LiquidObject liquidObject, Product product, BackInStockSubscription subscription, Store store, Language language)
        {
            var liquidBackInStockSubscription = new LiquidBackInStockSubscription(product, subscription, store, language);

            liquidObject.BackInStockSubscription = liquidBackInStockSubscription;
            await _mediator.EntityTokensAdded(subscription, liquidBackInStockSubscription, liquidObject);
        }
Пример #27
0
 private void AssertSubscription(BackInStockSubscription expected, BackInStockSubscription actual)
 {
     Assert.AreEqual(expected.ProductId, actual.ProductId);
     Assert.AreEqual(expected.WarehouseId, actual.WarehouseId);
 }
Пример #28
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")
            }));
        }
Пример #29
0
        public async Task <IActionResult> BackInStockSubscribePopup(int id)
        {
            var product = await _db.Products.FindByIdAsync(id, false);

            if (product == null || product.IsSystemProduct || !product.Published)
            {
                return(Content(T("Products.NotFound", id)));
            }

            var customer = Services.WorkContext.CurrentCustomer;

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

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

                // Out of stock.
                var subscription = await _db.BackInStockSubscriptions
                                   .ApplyStandardFilter(product.Id, customer.Id, store.Id)
                                   .FirstOrDefaultAsync();

                if (subscription != null)
                {
                    // Unsubscribe.
                    _db.BackInStockSubscriptions.Remove(subscription);
                    await _db.SaveChangesAsync();

                    return(Content("Unsubscribed"));
                }
                else
                {
                    var customerSubscriptionCount = await _db.BackInStockSubscriptions
                                                    .ApplyStandardFilter(customerId : customer.Id, storeId : store.Id)
                                                    .CountAsync();

                    if (customerSubscriptionCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                    {
                        return(Content(T("BackInStockSubscriptions.MaxSubscriptions", _catalogSettings.MaximumBackInStockSubscriptions)));
                    }

                    // Subscribe.
                    subscription = new BackInStockSubscription
                    {
                        Customer = customer,
                        //Product = product,    // INFO: (mh) (core) Throws: Ein expliziter Wert für die Identitätsspalte kann nicht in der Product-Tabelle eingefügt werden, wenn IDENTITY_INSERT auf OFF festgelegt ist.
                        ProductId    = product.Id,
                        StoreId      = store.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };

                    _db.BackInStockSubscriptions.Add(subscription);
                    await _db.SaveChangesAsync();

                    return(Content("Subscribed"));
                }
            }
            else
            {
                return(Content(T("BackInStockSubscriptions.NotAllowed")));
            }
        }
        public virtual ActionResult SubscribePopupPOST(int productId)
        {
            var product = _productService.GetProductById(productId);

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

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

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.GetTotalStockQuantity() <= 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);

                    return(Json(new
                    {
                        result = "Unsubscribed"
                    }));
                }

                //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
                {
                    Customer     = _workContext.CurrentCustomer,
                    Product      = product,
                    StoreId      = _storeContext.CurrentStore.Id,
                    CreatedOnUtc = DateTime.UtcNow
                };
                _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Json(new
                {
                    result = "Subscribed"
                }));
            }

            //subscription not possible
            return(Content(_localizationService.GetResource("BackInStockSubscriptions.NotAllowed")));
        }