From 28fe236fb28b80e5ba1b9bb65ef3fbeb88c1174d Mon Sep 17 00:00:00 2001 From: "ryan.jehan" Date: Fri, 13 Oct 2023 13:21:09 +1030 Subject: [PATCH] Updated the URL fetch from store scope level --- Model/Url/GetUrlsForCategories.php | 15 ++++++++++++--- Model/Url/GetUrlsForProducts.php | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Model/Url/GetUrlsForCategories.php b/Model/Url/GetUrlsForCategories.php index 1a57a9b..569a245 100644 --- a/Model/Url/GetUrlsForCategories.php +++ b/Model/Url/GetUrlsForCategories.php @@ -9,12 +9,15 @@ use Magento\Catalog\Model\Category; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Url; use Magento\Store\Model\App\Emulation; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; class GetUrlsForCategories { + private const DELIMETER = "?"; + /** @var CollectionFactory */ private CollectionFactory $categoryCollectionFactory; /** @var StoreManagerInterface */ @@ -22,6 +25,8 @@ class GetUrlsForCategories /** @var Emulation */ private Emulation $emulation; + private Url $url; + /** * * @param CollectionFactory $categoryCollectionFactory @@ -31,11 +36,13 @@ class GetUrlsForCategories public function __construct( CollectionFactory $categoryCollectionFactory, StoreManagerInterface $storeManager, - Emulation $emulation + Emulation $emulation, + Url $url ) { $this->categoryCollectionFactory = $categoryCollectionFactory; $this->storeManager = $storeManager; $this->emulation = $emulation; + $this->url = $url; } /** @@ -71,8 +78,10 @@ public function execute(array $categoryIds, int $storeId): array continue; } try { - // remove trailing slashes from urls - $urls[] = rtrim($store->getUrl($urlPath), '/'); + $url = $this->url->getUrl($urlPath, ['_scope_to_url' => true]); + + // remove trailing slashes and parameters from the url + $urls[] = substr($url, 0, strrpos($url, '/')); } catch (NoSuchEntityException $e) { continue; } diff --git a/Model/Url/GetUrlsForProducts.php b/Model/Url/GetUrlsForProducts.php index ef818d7..19f9817 100644 --- a/Model/Url/GetUrlsForProducts.php +++ b/Model/Url/GetUrlsForProducts.php @@ -9,12 +9,15 @@ use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Url; use Magento\Store\Model\App\Emulation; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; class GetUrlsForProducts { + private const DELIMETER = "?"; + /** @var CollectionFactory */ private CollectionFactory $productCollectionFactory; /** @var StoreManagerInterface */ @@ -22,6 +25,8 @@ class GetUrlsForProducts /** @var Emulation */ private Emulation $emulation; + private Url $url; + /** * * @param CollectionFactory $productCollectionFactory @@ -31,11 +36,13 @@ class GetUrlsForProducts public function __construct( CollectionFactory $productCollectionFactory, StoreManagerInterface $storeManager, - Emulation $emulation + Emulation $emulation, + Url $url ) { $this->productCollectionFactory = $productCollectionFactory; $this->storeManager = $storeManager; $this->emulation = $emulation; + $this->url = $url; } /** @@ -73,8 +80,10 @@ public function execute(array $productIds, int $storeId): array continue; } try { - // remove trailing slashes from urls - $urls[] = rtrim($store->getUrl($urlPath), '/'); + $url = $this->url->getUrl($urlPath, ['_scope_to_url' => true]); + + // remove trailing slashes and parameters from the url + $urls[] = substr($url, 0, strrpos($url, '/')); } catch (NoSuchEntityException $e) { continue; }