Hi how can I retrieve the value of a category url in the backend? I’m expecting the url value to be equivalent to the url in the frontend and NOT the url in the backend. Is there a way to achieve this in Magento 2?
Hi how can I retrieve the value of a category url in the backend? I’m expecting the url value to be equivalent to the url in the frontend and NOT the url in the backend. Is there a way to achieve this in Magento 2?
2
Answers
Ok I found the answer for this. The solution is to use an App/Emulator that is available in Magento. The idea is to start the emulation before you execute the Frontend Catgory URL retrieval that you want and close the emulation when its done.
Here are the links on how to implement it App Emulation. Didn't know it was this simple to solve the issue. Below is how my code looks like
So the idea here is to instantiate the emulator and make magento think that you are going to modify or perform like you are in the frontend hence the code MagentoFrameworkAppArea::AREA_FRONTEND when you close the environment emulation it will go back to how it was whether you are in adminhtml or frontend
In order to get the category url you need to use the
MagentoCatalogModelCategoryRepository
functiongetUrl()
like so: