Skip to content

Commit c4e060b

Browse files
authored
Update develop with develop-1.x (#999)
* Update B2B / Catalog Permissions raw SQL (#977) * update bundle product collection for subproducts (#982) * add adminhtml event for flush catalog image cache (#983)
1 parent 53dadcc commit c4e060b

10 files changed

+121
-48
lines changed

Factory/CatalogPermissionsFactory.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public function getCategoryPermissionsCollection()
5858
$indexResource = $this->getPermissionsIndexResource();
5959
$connection = $indexResource->getConnection();
6060

61-
$query = "
62-
SELECT category_id, GROUP_CONCAT(CONCAT(customer_group_id, '_', grant_catalog_category_view) SEPARATOR ',') AS permissions
63-
FROM {$indexResource->getMainTable()}
64-
GROUP BY category_id;
65-
";
61+
$select = $connection->select()
62+
->from($indexResource->getMainTable(), [])
63+
->columns('category_id')
64+
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(customer_group_id, '_', grant_catalog_category_view) SEPARATOR ',')")])
65+
->group('category_id');
6666

67-
$this->categoryPermissionsCollection = $connection->fetchPairs($query);
67+
$this->categoryPermissionsCollection = $connection->fetchPairs($select);
6868
}
6969

7070
return $this->categoryPermissionsCollection;
@@ -77,14 +77,13 @@ public function getProductPermissionsCollection()
7777
$indexResource = $this->getPermissionsIndexResource();
7878
$connection = $indexResource->getConnection();
7979

80-
$query = "
81-
SELECT product_id,
82-
GROUP_CONCAT(CONCAT(store_id, '_', customer_group_id, '_', grant_catalog_category_view) SEPARATOR ', ') AS permissions
83-
FROM {$indexResource->getTable([$indexResource->getMainTable(), 'product'])}
84-
GROUP BY product_id;
85-
";
80+
$select = $connection->select()
81+
->from($indexResource->getTable([$indexResource->getMainTable(), 'product']), [])
82+
->columns('product_id')
83+
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(store_id, '_', customer_group_id, '_', grant_catalog_category_view) SEPARATOR ', ')")])
84+
->group('product_id');
8685

87-
$this->productPermissionsCollection = $connection->fetchPairs($query);
86+
$this->productPermissionsCollection = $connection->fetchPairs($select);
8887
}
8988

9089
return $this->productPermissionsCollection;

Factory/SharedCatalogFactory.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,31 @@ public function getSharedCatalogConfig()
5959
return $this->objectManager->create('\Magento\SharedCatalog\Model\Config');
6060
}
6161

62+
private function getSharedCatalogCustomerGroups()
63+
{
64+
$sharedCatalogResource = $this->getSharedCatalogResource();
65+
$connection = $sharedCatalogResource->getConnection();
66+
67+
$select = $connection->select()
68+
->from(($sharedCatalogResource->getMainTable()), ['customer_group_id']);
69+
70+
return $connection->fetchAll($select);
71+
}
72+
6273
public function getSharedCategoryCollection()
6374
{
6475
if (!$this->sharedCategoryCollection) {
6576
$indexResource = $this->getSharedCatalogCategoryResource();
6677
$connection = $indexResource->getConnection();
6778

68-
$query = "
69-
SELECT category_id, GROUP_CONCAT(CONCAT(customer_group_id, '_', permission) SEPARATOR ',') AS permissions
70-
FROM {$indexResource->getMainTable()}
71-
WHERE customer_group_id IN (SELECT customer_group_id FROM shared_catalog)
72-
GROUP BY category_id;
73-
";
79+
$select = $connection->select()
80+
->from($indexResource->getMainTable(), [])
81+
->columns('category_id')
82+
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(customer_group_id, '_', permission) SEPARATOR ',')")])
83+
->where('customer_group_id IN (?)', $this->getSharedCatalogCustomerGroups())
84+
->group('category_id');
7485

75-
$this->sharedCategoryCollection = $connection->fetchPairs($query);
86+
$this->sharedCategoryCollection = $connection->fetchPairs($select);
7687
}
7788

