-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f73d75
commit aeee386
Showing
8 changed files
with
208 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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|'), '/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; ?> |