Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Below is a rundown of all configuration options
`www.example.com/example-category?color=red`.

If Seo path slugs is selected the url is constructed as `www.example.com/example-category/color/red`.
8) Use category URL from Tweakwise: When enabled, category facet links use the URL provided by Tweakwise in the navigation response instead of URLs generated by Magento. This reduces server-side computation and makes category URLs more predictable. Only applies on category pages, not on search results. Falls back to the standard Magento-generated URL when Tweakwise does not provide a link for an item.

#### Seo (All settings depend on Enabled having value yes)
1) Enabled: use Seo options yes or no.
Expand Down
2 changes: 1 addition & 1 deletion src/Block/Catalog/Product/ProductList/Toolbar/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function aroundGetAvailableOrders(Toolbar $subject, Closure $proceed)
}

/** @var SortFieldType[] $sortFields */
$sortFields = $this->context->getResponse()->getProperties()->getSortFields(); // @phpstan-ignore-line
$sortFields = $this->context->getResponse()->getProperties()->getSortFields() ?? []; // @phpstan-ignore-line

$result = [];
foreach ($sortFields as $field) {
Expand Down
9 changes: 7 additions & 2 deletions src/Block/LayeredNavigation/RenderLayered/DefaultRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ public function getCategoryUrl(Item $item): string
{
$catUrl = $item->getUrl();

if (strpos($catUrl, $this->getBaseUrl()) === false) {
$catUrl = $this->getBaseUrl() . $item->getUrl();
// Compare scheme-agnostically to handle http vs https mismatches between
// the Tweakwise-provided link and the Magento base URL.
$catUrlWithoutScheme = preg_replace('#^https?://#', '', $catUrl);
$baseUrlWithoutScheme = preg_replace('#^https?://#', '', $this->getBaseUrl());

if (strpos($catUrlWithoutScheme, $baseUrlWithoutScheme) === false) {
$catUrl = rtrim($this->getBaseUrl(), '/') . '/' . ltrim($catUrl, '/');
}

return $catUrl;
Expand Down
1 change: 0 additions & 1 deletion src/Model/Catalog/Layer/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ protected function createItem(AttributeType $attributeType)
$item = $this->itemFactory->create(['filter' => $this, 'attributeType' => $attributeType]);

$children = [];
// @phpstan-ignore-next-line
foreach ($attributeType->getChildren() as $childAttributeType) {
$children[] = $this->createItem($childAttributeType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getResponse()
*/
public function getTweakwiseRequestId(): string
{
$headers = $this->getContext()->getResponse()->getValue('headers');
$headers = $this->getContext()->getResponse()->getValue('headers') ?? [];
$normalized = array_change_key_case($headers, CASE_LOWER);

return $normalized['twn-request-id'][0] ?? '';
Expand Down
45 changes: 44 additions & 1 deletion src/Model/Catalog/Layer/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
namespace Tweakwise\Magento2Tweakwise\Model\Catalog\Layer;

use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Filter\Item;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext\CurrentContext;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\CategoryUrlInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\FilterApplierInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\Strategy\UrlStrategyFactory;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\UrlInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\UrlModel;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductSearchRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\FacetType\SettingsType;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2TweakwiseExport\Model\Helper as ExportHelper;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\App\Request\Http as MagentoHttpRequest;
use Magento\Framework\Exception\NoSuchEntityException;
use Exception;

/**
* Class Url will later implement logic to use implementation selected in configuration.
Expand Down Expand Up @@ -72,13 +76,18 @@ class Url
* @param MagentoHttpRequest $request
* @param CategoryRepositoryInterface $categoryRepository
* @param ExportHelper $exportHelper
* @param UrlModel $magentoUrl
* @param Config $config
* @param CurrentContext $currentContext
*/
public function __construct(
UrlStrategyFactory $urlStrategyFactory,
MagentoHttpRequest $request,
CategoryRepositoryInterface $categoryRepository,
ExportHelper $exportHelper,
UrlModel $magentoUrl
UrlModel $magentoUrl,
protected readonly Config $config,
protected readonly CurrentContext $currentContext
) {
$this->urlStrategyFactory = $urlStrategyFactory;
$this->categoryRepository = $categoryRepository;
Expand Down Expand Up @@ -143,13 +152,47 @@ public function getSelectFilter(Item $item): string
->getFacetSettings();

if ($settings->getSource() === SettingsType::SOURCE_CATEGORY) {
if ($this->shouldUseTweakwiseCategoryUrl($item)) {
return $item->getAttribute()->getLink();
}

return $this->getCategoryUrlStrategy()
->getCategoryFilterSelectUrl($this->request, $item);
}

return $this->addBaseUrl($this->getUrlStrategy()->getAttributeSelectUrl($this->request, $item));
}

/**
* Determine whether the Tweakwise-provided category URL should be used for a filter item.
* Returns true when:
* - the "Use category URL from Tweakwise" setting is enabled
* - the item carries a non-empty link from Tweakwise
* - the current request is not a search request
*
* @param Item $item
* @return bool
*/
protected function shouldUseTweakwiseCategoryUrl(Item $item): bool
{
if (!$this->config->isCategoryUrlFromTweakwiseEnabled()) {
return false;
}

$tweakwiseUrl = $item->getAttribute()->getLink();
if (empty($tweakwiseUrl)) {
return false;
}

try {
$navigationRequest = $this->currentContext->getRequest();
} catch (Exception $e) {
return false;
}

return !($navigationRequest instanceof ProductSearchRequest);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 11 additions & 3 deletions src/Model/Client/Type/AttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function setChildren(array $children)
}

/**
* @return string
* @return AttributeType[]
*/
public function getChildren()
public function getChildren(): array
{
return $this->getDataValue('children');
return $this->getDataValue('children') ?? [];
}

/**
Expand Down Expand Up @@ -82,6 +82,14 @@ public function getUrl()
return (string) $this->getDataValue('url');
}

/**
* @return string
*/
public function getLink(): string
{
return (string) $this->getDataValue('link');
}

/**
* @return int|null
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,15 @@ public function isCategoryViewDefault(?Store $store = null)
return $this->getStoreConfig('tweakwise/layered/default_category_view', $store);
}

/**
* @param Store|null $store
* @return bool
*/
public function isCategoryUrlFromTweakwiseEnabled(?Store $store = null): bool
{
return (bool) $this->getStoreConfig('tweakwise/layered/use_category_url_from_tweakwise', $store);
}

/**
* @return int
*/
Expand Down
8 changes: 8 additions & 0 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
<field id="default_link_renderer">0</field>
</depends>
</field>
<field id="use_category_url_from_tweakwise" translate="label,comment" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use category URL from Tweakwise</label>
<comment>When enabled, category facet links use the URL provided by Tweakwise instead of Magento-generated URLs. Only applies on category pages, not on search results.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
<group id="seo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<depends>
Expand Down
Loading