4
4
*/
5
5
6
6
declare (strict_types=1 );
7
+
7
8
namespace Aligent \PrerenderIo \Model \Url ;
8
9
9
- use Magento \Catalog \Model \Product ;
10
- use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
10
+ use Aligent \PrerenderIo \Helper \Config ;
11
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
12
use Magento \Store \Model \App \Emulation ;
13
13
use Magento \Store \Model \Store ;
14
14
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 ;
15
18
16
19
class GetUrlsForProducts
17
20
{
18
- /** @var CollectionFactory */
19
- private CollectionFactory $ productCollectionFactory ;
20
- /** @var StoreManagerInterface */
21
- private StoreManagerInterface $ storeManager ;
22
- /** @var Emulation */
23
- private Emulation $ emulation ;
24
-
25
21
/**
26
- *
27
- * @param CollectionFactory $productCollectionFactory
28
22
* @param StoreManagerInterface $storeManager
29
23
* @param Emulation $emulation
24
+ * @param UrlFinderInterface $urlFinder
25
+ * @param Config $prerenderConfigHelper
30
26
*/
31
27
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
35
32
) {
36
- $ this ->productCollectionFactory = $ productCollectionFactory ;
37
- $ this ->storeManager = $ storeManager ;
38
- $ this ->emulation = $ emulation ;
39
33
}
40
34
41
35
/**
@@ -47,34 +41,40 @@ public function __construct(
47
41
*/
48
42
public function execute (array $ productIds , int $ storeId ): array
49
43
{
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
-
60
44
try {
61
45
/** @var Store $store */
62
46
$ store = $ this ->storeManager ->getStore ($ storeId );
63
47
} catch (NoSuchEntityException $ e ) {
64
48
return [];
65
49
}
66
50
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
+
67
61
$ this ->emulation ->startEnvironmentEmulation ($ storeId );
68
62
$ 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 ()) {
73
71
continue ;
74
72
}
75
73
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 , '/ ' );
78
78
} catch (NoSuchEntityException $ e ) {
79
79
continue ;
80
80
}
0 commit comments