Skip to content

Commit c14dbc5

Browse files
committed
Merge branch 'ACP2E-2629' of https://github.com/magento-l3/magento2ce into PR-12-19-2023
2 parents 2d5c946 + c5de1f7 commit c14dbc5

File tree

10 files changed

+556
-80
lines changed

10 files changed

+556
-80
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
namespace Magento\CatalogUrlRewrite\Model;
19+
20+
use Magento\Catalog\Api\Data\CategoryInterface;
21+
use Magento\Catalog\Api\Data\ProductInterface;
22+
use Magento\Eav\Model\ResourceModel\AttributeValue;
23+
use Magento\Framework\EntityManager\MetadataPool;
24+
use Magento\Store\Model\Store;
25+
26+
class GetAttributeByStore
27+
{
28+
29+
/**
30+
* @param AttributeValue $attributeValue
31+
* @param MetadataPool $metadataPool
32+
*/
33+
public function __construct(
34+
private readonly AttributeValue $attributeValue,
35+
private readonly MetadataPool $metadataPool
36+
) {
37+
}
38+
39+
/**
40+
* Get attribute values by store
41+
*
42+
* @param ProductInterface|CategoryInterface $entity
43+
* @param string $attributeCode
44+
* @param array $storeIds
45+
* @return array
46+
*/
47+
public function execute(
48+
ProductInterface|CategoryInterface $entity,
49+
string $attributeCode,
50+
array $storeIds = [Store::DEFAULT_STORE_ID]
51+
): array {
52+
$storeIds = array_merge($entity->getStoreIds(), $storeIds);
53+
$entityType = $entity instanceof CategoryInterface ?
54+
CategoryInterface::class : ProductInterface::class;
55+
56+
try {
57+
$metadata = $this->metadataPool->getMetadata($entityType);
58+
$attributeRows = $this->attributeValue->getValues(
59+
$entityType,
60+
(int)$entity->getData($metadata->getLinkField()),
61+
[$attributeCode],
62+
$storeIds
63+
);
64+
} catch (\Exception) {
65+
$attributeRows = [];
66+
}
67+
68+
$attributeByStore = [];
69+
foreach ($attributeRows as $row) {
70+
$attributeByStore[$row['store_id']] = $row['value'];
71+
}
72+
return $attributeByStore;
73+
}
74+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
namespace Magento\CatalogUrlRewrite\Model;
19+
20+
use Magento\Catalog\Api\Data\ProductInterface;
21+
use Magento\Catalog\Model\Product\Visibility;
22+
use Magento\Store\Model\Store;
23+
24+
class GetVisibleForStores
25+
{
26+
27+
/**
28+
* @param GetAttributeByStore $attributeByStore
29+
*/
30+
public function __construct(
31+
private readonly GetAttributeByStore $attributeByStore
32+
) {
33+
}
34+
35+
/**
36+
* Get all store ids for which the product is visible
37+
*
38+
* @param ProductInterface $product
39+
* @return array
40+
*/
41+
public function execute(ProductInterface $product): array
42+
{
43+
$visibilityByStore = $this->attributeByStore->execute($product, 'visibility');
44+
45+
$storeIds = array_merge($product->getStoreIds(), [Store::DEFAULT_STORE_ID]) ;
46+
47+
$visibleStoreIds = [];
48+
foreach ($storeIds as $storeId) {
49+
if (!isset($visibilityByStore[$storeId]) && isset($visibilityByStore[0]) &&
50+
(int)$visibilityByStore[0] !== Visibility::VISIBILITY_NOT_VISIBLE
51+
|| isset($visibilityByStore[$storeId]) &&
52+
(int)$visibilityByStore[$storeId] !== Visibility::VISIBILITY_NOT_VISIBLE) {
53+
$visibleStoreIds[] = (int)$storeId;
54+
}
55+
}
56+
57+
return $visibleStoreIds;
58+
}
59+
}

app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
6868
if ($anchorCategoryIds) {
6969
foreach ($anchorCategoryIds as $anchorCategoryId) {
7070
$anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);
71-
if ((int)$anchorCategory->getParentId() === Category::TREE_ROOT_ID) {
71+
if (in_array(
72+
(int)$anchorCategory->getParentId(),
73+
[Category::TREE_ROOT_ID, Category::ROOT_CATEGORY_ID]
74+
)) {
7275
continue;
7376
}
7477
$urls[] = $this->urlRewriteFactory->create()

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class ProductScopeRewriteGenerator
8484
*/
8585
private $productRepository;
8686

87+
/**
88+
* @var GetVisibleForStores|mixed
89+
*/
90+
private mixed $visibleForStores;
91+
8792
/**
8893
* @param StoreViewService $storeViewService
8994
* @param StoreManagerInterface $storeManager
@@ -92,10 +97,11 @@ class ProductScopeRewriteGenerator
9297
* @param CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator
9398
* @param CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator
9499
* @param AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator
95-
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
100+
* @param MergeDataProviderFactory|null $mergeDataProviderFactory
96101
* @param CategoryRepositoryInterface|null $categoryRepository
97102
* @param ScopeConfigInterface|null $config
98103
* @param ProductRepositoryInterface|null $productRepository
104+
* @param GetVisibleForStores|null $visibleForStores
99105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
100106
*/
101107
public function __construct(
@@ -109,7 +115,8 @@ public function __construct(
109115
MergeDataProviderFactory $mergeDataProviderFactory = null,
110116
CategoryRepositoryInterface $categoryRepository = null,
111117
ScopeConfigInterface $config = null,
112-
ProductRepositoryInterface $productRepository = null
118+
ProductRepositoryInterface $productRepository = null,
119+
GetVisibleForStores $visibleForStores = null
113120
) {
114121
$this->storeViewService = $storeViewService;
115122
$this->storeManager = $storeManager;
@@ -127,6 +134,8 @@ public function __construct(
127134
$this->config = $config ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
128135
$this->productRepository = $productRepository ?:
129136
ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
137+
$this->visibleForStores = $visibleForStores ??
138+
ObjectManager::getInstance()->get(GetVisibleForStores::class);
130139
}
131140

132141
/**
@@ -152,6 +161,7 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
152161
{
153162
$productId = $product->getEntityId();
154163
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
164+
$visibleForStores = $this->visibleForStores->execute($product);
155165

156166
foreach ($product->getStoreIds() as $id) {
157167
if (!$this->isGlobalScope($id)) {
@@ -160,20 +170,30 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
160170
$productId,
161171
Product::ENTITY
162172
)) {
163-
$mergeDataProvider->merge(
164-
$this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId, true)
165-
);
173+
if (count($visibleForStores) == 0 || in_array((int)$id, $visibleForStores)) {
174+
$mergeDataProvider->merge(
175+
$this->generateForSpecificStoreView(
176+
$id,
177+
$productCategories,
178+
$product,
179+
$rootCategoryId,
180+
true
181+
)
182+
);
183+
}
166184
} else {
167-
$scopedProduct = $this->productRepository->getById($productId, false, $id);
168-
$mergeDataProvider->merge(
169-
$this->generateForSpecificStoreView(
170-
$id,
171-
$productCategories,
172-
$scopedProduct,
173-
$rootCategoryId,
174-
true
175-
)
176-
);
185+
if (count($visibleForStores) == 0 || in_array((int)$id, $visibleForStores)) {
186+
$scopedProduct = $this->productRepository->getById($productId, false, $id);
187+
$mergeDataProvider->merge(
188+
$this->generateForSpecificStoreView(
189+
$id,
190+
$productCategories,
191+
$scopedProduct,
192+
$rootCategoryId,
193+
true
194+
)
195+
);
196+
}
177197
}
178198
}
179199
}

0 commit comments

Comments
 (0)