How to Get All Product URLs in Magento 2
The admin can create six different types of products in Magento 2 store and each time a product is created, a unique URL is created for that product page.
Now, when you are working with HTML sitemap in your Magento 2 store, you might need the list of all these product URLs. Manually creating this list is a tedious process and prone to error.
Hence, I have posted a programmatic solution to get all product URLs in Magento 2 store.
Using this solution, one can get all product URLs and use them while updating the product data, creating HTML sitemap for navigation purpose or simply track the URLs of the store.
Also, a store owner may maintain and update such a product list from time to time to monitor the store expansion and product performance.
In all such scenarios, follow the programmatic solution given below to get all product URLs in Magento 2.
Note: You might also find the solution to get all category URLs in Magento 2 helpful if you are creating HTML sitemap and landed to my post!
Steps to Get All Product URLs in Magento 2
- Create registration.php at Vendor/Module.
123456789<?phpuse Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE,'Vendor_Module',__DIR__); - Create module.xml at Vendor/Module/etc.
123456<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"></module></config> - Create ProductPageUrls.php at Vendor/Module/Helper.
1234567891011121314151617181920212223242526272829303132333435363738394041424344<?phpnamespace Vendor\Module\Helper;use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;use Magento\Framework\App\Config\ScopeConfigInterface;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;class ProductPageUrls extends AbstractHelper{const PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';/*** @var ProductCollectionFactory*/private $productCollectionFactory;public function __construct(Context $context, ProductCollectionFactory $productCollectionFactory){$this->productCollectionFactory = $productCollectionFactory;parent::__construct($context);}public function getProductUrls(){$productUrls = [];$products = $this->productCollectionFactory->create();$products->addAttributeToSelect('url_key');foreach ($products as $product) {if (!is_null($product->getData('url_key'))) {$productUrls[] = rtrim($this->_getUrl($product->getData('url_key')), '/') .$this->getConfigData(self::PRODUCT_URL_SUFFIX);}}return $productUrls;}public function getConfigData($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = null){return $this->scopeConfig->getValue($path, $scope, $scopeId);}}
That’s it.
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo in Magento 2
Solved: Failed to Set ini Option “session.cookie_samesite” to Value “Lax” in Magento 2.3.6-p1
Next