7889
return $this->sharedCategoryCollection;
@@ -85,17 +96,22 @@ public function getSharedProductItemCollection()
8596
$indexResource = $this->getSharedCatalogProductItemResource();
8697
$connection = $indexResource->getConnection();
8798

88-
$query = "
89-
SELECT cpe.entity_id, GROUP_CONCAT(pi.customer_group_id SEPARATOR ',') as groups
90-
FROM {$indexResource->getMainTable()} as pi
91-
INNER JOIN {$this->getSharedCatalogResource()->getMainTable()} AS sc
92-
ON sc.customer_group_id = pi.customer_group_id
93-
LEFT JOIN {$indexResource->getTable('catalog_product_entity')} AS cpe
94-
ON pi.sku = cpe.sku
95-
GROUP BY pi.sku
96-
";
97-
98-
$productItems = $connection->fetchPairs($query);
99+
$select = $connection->select()
100+
->from(['pi' => $indexResource->getMainTable()], [])
101+
->columns('cpe.entity_id')
102+
->columns(['groups' => new \Zend_Db_Expr("GROUP_CONCAT(pi.customer_group_id SEPARATOR ',')")])
103+
->joinInner(
104+
['sc' => $this->getSharedCatalogResource()->getMainTable()],
105+
'sc.customer_group_id = pi.customer_group_id',
106+
[]
107+
)
108+
->joinLeft(
109+
['cpe' => $indexResource->getTable('catalog_product_entity')],
110+
'pi.sku = cpe.sku',
111+
[]
112+
)->group('pi.sku');
113+
114+
$productItems = $connection->fetchPairs($select);
99115
$groups = $this->getSharedCatalogGroups();
100116

101117
foreach ($productItems as $productId => $permissions) {

Helper/Entity/ProductHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ private function getSubProducts(Product $product)
550550
if ($typeInstance instanceof Configurable) {
551551
$subProducts = $typeInstance->getUsedProducts($product);
552552
} elseif ($typeInstance instanceof BundleProductType) {
553-
$subProducts = $typeInstance->getOptions($product);
553+
$subProducts = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
554554
} else { // Grouped product
555555
$subProducts = $typeInstance->getAssociatedProducts($product);
556556
}

Model/Observer/CatalogPermissions/ApplyProductPermissionsFilter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public function __construct(
3737
public function execute(Observer $observer)
3838
{
3939
$storeId = $this->storeManager->getStore()->getId();
40-
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)
41-
|| ($this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId)
42-
&& !$this->sharedCatalogFactory->isSharedCatalogEnabled($storeId))
43-
) {
40+
41+
// shared catalog is dependant on catalog permissions settings,
42+
// should only check against catalog permissions
43+
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)) {
4444
return $this;
4545
}
4646

Model/Observer/CatalogPermissions/CategoryCollectionAddPermissions.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ protected function addCatalogPermissionsData($collection, $categoryIds)
6060
}
6161
list($customerGroupId, $level) = $permission;
6262
if ($category = $collection->getItemById($categoryId)) {
63-
$category->setData('customer_group_permission_' . $customerGroupId, (($level == -2 || $level != -1
64-
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1));
63+
$category->setData(
64+
'customer_group_permission_' . $customerGroupId,
65+
$level == -2 ? 0 : 1
66+
);
6567
}
6668
}
6769
}

Model/Observer/CatalogPermissions/CategoryPermissions.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ public function execute(Observer $observer)
3737

3838
foreach ($collection as $customerGroup) {
3939
$customerGroupId = $customerGroup->getCustomerGroupId();
40-
$permissions['customer_group_' . $customerGroupId] =
41-
!is_null($category->getData('shared_catalog_permission_' . $customerGroupId))
42-
? (int) $category->getData('shared_catalog_permission_' . $customerGroupId)
43-
: (int) $category->getData('customer_group_permission_' . $customerGroupId);
40+
41+
$isVisible = (int) $this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId, $customerGroupId);
42+
if (!is_null($category->getData('shared_catalog_permission_' . $customerGroupId))) {
43+
$isVisible = (int) $category->getData('shared_catalog_permission_' . $customerGroupId);
44+
} elseif (!is_null($category->getData('customer_group_permission_' . $customerGroupId))) {
45+
$isVisible = (int) $category->getData('customer_group_permission_' . $customerGroupId);
46+
}
47+
48+
$permissions['customer_group_' . $customerGroupId] = $isVisible;
4449
}
4550

