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; }