From 28fe236fb28b80e5ba1b9bb65ef3fbeb88c1174d Mon Sep 17 00:00:00 2001 From: "ryan.jehan" Date: Fri, 13 Oct 2023 13:21:09 +1030 Subject: [PATCH 1/2] 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; } From 72aeec07cc62a172b1f830c26fcc8769791c239c Mon Sep 17 00:00:00 2001 From: "ryan.jehan" Date: Mon, 16 Oct 2023 11:03:42 +1030 Subject: [PATCH 2/2] updated CS changes --- Model/Url/GetUrlsForCategories.php | 1 + Model/Url/GetUrlsForProducts.php | 1 + 2 files changed, 2 insertions(+) diff --git a/Model/Url/GetUrlsForCategories.php b/Model/Url/GetUrlsForCategories.php index 569a245..e8696d5 100644 --- a/Model/Url/GetUrlsForCategories.php +++ b/Model/Url/GetUrlsForCategories.php @@ -25,6 +25,7 @@ class GetUrlsForCategories /** @var Emulation */ private Emulation $emulation; + /** @var Url */ private Url $url; /** diff --git a/Model/Url/GetUrlsForProducts.php b/Model/Url/GetUrlsForProducts.php index 19f9817..e2ba4b6 100644 --- a/Model/Url/GetUrlsForProducts.php +++ b/Model/Url/GetUrlsForProducts.php @@ -25,6 +25,7 @@ class GetUrlsForProducts /** @var Emulation */ private Emulation $emulation; + /** @var Url */ private Url $url; /**