Exemple #1
0
        public virtual void InheritStoresIntoChildren(
            int categoryId,
            bool touchProductsWithMultipleCategories = false,
            bool touchExistingAcls = false,
            bool categoriesOnly    = false)
        {
            var category              = GetCategoryById(categoryId);
            var subcategories         = GetAllCategoriesByParentCategoryId(categoryId, true);
            var allStores             = _storeService.GetAllStores();
            var categoryStoreMappings = _storeMappingService.GetStoresIdsWithAccess(category);

            var categoryIds = new HashSet <int>(subcategories.Select(x => x.Id));

            categoryIds.Add(categoryId);

            var searchQuery = new CatalogSearchQuery()
                              .WithCategoryIds(null, categoryIds.ToArray());

            var query    = _catalogSearchService.PrepareQuery(searchQuery);
            var products = query.OrderBy(p => p.Id).ToList();

            using (var scope = new DbContextScope(ctx: _storeMappingRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
            {
                _storeMappingRepository.AutoCommitEnabled = false;

                foreach (var subcategory in subcategories)
                {
                    if (subcategory.LimitedToStores != category.LimitedToStores)
                    {
                        subcategory.LimitedToStores = category.LimitedToStores;
                        _categoryRepository.Update(subcategory);
                    }

                    var existingStoreMappingsRecords = _storeMappingService.GetStoreMappings(subcategory).ToDictionary(x => x.StoreId);

                    foreach (var store in allStores)
                    {
                        if (categoryStoreMappings.Contains(store.Id))
                        {
                            if (!existingStoreMappingsRecords.ContainsKey(store.Id))
                            {
                                _storeMappingRepository.Insert(new StoreMapping {
                                    StoreId = store.Id, EntityId = subcategory.Id, EntityName = "Category"
                                });
                            }
                        }
                        else
                        {
                            if (existingStoreMappingsRecords.TryGetValue(store.Id, out var storeMappingToDelete))
                            {
                                _storeMappingRepository.Delete(storeMappingToDelete);
                            }
                        }
                    }
                }

                _storeMappingRepository.Context.SaveChanges();

                foreach (var product in products)
                {
                    if (product.LimitedToStores != category.LimitedToStores)
                    {
                        product.LimitedToStores = category.LimitedToStores;
                        _productRepository.Update(product);
                    }

                    var existingStoreMappingsRecords = _storeMappingService.GetStoreMappings(product).ToDictionary(x => x.StoreId);

                    foreach (var store in allStores)
                    {
                        if (categoryStoreMappings.Contains(store.Id))
                        {
                            if (!existingStoreMappingsRecords.ContainsKey(store.Id))
                            {
                                _storeMappingRepository.Insert(new StoreMapping {
                                    StoreId = store.Id, EntityId = product.Id, EntityName = "Product"
                                });
                            }
                        }
                        else
                        {
                            if (existingStoreMappingsRecords.TryGetValue(store.Id, out var storeMappingToDelete))
                            {
                                _storeMappingRepository.Delete(storeMappingToDelete);
                            }
                        }
                    }
                }

                _storeMappingRepository.Context.SaveChanges();
            }
        }
Exemple #2
0
 /// <summary>
 /// Gets store mapping records
 /// </summary>
 /// <param name="entityName">Type</param>
 /// <param name="entity">Entity</param>
 /// <returns>Store mapping records</returns>
 public IList <StoreMapping> GetStoreMappings(string entityName, int entityId)
 {
     return(_storeMappingService.GetStoreMappings(entityName, entityId));
 }
Exemple #3
0
        public virtual void InheritStoresIntoChildren(int categoryId,
                                                      bool touchProductsWithMultipleCategories = false,
                                                      bool touchExistingAcls = false,
                                                      bool categoriesOnly    = false)
        {
            var category      = GetCategoryById(categoryId);
            var subcategories = GetAllCategoriesByParentCategoryId(categoryId, true);
            var context       = new ProductSearchContext {
                PageSize = int.MaxValue, ShowHidden = true
            };

            context.CategoryIds.AddRange(subcategories.Select(x => x.Id));
            context.CategoryIds.Add(categoryId);
            var products = _productService.SearchProducts(context);

            var allStores             = _storeService.GetAllStores();
            var categoryStoreMappings = _storeMappingService.GetStoresIdsWithAccess(category);

            using (var scope = new DbContextScope(ctx: _storeMappingRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
            {
                _storeMappingRepository.AutoCommitEnabled = false;

                foreach (var subcategory in subcategories)
                {
                    if (subcategory.LimitedToStores != category.LimitedToStores)
                    {
                        subcategory.LimitedToStores = category.LimitedToStores;
                        _categoryRepository.Update(subcategory);
                    }

                    var existingStoreMappingsRecords = _storeMappingService.GetStoreMappings(subcategory).ToDictionary(x => x.StoreId);

                    foreach (var store in allStores)
                    {
                        if (categoryStoreMappings.Contains(store.Id))
                        {
                            if (!existingStoreMappingsRecords.ContainsKey(store.Id))
                            {
                                _storeMappingRepository.Insert(new StoreMapping {
                                    StoreId = store.Id, EntityId = subcategory.Id, EntityName = "Category"
                                });
                            }
                        }
                        else
                        {
                            StoreMapping storeMappingToDelete;
                            if (existingStoreMappingsRecords.TryGetValue(store.Id, out storeMappingToDelete))
                            {
                                _storeMappingRepository.Delete(storeMappingToDelete);
                            }
                        }
                    }
                }

                _storeMappingRepository.Context.SaveChanges();

                foreach (var product in products)
                {
                    if (product.LimitedToStores != category.LimitedToStores)
                    {
                        product.LimitedToStores = category.LimitedToStores;
                        _productRepository.Update(product);
                    }

                    var existingStoreMappingsRecords = _storeMappingService.GetStoreMappings(product).ToDictionary(x => x.StoreId);

                    foreach (var store in allStores)
                    {
                        if (categoryStoreMappings.Contains(store.Id))
                        {
                            if (!existingStoreMappingsRecords.ContainsKey(store.Id))
                            {
                                _storeMappingRepository.Insert(new StoreMapping {
                                    StoreId = store.Id, EntityId = product.Id, EntityName = "Product"
                                });
                            }
                        }
                        else
                        {
                            StoreMapping storeMappingToDelete;
                            if (existingStoreMappingsRecords.TryGetValue(store.Id, out storeMappingToDelete))
                            {
                                _storeMappingRepository.Delete(storeMappingToDelete);
                            }
                        }
                    }
                }

                _storeMappingRepository.Context.SaveChanges();
            }
        }
        public virtual int SendCampaign(Campaign campaign)
        {
            Guard.NotNull(campaign, nameof(campaign));

            var totalEmailsSent = 0;
            var pageIndex       = -1;

            int[] storeIds = null;
            int[] rolesIds = null;
            var   alreadyProcessedEmails = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

            if (campaign.LimitedToStores)
            {
                storeIds = _storeMappingService.GetStoreMappings(campaign)
                           .Select(x => x.StoreId)
                           .Distinct()
                           .ToArray();
            }

            if (campaign.SubjectToAcl)
            {
                rolesIds = _aclService.GetAclRecords(campaign)
                           .Select(x => x.CustomerRoleId)
                           .Distinct()
                           .ToArray();
            }

            while (true)
            {
                var subscribers = _newsLetterSubscriptionService.GetAllNewsLetterSubscriptions(null, ++pageIndex, 500, false, storeIds, rolesIds);

                foreach (var subscriber in subscribers)
                {
                    // Create only one message per subscription email.
                    if (alreadyProcessedEmails.Contains(subscriber.Subscription.Email))
                    {
                        continue;
                    }

                    if (subscriber.Customer != null && !subscriber.Customer.Active)
                    {
                        continue;
                    }

                    var result = SendCampaign(campaign, subscriber);
                    if ((result?.Email?.Id ?? 0) != 0)
                    {
                        alreadyProcessedEmails.Add(subscriber.Subscription.Email);

                        ++totalEmailsSent;
                    }
                }

                if (!subscribers.HasNextPage)
                {
                    break;
                }
            }

            return(totalEmailsSent);
        }