Skip to content

Commit

Permalink
Added suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jansentjeu committed Dec 13, 2024
1 parent 0f73d75 commit aeee386
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Config

private const XML_PATH_MERCHANDISING_ENABLED = 'tweakwisejs/merchandising/enabled';

private const XML_PATH_SEARCH_TYPE = 'tweakwisejs/search/type';

/**
* @param ScopeConfigInterface $scopeConfig
*/
Expand Down Expand Up @@ -45,4 +47,12 @@ public function isMerchandisingEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_MERCHANDISING_ENABLED, ScopeInterface::SCOPE_STORE);
}

/**
* @return string
*/
public function getSearchType(): string
{
return $this->scopeConfig->getValue(self::XML_PATH_SEARCH_TYPE, ScopeInterface::SCOPE_STORE);
}
}
36 changes: 36 additions & 0 deletions src/Observer/AddPageAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\View\Page\Config as PageConfig;
use Tweakwise\TweakwiseJs\Model\Config;
use Tweakwise\TweakwiseJs\Model\Enum\SearchType;

class AddPageAssets implements ObserverInterface
{
Expand All @@ -33,6 +34,20 @@ public function execute(Observer $observer)
return;
}

$this->addDefaultPageAssets();

if ($this->config->getSearchType() !== SearchType::SUGGESTIONS->value) {
return;
}

$this->addSuggestionsPageAssets();
}

/**
* @return void
*/
private function addDefaultPageAssets(): void
{
$instanceKey = $this->config->getInstanceKey();

$this->pageConfig->addRemotePageAsset(
Expand All @@ -54,4 +69,25 @@ public function execute(Observer $observer)
]]
);
}

/**
* @return void
*/
private function addSuggestionsPageAssets(): void
{
$this->pageConfig->addRemotePageAsset(
'https://gateway.tweakwisenavigator.net/js/suggestions.js',
'link',
['attributes' => ['rel' => 'preload', 'as' => 'script']]
);

$this->pageConfig->addRemotePageAsset(
'https://gateway.tweakwisenavigator.net/js/suggestions.js',
'js',
['attributes' => [
'data-failover' => 'https://gateway.tweakwisenavigator.com/js/suggestions.js',
'onerror' => 'window.tweakwiseFailover(this.dataset.failover)'
]]
);
}
}
44 changes: 40 additions & 4 deletions src/Observer/ManageLayoutBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Layout;
use Tweakwise\TweakwiseJs\Model\Config;
use Tweakwise\TweakwiseJs\ViewModel\TweakwiseJs;
use Tweakwise\TweakwiseJs\Model\Enum\SearchType;
use Tweakwise\TweakwiseJs\ViewModel\Merchandising;
use Tweakwise\TweakwiseJs\ViewModel\Search;

