Skip to content

Commit 6ba80fb

Browse files
Merge pull request #13 from aligent/fix/non-canonical-product-urls-recaching
fix for non-canonical URL being sent for product recaching
2 parents 959f78f + f2c024b commit 6ba80fb

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

Helper/Config.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
declare(strict_types=1);
7+
78
namespace Aligent\PrerenderIo\Helper;
89

910
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -13,6 +14,7 @@ class Config
1314
{
1415
private const XML_PATH_RECACHE_ENABLED = 'system/prerender_io/enabled';
1516
private const XML_PATH_PRERENDER_TOKEN = 'system/prerender_io/token';
17+
private const XML_PATH_PRERENDER_USE_PRODUCT_CANONICAL_URL = 'system/prerender_io/use_product_canonical_url';
1618

1719
/** @var ScopeConfigInterface */
1820
private ScopeConfigInterface $scopeConfig;
@@ -36,7 +38,7 @@ public function isRecacheEnabled(?int $storeId = null): bool
3638
{
3739
return $this->scopeConfig->isSetFlag(
3840
self::XML_PATH_RECACHE_ENABLED,
39-
ScopeInterface::SCOPE_STORE,
41+
ScopeInterface::SCOPE_STORES,
4042
$storeId
4143
);
4244
}
@@ -51,7 +53,22 @@ public function getToken(?int $storeId = null): ?string
5153
{
5254
return $this->scopeConfig->getValue(
5355
self::XML_PATH_PRERENDER_TOKEN,
54-
ScopeInterface::SCOPE_STORE,
56+
ScopeInterface::SCOPE_STORES,
57+
$storeId
58+
);
59+
}
60+
61+
/**
62+
* Return if product canonical url configuration is enabled or not
63+
*
64+
* @param int|null $storeId
65+
* @return string|null
66+
*/
67+
public function isUseProductCanonicalUrlEnabled(?int $storeId = null): bool
68+
{
69+
return $this->scopeConfig->isSetFlag(
70+
self::XML_PATH_PRERENDER_USE_PRODUCT_CANONICAL_URL,
71+
ScopeInterface::SCOPE_STORES,
5572
$storeId
5673
);
5774
}

Model/Url/GetUrlsForProducts.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,32 @@
44
*/
55

66
declare(strict_types=1);
7+
78
namespace Aligent\PrerenderIo\Model\Url;
89

9-
use Magento\Catalog\Model\Product;
10-
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
10+
use Aligent\PrerenderIo\Helper\Config;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Store\Model\App\Emulation;
1313
use Magento\Store\Model\Store;
1414
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
16+
use Magento\UrlRewrite\Model\UrlFinderInterface;
17+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1518

1619
class GetUrlsForProducts
1720
{
18-
/** @var CollectionFactory */
19-
private CollectionFactory $productCollectionFactory;
20-
/** @var StoreManagerInterface */
21-
private StoreManagerInterface $storeManager;
22-
/** @var Emulation */
23-
private Emulation $emulation;
24-
2521
/**
26-
*
27-
* @param CollectionFactory $productCollectionFactory
2822
* @param StoreManagerInterface $storeManager
2923
* @param Emulation $emulation
24+
* @param UrlFinderInterface $urlFinder
25+
* @param Config $prerenderConfigHelper
3026
*/
3127
public function __construct(
32-
CollectionFactory $productCollectionFactory,
33-
StoreManagerInterface $storeManager,
34-
Emulation $emulation
28+
private readonly StoreManagerInterface $storeManager,
29+
private readonly Emulation $emulation,
30+
private readonly UrlFinderInterface $urlFinder,
31+
private readonly Config $prerenderConfigHelper
3532
) {
36-
$this->productCollectionFactory = $productCollectionFactory;
37-
$this->storeManager = $storeManager;
38-
$this->emulation = $emulation;
3933
}
4034

4135
/**
@@ -47,34 +41,40 @@ public function __construct(
4741
*/
4842
public function execute(array $productIds, int $storeId): array
4943
{
50-
$productCollection = $this->productCollectionFactory->create();
51-
// do not ignore out of stock products
52-
$productCollection->setFlag('has_stock_status_filter', true);
53-
// if array of product ids is empty, just load all products
54-
if (!empty($productIds)) {
55-
$productCollection->addIdFilter($productIds);
56-
}
57-
$productCollection->setStoreId($storeId);
58-
$productCollection->addUrlRewrite();
59-
6044
try {
6145
/** @var Store $store */
6246
$store = $this->storeManager->getStore($storeId);
6347
} catch (NoSuchEntityException $e) {
6448
return [];
6549
}
6650

51+
$useProductCanonical = $this->prerenderConfigHelper->isUseProductCanonicalUrlEnabled($storeId);
52+
53+
$findByData = [
54+
UrlRewrite::ENTITY_TYPE => Rewrite::ENTITY_TYPE_PRODUCT,
55+
UrlRewrite::STORE_ID => $storeId,
56+
UrlRewrite::ENTITY_ID => $productIds
57+
];
58+
59+
$urlRewrites = $this->urlFinder->findAllByData($findByData);
60+
6761
$this->emulation->startEnvironmentEmulation($storeId);
6862
$urls = [];
69-
/** @var Product $product */
70-
foreach ($productCollection as $product) {
71-
$urlPath = $product->getData('request_path');
72-
if (empty($urlPath)) {
63+
64+
foreach ($urlRewrites as $urlRewrite) {
65+
if (empty($urlRewrite->getRequestPath())) {
66+
continue;
67+
}
68+
69+
// Ignore the product URL with category path.
70+
if ($useProductCanonical && $urlRewrite->getMetadata()) {
7371
continue;
7472
}
7573
try {
76-
// remove trailing slashes from urls
77-
$urls[] = rtrim($store->getUrl($urlPath), '/');
74+
// Generate direct URL to avoid Magento stopping at the 4th level onwards
75+
$url = $store->getUrl('', ['_direct' => $urlRewrite->getRequestPath()]);
76+
// Remove trailing slashes from urls
77+
$urls[] = rtrim($url, '/');
7878
} catch (NoSuchEntityException $e) {
7979
continue;
8080
}

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<field id="enabled">1</field>
2121
</depends>
2222
</field>
23+
<field id="use_product_canonical_url" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
24+
<label>Use Canonical URL For Products</label>
25+
<comment>Ignore the non-canonical URLs which includes category paths for products.</comment>
26+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
27+
</field>
2328
</group>
2429
</section>
2530
</system>

etc/config.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
4+
<default>
5+
<system>
6+
<prerender_io>
7+
<use_product_canonical_url>0</use_product_canonical_url>
8+
</prerender_io>
9+
</system>
10+
</default>
11+
</config>

0 commit comments

Comments
 (0)