Skip to content

Commit bf69016

Browse files
committed
ACP2E-1783: Unable to create 301/302 redirect for product with a category path when Generate "category/product" URL Rewrites set to Yes
- improved solution
1 parent 70848f5 commit bf69016

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Products/AppendUrlRewritesToProducts.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,6 @@ public function getProductUrlRewrites(ProductInterface $product, array $stores):
118118
return $urls;
119119
}
120120

121-
/**
122-
* Replaces given product URL rewrites
123-
*
124-
* @param array $rewrites
125-
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
126-
* @throws UrlAlreadyExistsException
127-
*/
128-
public function saveProductUrlRewrites(array $rewrites)
129-
{
130-
return $this->urlPersist->replace($rewrites);
131-
}
132-
133121
/**
134122
* Generate urls for specific store
135123
*

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Products/AppendUrlRewritesToProductsTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,6 @@ protected function setUp(): void
6767
parent::setUp();
6868
}
6969

70-
/**
71-
* @return void
72-
* @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
73-
*/
74-
public function testSaveProductUrlRewrites(): void
75-
{
76-
$rewrites = ['test'];
77-
$this->urlPersist->expects($this->once())->method('replace')->with($rewrites);
78-
$this->append->saveProductUrlRewrites($rewrites);
79-
}
80-
8170
/**
8271
* @return void
8372
* @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException

app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ protected function generateTargetPath($model)
118118
])) {
119119
$targetPath = $rewrite->getRequestPath();
120120
} else {
121+
$check = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT ?
122+
$this->_getProduct()->canBeShowInCategory($this->_getCategory()->getId()) &&
123+
in_array($model->getStoreId(), $this->_getProduct()->getStoreIds()) :
124+
$this->_getCategory()->getStoreId() == $model->getStoreId();
125+
if (false === $check) {
126+
throw new LocalizedException(
127+
$model->getEntityType() === self::ENTITY_TYPE_PRODUCT
128+
? __("The selected product isn't associated with the selected store or category.")
129+
: __("The selected category isn't associated with the selected store.")
130+
);
131+
}
132+
121133
if ($model->getEntityType() === self::ENTITY_TYPE_PRODUCT) {
122134
$productRewrites = $this->productAppendRewrites->getProductUrlRewrites(
123135
$this->_getProduct(),
@@ -126,22 +138,21 @@ protected function generateTargetPath($model)
126138
$productRewrites = array_merge(...$productRewrites);
127139
/** @var UrlRewrite $rewrite */
128140
foreach ($productRewrites as $rewrite) {
129-
if ($rewrite->getTargetPath() == $model->getTargetPath()) {
130-
$targetPath = $rewrite->getRequestPath();
131-
} else {
132-
$this->missingRewrites[] = $rewrite;
141+
if ($rewrite->getRequestPath() != $model->getRequestPath()) {
142+
$missingRewrite = $this->_objectManager->create(\Magento\UrlRewrite\Model\UrlRewrite::class);
143+
$missingRewrite->setEntityType(self::ENTITY_TYPE_PRODUCT)
144+
->setRequestPath($rewrite->getRequestPath())
145+
->setTargetPath($rewrite->getTargetPath())
146+
->setRedirectType($rewrite->getRedirectType())
147+
->setStoreId($rewrite->getStoreId())
148+
->setDescription($rewrite->getDescription())
149+
->setMetadata($rewrite->getMetadata());
150+
$this->missingRewrites[] = $missingRewrite;
151+
if ($rewrite->getTargetPath() == $targetPath) {
152+
$targetPath = $rewrite->getRequestPath();
153+
}
133154
}
134155
}
135-
if (!$targetPath) {
136-
throw new LocalizedException(
137-
__(
138-
"The selected product isn't associated with the selected store or category."
139-
)
140-
);
141-
}
142-
} else {
143-
throw new
144-
LocalizedException(__("The selected category isn't associated with the selected store."));
145156
}
146157
}
147158
}
@@ -215,8 +226,11 @@ public function execute()
215226
$this->_handleCatalogUrlRewrite($model);
216227
$model->save();
217228
if (!empty($this->missingRewrites)) {
218-
$this->productAppendRewrites->saveProductUrlRewrites($this->missingRewrites);
229+
foreach ($this->missingRewrites as $missingRewrite) {
230+
$missingRewrite->save();
231+
}
219232
}
233+
220234
$this->messageManager->addSuccess(__('The URL Rewrite has been saved.'));
221235
$this->_redirect('adminhtml/*/');
222236
return;

0 commit comments

Comments
 (0)