Skip to content

Commit b056fc7

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
- missing redirects are restored
1 parent bbbea3f commit b056fc7

File tree

1 file changed

+40
-22
lines changed
  • app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite

1 file changed

+40
-22
lines changed

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
namespace Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
99

1010
use Magento\Backend\App\Action\Context;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
1211
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
12+
use Magento\CatalogUrlRewrite\Model\Products\AppendUrlRewritesToProducts;
1313
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
1414
use Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator;
1515
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1616
use Magento\Framework\Exception\LocalizedException;
1717
use Magento\UrlRewrite\Model\UrlFinderInterface;
1818
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1919

20+
/**
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
2023
class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implements HttpPostActionInterface
2124
{
2225
/**
@@ -40,33 +43,33 @@ class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implemen
4043
protected $urlFinder;
4144

4245
/**
43-
* @var ProductRepositoryInterface
46+
* @var AppendUrlRewritesToProducts
4447
*/
45-
protected ProductRepositoryInterface $productRepository;
48+
protected AppendUrlRewritesToProducts $productAppendRewrites;
4649

4750
/**
4851
* @param Context $context
4952
* @param ProductUrlPathGenerator $productUrlPathGenerator
5053
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
5154
* @param CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
5255
* @param UrlFinderInterface $urlFinder
53-
* @param ProductRepositoryInterface|null $productRepository
56+
* @param AppendUrlRewritesToProducts|null $productAppendRewrites
5457
*/
5558
public function __construct(
56-
\Magento\Backend\App\Action\Context $context,
57-
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
59+
\Magento\Backend\App\Action\Context $context,
60+
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
5861
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
59-
\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
60-
UrlFinderInterface $urlFinder,
61-
ProductRepositoryInterface $productRepository = null
62+
\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
63+
UrlFinderInterface $urlFinder,
64+
AppendUrlRewritesToProducts $productAppendRewrites = null
6265
) {
6366
parent::__construct($context);
6467
$this->productUrlPathGenerator = $productUrlPathGenerator;
6568
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
6669
$this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
6770
$this->urlFinder = $urlFinder;
68-
$this->productRepository = $productRepository ?:
69-
\Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
71+
$this->productAppendRewrites = $productAppendRewrites ?:
72+
\Magento\Framework\App\ObjectManager::getInstance()->create(AppendUrlRewritesToProducts::class);
7073
}
7174

7275
/**
@@ -88,18 +91,18 @@ protected function _handleCatalogUrlRewrite($model)
8891
$model->setMetadata(['category_id' => $categoryId]);
8992
}
9093
}
91-
$model->setTargetPath($this->getTargetPath($model));
94+
$model->setTargetPath($this->generateTargetPath($model));
9295
}
9396
}
9497

9598
/**
96-
* Get Target Path
99+
* Generate Target Path
97100
*
98101
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
99102
* @return string
100103
* @throws \Magento\Framework\Exception\LocalizedException
101104
*/
102-
protected function getTargetPath($model)
105+
protected function generateTargetPath($model)
103106
{
104107
$targetPath = $this->getCanonicalTargetPath();
105108
if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
@@ -111,14 +114,29 @@ protected function getTargetPath($model)
111114
];
112115
$rewrite = $this->urlFinder->findOneByData($data);
113116
if (!$rewrite) {
114-
$check = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT ?
115-
$this->_getProduct()->canBeShowInCategory($this->_getCategory()->getId()) :
116-
$this->_getCategory()->getStoreId() == $model->getStoreId();
117-
if (false === $check) {
118-
$message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
119-
? __("The selected product isn't associated with the selected store or category.")
120-
: __("The selected category isn't associated with the selected store.");
121-
throw new LocalizedException($message);
117+
if ($model->getEntityType() === self::ENTITY_TYPE_PRODUCT) {
118+
$this->productAppendRewrites->execute(
119+
[$this->_getProduct()],
120+
[$this->getRequest()->getParam('store_id', 0)]
121+
);
122+
$rewrite = $this->urlFinder->findOneByData($data);
123+
if (!$rewrite) {
124+
throw new LocalizedException(
125+
__(
126+
"The selected product isn't associated with the selected store or category."
127+
)
128+
);
129+
}
130+
$targetPath = $rewrite->getRequestPath();
131+
if ($rewrite->getRequestPath() == $model->getRequestPath() &&
132+
$rewrite->getStoreId() == $model->getStoreId()) {
133+
$obsoleteRewrite = $this->_objectManager->create(\Magento\UrlRewrite\Model\UrlRewrite::class);
134+
$obsoleteRewrite->load($rewrite->getUrlRewriteId());
135+
$obsoleteRewrite->delete();
136+
}
137+
} else {
138+
throw new
139+
LocalizedException(__("The selected category isn't associated with the selected store."));
122140
}
123141
} else {
124142
$targetPath = $rewrite->getRequestPath();

0 commit comments

Comments
 (0)