44 */
55
66declare (strict_types=1 );
7+
78namespace 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 ;
1111use Magento \Framework \Exception \NoSuchEntityException ;
1212use Magento \Store \Model \App \Emulation ;
1313use Magento \Store \Model \Store ;
1414use 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
1619class 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 }
0 commit comments