Skip to content

Corrupt url when deleting filter using ajax. #303

@dhacquebord

Description

@dhacquebord

Hi,

I'm trying to add Ajax filtering to our websites but at some times i'm encountering a weird error.

For example:
I'm currently at the this URL: /kleding/schoenen/eigenschappen/rits/geslacht/unisex/merk/nike/merk/adidas.

Expected result:
I expect the new URL to be /kleding/schoenen/eigenschappen/rits/geslacht/unisex/merk/adidas.

Actual result:
When i try to remove filter merk/nike the page does refresh and give me the correct products and selected filters but the URL turned to:
/kleding/schoenen/eigenschappen/rits/geslacht/unisex/merk/nike/merk/adidas/eigenschappen/rits/geslacht/unisex/merk/adidas/.

I think the problem lies in the following function:

public function buildFilterUrl(MagentoHttpRequest $request, array $filters = []): string
{
$currentUrl = $this->getOriginalUrl($request);
$currentFilterPath = $request->getParam(self::REQUEST_FILTER_PATH);
$newFilterPath = $this->buildFilterSlugPath($filters);
if (empty($currentFilterPath)) {
$urlParts = parse_url($currentUrl);
if (strpos($urlParts['path'], $newFilterPath) === false) {
$url = $urlParts['path'] . $newFilterPath;
} else {
//filter path already exists in url
$url = $urlParts['path'];
}
if (isset($urlParts['query'])) {
$url .= '?' . $urlParts['query'];
}
} else {
// Replace filter path in current URL with the new filter combination path
if (strpos($currentUrl, $currentFilterPath) !== false) {
$url = str_replace(
sprintf('%s/', rtrim($currentFilterPath, '/')),
sprintf('%s/', $newFilterPath),
$currentUrl
);
} else {
$url = $currentUrl . '/' . $newFilterPath;
}
}
$categoryUrlSuffix = $this->scopeConfig->getValue(
CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX,
'store'
);
// Replace all occurrences of double slashes with a single slash except those in scheme.
// This can happen when $categoryUrlSuffix === '/' and in some other cases
return preg_replace('/(?<!:)\/\//', '/', $url);
}

When i add some logging i notice that:

  • currentUrl is /kleding/schoenen/eigenschappen/rits/geslacht/unisex/merk/nike/merk/adidas
  • currentFilterPath is empty
  • newFilterPath is /eigenschappen/rits/geslacht/unisex/merk/adidas/

Line 385 checks if currentUrl contains newFilterPath and concludes that it doesn't. Therefore it adds newFilterPath to the current Url and returns it.

How is this function supposed to work? Should currentFilterPath not be empty?

Context:
We're running on Magento 2.4.6 with Hyvä 1.3.14, with the following Tweakwise extensions:

        "tweakwise/magento2-attributelanding-tweakwise": "4.4.1",
        "tweakwise/magento2-tweakwise": "8.6.1",
        "tweakwise/magento2-tweakwise-export": "7.6.0",
        "tweakwise/magento2-tweakwise-hyva": "4.4.0",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions