Skip to content

Commit

Permalink
Merge pull request #5 from aligent/feature/updated-url-fetch-using-scope
Browse files Browse the repository at this point in the history
Updated the URL fetch from store scope level
  • Loading branch information
aligent-lturner authored Oct 16, 2023
2 parents 0db2f36 + 72aeec0 commit 1fb6078
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 13 additions & 3 deletions Model/Url/GetUrlsForCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
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 */
private StoreManagerInterface $storeManager;
/** @var Emulation */
private Emulation $emulation;

/** @var Url */
private Url $url;

/**
*
* @param CollectionFactory $categoryCollectionFactory
Expand All @@ -31,11 +37,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;
}

/**
Expand Down Expand Up @@ -71,8 +79,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;
}
Expand Down
16 changes: 13 additions & 3 deletions Model/Url/GetUrlsForProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
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 */
private StoreManagerInterface $storeManager;
/** @var Emulation */
private Emulation $emulation;

/** @var Url */
private Url $url;

/**
*
* @param CollectionFactory $productCollectionFactory
Expand All @@ -31,11 +37,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;
}

/**
Expand Down Expand Up @@ -73,8 +81,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;
}
Expand Down

0 comments on commit 1fb6078

Please sign in to comment.