VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: Ghost on March 18, 2025, 07:59:31 AM

Title: Building frontend URLs from backend doesn't work
Post by: Ghost on March 18, 2025, 07:59:31 AM
Building frontend SEF URLs using Joomla\CMS\Router\Route::link() doesn't work. This check in vmrouterHelper::buildRoute() prevents it from working:
if (!VmConfig::isSite()) return $segments;
Title: Re: Building frontend URLs from backend doesn't work
Post by: Kuubs on March 23, 2025, 10:45:40 AM
Quote from: Ghost on March 18, 2025, 07:59:31 AMBuilding frontend SEF URLs using Joomla\CMS\Router\Route::link() doesn't work. This check in vmrouterHelper::buildRoute() prevents it from working:
if (!VmConfig::isSite()) return $segments;

Yeah that is correct. I created a plugin to fix this, but it's probably better to make it work in the core code.

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;

class PlgSystemSefurl extends CMSPlugin
{
    public function onAfterInitialise()
    {
        $app = Factory::getApplication();
        if ($app->isClient('site') && $app->input->getCmd('option') === 'com_sefurl') {
            $virtuemart_product_id = $app->input->getInt('virtuemart_product_id');
            $virtuemart_category_id = $app->input->getInt('virtuemart_category_id');
            $link = 'index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' . $virtuemart_product_id . '&virtuemart_category_id=' . $virtuemart_category_id;
            $sef_url = Route::_($link, false);
            echo json_encode(['sef_url' => $sef_url]);
            $app->close();
        }
    }
}