Skip to content

Commit 1bc54b6

Browse files
authored
Merge pull request #1307 from algolia/bugfix/MAGE-561
Fix for Indexer created for websites excluded in Customer groups - (In Progress)
2 parents 1a6d14d + 5d7bbcf commit 1bc54b6

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

Helper/ConfigHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Magento\Framework\Serialize\SerializerInterface;
1212
use Magento\Store\Model\ScopeInterface;
1313
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
1415
use Magento\Cookie\Helper\Cookie as CookieHelper;
1516

17+
1618
class ConfigHelper
1719
{
1820
public const ENABLE_FRONTEND = 'algoliasearch_credentials/credentials/enable_frontend';
@@ -195,6 +197,11 @@ class ConfigHelper
195197
*/
196198
protected $groupCollection;
197199

200+
/**
201+
* @var GroupExcludedWebsiteRepositoryInterface
202+
*/
203+
protected $groupExcludedWebsiteRepository;
204+
198205
/**
199206
* @var CookieHelper
200207
*/
@@ -211,7 +218,9 @@ class ConfigHelper
211218
* @param Magento\Framework\Event\ManagerInterface $eventManager
212219
* @param SerializerInterface $serializer
213220
* @param GroupCollection $groupCollection
221+
* @param GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository
214222
* @param CookieHelper $cookieHelper
223+
215224
*/
216225
public function __construct(
217226
Magento\Framework\App\Config\ScopeConfigInterface $configInterface,
@@ -224,6 +233,7 @@ public function __construct(
224233
Magento\Framework\Event\ManagerInterface $eventManager,
225234
SerializerInterface $serializer,
226235
GroupCollection $groupCollection,
236+
GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
227237
CookieHelper $cookieHelper
228238
) {
229239
$this->configInterface = $configInterface;
@@ -236,6 +246,7 @@ public function __construct(
236246
$this->eventManager = $eventManager;
237247
$this->serializer = $serializer;
238248
$this->groupCollection = $groupCollection;
249+
$this->groupExcludedWebsiteRepository = $groupExcludedWebsiteRepository;
239250
$this->cookieHelper = $cookieHelper;
240251
}
241252

@@ -1021,12 +1032,17 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10211032
$indexName = false;
10221033
$sortAttribute = false;
10231034
if ($this->isCustomerGroupsEnabled($storeId) && $attr['attribute'] === 'price') {
1035+
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
10241036
$groupCollection = $this->groupCollection;
10251037
if (!is_null($currentCustomerGroupId)) {
10261038
$groupCollection->addFilter('customer_group_id', $currentCustomerGroupId);
10271039
}
10281040
foreach ($groupCollection as $group) {
10291041
$customerGroupId = (int)$group->getData('customer_group_id');
1042+
$excludedWebsites = $this->groupExcludedWebsiteRepository->getCustomerGroupExcludedWebsites($customerGroupId);
1043+
if (in_array($websiteId, $excludedWebsites)) {
1044+
continue;
1045+
}
10301046
$groupIndexNameSuffix = 'group_' . $customerGroupId;
10311047
$groupIndexName =
10321048
$originalIndexName . '_' . $attr['attribute'] . '_' . $groupIndexNameSuffix . '_' . $attr['sort'];

Helper/Entity/Product/PriceManager/ProductWithoutChildren.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Customer\Model\Group;
1212
use Magento\Customer\Api\Data\GroupInterface;
1313
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
14+
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
1415
use Magento\Framework\Pricing\PriceCurrencyInterface;
1516
use Magento\Tax\Helper\Data as TaxHelper;
1617
use Magento\Tax\Model\Config as TaxConfig;
@@ -47,6 +48,11 @@ abstract class ProductWithoutChildren
4748
*/
4849
protected $productloader;
4950

51+
/**
52+
* @var GroupExcludedWebsiteRepositoryInterface
53+
*/
54+
protected $groupExcludedWebsiteRepository;
55+
5056
/**
5157
* @var ScopedProductTierPriceManagementInterface
5258
*/
@@ -61,6 +67,7 @@ abstract class ProductWithoutChildren
6167
/**
6268
* @param ConfigHelper $configHelper
6369
* @param CollectionFactory $customerGroupCollectionFactory
70+
* @param GroupExcludedWebsiteRepositoryInterface groupExcludedWebsiteRepository
6471
* @param PriceCurrencyInterface $priceCurrency
6572
* @param CatalogHelper $catalogHelper
6673
* @param TaxHelper $taxHelper
@@ -71,6 +78,7 @@ abstract class ProductWithoutChildren
7178
public function __construct(
7279
ConfigHelper $configHelper,
7380
CollectionFactory $customerGroupCollectionFactory,
81+
GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
7482
PriceCurrencyInterface $priceCurrency,
7583
CatalogHelper $catalogHelper,
7684
TaxHelper $taxHelper,
@@ -80,6 +88,7 @@ public function __construct(
8088
) {
8189
$this->configHelper = $configHelper;
8290
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
91+
$this->groupExcludedWebsiteRepository = $groupExcludedWebsiteRepository;
8392
$this->priceCurrency = $priceCurrency;
8493
$this->catalogHelper = $catalogHelper;
8594
$this->taxHelper = $taxHelper;
@@ -105,6 +114,18 @@ public function addPriceData($customData, Product $product, $subProducts): array
105114
$fields = $this->getFields();
106115
if (!$this->areCustomersGroupsEnabled) {
107116
$this->groups->addFieldToFilter('main_table.customer_group_id', 0);
117+
} else {
118+
$excludedGroups = array();
119+
foreach ($this->groups as $group) {
120+
$groupId = (int)$group->getData('customer_group_id');
121+
$excludedWebsites = $this->groupExcludedWebsiteRepository->getCustomerGroupExcludedWebsites($groupId);
122+
if (in_array($product->getStore()->getWebsiteId(), $excludedWebsites)) {
123+
$excludedGroups[] = $groupId;
124+
}
125+
}
126+
if(count($excludedGroups) > 0) {
127+
$this->groups->addFieldToFilter('main_table.customer_group_id', ["nin" => $excludedGroups]);
128+
}
108129
}
109130
// price/price_with_tax => true/false
110131
foreach ($fields as $field => $withTax) {

Helper/Entity/ProductHelper.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Magento\Framework\Event\ManagerInterface;
3434
use Magento\Store\Model\Store;
3535
use Magento\Store\Model\StoreManagerInterface;
36+
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
3637

3738
class ProductHelper
3839
{
@@ -112,6 +113,11 @@ class ProductHelper
112113
*/
113114
protected $productAttributes;
114115

116+
/**
117+
* @var GroupExcludedWebsiteRepositoryInterface
118+
*/
119+
protected $groupExcludedWebsiteRepository;
120+
115121
/**
116122
* @var string[]
117123
*/
@@ -166,6 +172,7 @@ class ProductHelper
166172
* @param Type $productType
167173
* @param CollectionFactory $productCollectionFactory
168174
* @param GroupCollection $groupCollection
175+
* @param GroupExcludedWebsiteRepositoryInterface groupExcludedWebsiteRepository
169176
* @param ImageHelper $imageHelper
170177
*/
171178
public function __construct(
@@ -178,15 +185,15 @@ public function __construct(
178185
Visibility $visibility,
179186
Stock $stockHelper,
180187
StockRegistryInterface $stockRegistry,
181-
CurrencyHelper $currencyManager,
182-
CategoryHelper $categoryHelper,
183-
PriceManager $priceManager,
184-
Type $productType,
185-
CollectionFactory $productCollectionFactory,
186-
GroupCollection $groupCollection,
187-
ImageHelper $imageHelper
188-
)
189-
{
188+
CurrencyHelper $currencyManager,
189+
CategoryHelper $categoryHelper,
190+
PriceManager $priceManager,
191+
Type $productType,
192+
CollectionFactory $productCollectionFactory,
193+
GroupCollection $groupCollection,
194+
GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
195+
ImageHelper $imageHelper
196+
) {
190197
$this->eavConfig = $eavConfig;
191198
$this->configHelper = $configHelper;
192199
$this->algoliaHelper = $algoliaHelper;
@@ -202,6 +209,7 @@ public function __construct(
202209
$this->productType = $productType;
203210
$this->productCollectionFactory = $productCollectionFactory;
204211
$this->groupCollection = $groupCollection;
212+
$this->groupExcludedWebsiteRepository = $groupExcludedWebsiteRepository;
205213
$this->imageHelper = $imageHelper;
206214
}
207215

@@ -1313,9 +1321,14 @@ protected function getAttributesForFaceting($storeId)
13131321
$facet['attribute'] = 'price.' . $currency_code . '.default';
13141322

13151323
if ($this->configHelper->isCustomerGroupsEnabled($storeId)) {
1324+
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
13161325
foreach ($this->groupCollection as $group) {
1317-
$group_id = (int)$group->getData('customer_group_id');
1318-
1326+
$group_id = (int) $group->getData('customer_group_id');
1327+
$groupId = (int)$group->getData('customer_group_id');
1328+
$excludedWebsites = $this->groupExcludedWebsiteRepository->getCustomerGroupExcludedWebsites($groupId);
1329+
if (in_array($websiteId, $excludedWebsites)) {
1330+
continue;
1331+
}
13191332
$attributesForFaceting[] = 'price.' . $currency_code . '.group_' . $group_id;
13201333
}
13211334
}

0 commit comments

Comments
 (0)