4651
$transport->setData('catalog_permissions', $permissions);

Model/Observer/CatalogPermissions/ProductCollectionAddPermissions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ protected function addProductPermissionsData($additionalData, $productIds, $stor
7070
list($permissionStoreId, $customerGroupId, $level) = $permission;
7171
if ($permissionStoreId == $storeId) {
7272
$additionalData->addProductData($productId, [
73-
'customer_group_permission_' . $customerGroupId => (($level == -2 || $level != -1
74-
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1),
73+
'customer_group_permission_' . $customerGroupId => ($level == -2 ? 0 : 1),
7574
]);
7675
}
7776
}

Model/Observer/CatalogPermissions/ProductPermissions.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ public function execute(Observer $observer)
3838
foreach ($collection as $customerGroup) {
3939
$customerGroupId = $customerGroup->getCustomerGroupId();
4040

41-
$permissions['customer_group_' . $customerGroupId] =
42-
!is_null($product->getData('customer_group_permission_' . $customerGroupId))
43-
? (int) $product->getData('customer_group_permission_' . $customerGroupId)
44-
: (int) $product->getData('shared_catalog_permission_' . $customerGroupId);
41+
$isVisible = (int) $this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId, $customerGroupId);
42+
if (!is_null($product->getData('shared_catalog_permission_' . $customerGroupId))) {
43+
$isVisible = (int) $product->getData('shared_catalog_permission_' . $customerGroupId);
44+
} elseif (!is_null($product->getData('customer_group_permission_' . $customerGroupId))) {
45+
$isVisible = (int) $product->getData('customer_group_permission_' . $customerGroupId);
46+
}
47+
48+
$permissions['customer_group_' . $customerGroupId] = $isVisible;
4549
}
4650

4751
if (count($permissions)) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Observer;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Framework\Event\Observer;
7+
use Magento\Framework\Event\ObserverInterface;
8+
use Magento\Framework\Message\ManagerInterface;
9+
10+
class CleanCatalogImagesCacheAfter implements ObserverInterface
11+
{
12+
/** @var ConfigHelper */
13+
private $configHelper;
14+
15+
/** @var ManagerInterface */
16+
private $messageManager;
17+
18+
/**
19+
* @param ConfigHelper $configHelper
20+
* @param ManagerInterface $messageManager
21+
*/
22+
public function __construct(
23+
ConfigHelper $configHelper,
24+
ManagerInterface $messageManager
25+
) {
26+
$this->configHelper = $configHelper;
27+
$this->messageManager = $messageManager;
28+
}
29+
30+
/**
31+
* Add Notice for Product Reindexing after image flush
32+
*
33+
* @param Observer $observer
34+
*/
35+
public function execute(Observer $observer)
36+
{
37+
$this->messageManager->addWarningMessage(__('
38+
Algolia Warning: The image cache has been cleared.
39+
All indexed image links will become invalid because the file will be nonexistent.
40+
Please run a full reindex of your catalog data to resolve broken images in your Algolia Search.
41+
'));
42+
}
43+
}

etc/adminhtml/events.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@
5757
<event name="algolia_after_create_category_object">
5858
<observer name="algoliasearch_category_permissions" instance="Algolia\AlgoliaSearch\Model\Observer\CatalogPermissions\CategoryPermissions" />
5959
</event>
60+
61+
<event name="clean_catalog_images_cache_after">
62+
<observer name="algoliasearch_flush_catalog_image_cache_after" instance="Algolia\AlgoliaSearch\Model\Observer\CleanCatalogImagesCacheAfter" />
63+
</event>
64+
6065
</config>

0 commit comments

Comments
 (0)