class ManageLayoutBlocks implements ObserverInterface
{
/**
* @param Http $request
* @param Config $config
* @param TweakwiseJs $viewModel
* @param Merchandising $merchandisingViewModel
* @param Search $searchViewModel
* @param Resolver $layerResolver
*/
public function __construct(
private readonly Http $request,
private readonly Config $config,
private readonly TweakwiseJs $viewModel,
private readonly Merchandising $merchandisingViewModel,
private readonly Search $searchViewModel,
private readonly Resolver $layerResolver
) {
}
Expand All @@ -39,6 +43,7 @@ public function __construct(
*/
public function execute(Observer $observer)
{
// TODO: CAN WE SPECIFY ON WHICH PAGE TYPES THE BLOCKS MUST BE LOADED?
if (!$this->config->isEnabled()) {
return;
}
Expand All @@ -47,6 +52,10 @@ public function execute(Observer $observer)

$this->addDefaultBlock($layout);

if ($this->useTweakwiseJsSearch()) {
$this->addSearchBlock($layout);
}

if (!$this->isCategoryPage() || !$this->showTweakwiseJsCategoryViewBlock()) {
return;
}
Expand Down Expand Up @@ -86,7 +95,7 @@ private function addTweakwiseJsCategoryViewBlock(Layout $layout): void
$blockName,
[
'data' => [
'view_model' => $this->viewModel
'view_model' => $this->merchandisingViewModel
]
]
)->setTemplate('Tweakwise_TweakwiseJs::category/listing.phtml');
Expand Down Expand Up @@ -114,4 +123,31 @@ private function showTweakwiseJsCategoryViewBlock(): bool

return true;
}

/**
* @return bool
*/
private function useTweakwiseJsSearch(): bool
{
return $this->config->getSearchType() !== SearchType::MAGENTO_DEFAULT->value;
}

/**
* @param Layout $layout
* @return void
*/
private function addSearchBlock(Layout $layout): void
{
$blockName = 'tweakwise-js-search';
$layout->createBlock(
Template::class,
$blockName,
[
'data' => [
'view_model' => $this->searchViewModel
]
]
)->setTemplate('Tweakwise_TweakwiseJs::search.phtml');
$layout->setChild('after.body.start', $blockName, $blockName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Tweakwise\TweakwiseJs\Helper\Data;

class TweakwiseJs implements ArgumentInterface
class Merchandising implements ArgumentInterface
{
/**
* @param Data $dataHelper
Expand All @@ -22,10 +22,13 @@ public function __construct(
/**
* @param View $block
* @return string
* @throws NoSuchEntityException
*/
public function getTweakwiseCategoryId(View $block): string
{
return $this->dataHelper->getTweakwiseId((int) $block->getCurrentCategory()->getId());
try {
return $this->dataHelper->getTweakwiseId((int)$block->getCurrentCategory()->getId());
} catch (NoSuchEntityException $e) {
return '0';
}
}
}
67 changes: 67 additions & 0 deletions src/ViewModel/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\ViewModel;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tweakwise\TweakwiseJs\Helper\Data;
use Tweakwise\TweakwiseJs\Model\Config;

class Search implements ArgumentInterface
{
/**
* @param Config $config
* @param StoreManagerInterface $storeManager
* @param Data $dataHelper
* @param UrlInterface $urlBuilder
*/
public function __construct(
private readonly Config $config,
private readonly StoreManagerInterface $storeManager,
private readonly Data $dataHelper,
private readonly UrlInterface $urlBuilder
) {
}

/**
* @return string
*/
public function getSearchType(): string
{
return $this->config->getSearchType();
}

/**
* @return string|null
*/
public function getInstanceKey(): ?string
{
return $this->config->getInstanceKey();
}

/**
* @return int
*/
public function getStoreRootCategory(): int
{
try {
return (int)$this->dataHelper->getTweakwiseId(
(int)$this->storeManager->getStore()->getRootCategoryId()
);
} catch (NoSuchEntityException $e) {
return 0;
}
}

/**
* @return string
*/
public function getSearchUrl(): string
{
return trim($this->urlBuilder->getUrl('twsearch#twn|'), '/');
}
}
9 changes: 7 additions & 2 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<tweakwisejs>
<general>
<enabled>0</enabled>
</general>
<merchandising>
<category_products_list_block_name>category.products.list</category_products_list_block_name>
<sidebar_block_name>sidebar.main</sidebar_block_name>
<enabled>0</enabled>
</merchandising>
<search>
<type>magento_default</type>
</search>
</tweakwisejs>
</default>
</config>
2 changes: 2 additions & 0 deletions src/view/frontend/templates/category/listing.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
declare(strict_types = 1);

use Magento\Catalog\Block\Category\View;
use Tweakwise\TweakwiseJs\ViewModel\Merchandising;

/** @var View $block */

/** @var Merchandising $viewModel */
$viewModel = $block->getViewModel();
?>

Expand Down
40 changes: 40 additions & 0 deletions src/view/frontend/templates/search.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types = 1);

use Magento\Framework\View\Element\Template;
use Tweakwise\TweakwiseJs\Model\Enum\SearchType;
use Tweakwise\TweakwiseJs\ViewModel\Search;

/** @var Template $block */

/** @var Search $viewModel */
$viewModel = $block->getViewModel();
?>

<?php if ($viewModel->getSearchType() === SearchType::SUGGESTIONS->value): ?>
<script>
window['twn-starter-config'].input = [];
window.addEventListener('DOMContentLoaded', function () {
window.tweakwiseSuggestions({
input: "#search",
instancekey: "<?= $viewModel->getInstanceKey();?>",
cid: "<?= $viewModel->getStoreRootCategory();?>",
searchPhrases: {
handle: ({ data }) => {
location.href = '<?= $viewModel->getSearchUrl();?>?tn_q=' + data.match;
}
}
});
});
</script>
<?php else: ?>
<script>
window.addEventListener("DOMContentLoaded", function () {
var form = document.getElementById('search');
console.log(form);
form.submit = function(event){
event.preventDefault();
};
});
</script>
<?php endif; ?>

0 comments on commit aeee386

Please sign in to comment.