Skip to content
Merged
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
10 changes: 10 additions & 0 deletions Block/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,17 @@ protected function getAutocompleteConfiguration(): array
'sections' => $config->getAdditionalSections(),
'nbOfProductsSuggestions' => $config->getNumberOfProductsSuggestions(),
'nbOfCategoriesSuggestions' => $config->getNumberOfCategoriesSuggestions(),
// SUGGESTIONS - START
'areSuggestionsEnabled' => $config->areSuggestionsEnabled(),
'suggestionsMode' => $config->getSuggestionsMode(),
// Magento
'showMagentoSuggestions' => $config->showMagentoSuggestions(),
'nbOfQueriesSuggestions' => $config->getNumberOfQueriesSuggestions(),
// Algolia
'showAlgoliaSuggestions' => $config->showAlgoliaSuggestions(),
'suggestionsIndexName' => $config->getSuggestionsIndexName(),
'nbOfAlgoliaSuggestions' => $config->getNumberOfAlgoliaSuggestions(),
// SUGGESTIONS - END
'isDebugEnabled' => $config->isDebugEnabled(),
'isNavigatorEnabled' => $config->isKeyboardNavigationEnabled(),
'debounceMilliseconds' => $config->getDebounceMilliseconds(),
Expand Down
50 changes: 50 additions & 0 deletions Helper/Configuration/AutocompleteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Algolia\AlgoliaSearch\Helper\Configuration;

use Algolia\AlgoliaSearch\Service\Serializer;
use Algolia\AlgoliaSearch\Model\Source\Suggestions;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

Expand All @@ -13,9 +14,13 @@ class AutocompleteHelper
public const ADDITIONAL_SECTIONS = 'algoliasearch_autocomplete/autocomplete/sections';
public const NB_OF_PRODUCTS_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_products_suggestions';
public const NB_OF_CATEGORIES_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_categories_suggestions';

public const SUGGESTIONS_MODE = 'algoliasearch_autocomplete/autocomplete/suggestions_mode';
public const NB_OF_QUERIES_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_queries_suggestions';
public const MIN_QUERY_POPULARITY = 'algoliasearch_autocomplete/autocomplete/min_popularity';
public const MIN_QUERY_NUMBER_OF_RESULTS = 'algoliasearch_autocomplete/autocomplete/min_number_of_results';
public const SUGGESTIONS_INDEX_NAME = 'algoliasearch_autocomplete/autocomplete/suggestions_index_name';
public const NB_OF_ALGOLIA_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_algolia_suggestions';

public const EXCLUDED_PAGES = 'algoliasearch_autocomplete/autocomplete/excluded_pages';
public const RENDER_TEMPLATE_DIRECTIVES = 'algoliasearch_autocomplete/autocomplete/render_template_directives';
Expand Down Expand Up @@ -79,6 +84,33 @@ public function getNumberOfCategoriesSuggestions(?int $storeId = null): int
);
}

public function areSuggestionsEnabled(?int $storeId = null): bool
{
return $this->getSuggestionsMode($storeId) > 0;
}

public function showMagentoSuggestions(?int $storeId = null): bool
{
return $this->getSuggestionsMode($storeId) === Suggestions::SUGGESTIONS_MAGENTO
&& $this->getNumberOfQueriesSuggestions($storeId) > 0;
}

public function showAlgoliaSuggestions(?int $storeId = null): bool
{
return $this->getSuggestionsMode($storeId) === Suggestions::SUGGESTIONS_ALGOLIA
&& $this->getSuggestionsIndexName($storeId) !== ''
&& $this->getNumberOfAlgoliaSuggestions($storeId) > 0;
}

public function getSuggestionsMode(?int $storeId = null): int
{
return (int) $this->configInterface->getValue(
self::SUGGESTIONS_MODE,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

public function getNumberOfQueriesSuggestions(?int $storeId = null): int
{
return (int) $this->configInterface->getValue(
Expand Down Expand Up @@ -108,6 +140,24 @@ public function getMinQueryNumberOfResults(?int $storeId = null): int
);
}

public function getSuggestionsIndexName(?int $storeId = null): string
{
return $this->configInterface->getValue(
self::SUGGESTIONS_INDEX_NAME,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

public function getNumberOfAlgoliaSuggestions(?int $storeId = null): int
{
return (int) $this->configInterface->getValue(
self::NB_OF_ALGOLIA_SUGGESTIONS,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Retrieve CMS pages to be excluded from the Autocomplete search
* Also impacts what pages are indexed
Expand Down
31 changes: 31 additions & 0 deletions Model/Source/Suggestions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Algolia\AlgoliaSearch\Model\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Suggestions implements OptionSourceInterface
{

public const SUGGESTIONS_DISABLED = 0;
public const SUGGESTIONS_MAGENTO = 1;
public const SUGGESTIONS_ALGOLIA = 2;

public function toOptionArray()
{
return [
[
'value' => self::SUGGESTIONS_DISABLED,
'label' => __('No'),
],
[
'value' => self::SUGGESTIONS_MAGENTO,
'label' => __('Use Magento Search Queries (deprecated)'),
],
[
'value' => self::SUGGESTIONS_ALGOLIA,
'label' => __('Use Algolia Query Suggestions'),
],
];
}
}
79 changes: 79 additions & 0 deletions Setup/Patch/Data/MigrateSuggestionsConfigPatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Algolia\AlgoliaSearch\Setup\Patch\Data;

use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
use Algolia\AlgoliaSearch\Model\Source\Suggestions;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchInterface;

class MigrateSuggestionsConfigPatch implements DataPatchInterface
{
public function __construct(
protected ModuleDataSetupInterface $moduleDataSetup,
) {}

/**
* @inheritDoc
*/
public function apply(): PatchInterface
{
$this->moduleDataSetup->getConnection()->startSetup();

$this->moveIndexingSettings();

$this->moduleDataSetup->getConnection()->endSetup();

return $this;
}

/**
* Migrate legacy configurations
* @return void
*/
protected function moveIndexingSettings(): void
{
$connection = $this->moduleDataSetup->getConnection();
$configDataTable = $this->moduleDataSetup->getTable('core_config_data');

// Get current number of configured Magento suggestions
$whereConfigPathFrom = $connection->quoteInto('path = ?', AutocompleteHelper::NB_OF_QUERIES_SUGGESTIONS);
$select = $connection->select()
->from($configDataTable)
->where('path = ?', AutocompleteHelper::NB_OF_QUERIES_SUGGESTIONS);
$existingValues = $connection->fetchAll($select);

foreach ($existingValues as $item) {
// If number of suggestions used to be superior to zero, this means that the feature was activated
// So we automatically set the suggestion mode to "Magento"
if ((int) $item['value'] > 0) {
$connection->insertOnDuplicate(
$configDataTable,
[
'scope' => $item['scope'],
'scope_id' => $item['scope_id'],
'path' => AutocompleteHelper::SUGGESTIONS_MODE,
'value' => Suggestions::SUGGESTIONS_MAGENTO
]
);
}
}
}

/**
* @inheritDoc
*/
public static function getDependencies(): array
{
return [];
}

/**
* @inheritDoc
*/
public function getAliases(): array
{
return [];
}
}
43 changes: 42 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,19 @@
<field id="is_popup_enabled">1</field>
</depends>
</field>
<field id="nb_of_queries_suggestions" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="suggestions_mode" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Suggestions</label>
<source_model>Algolia\AlgoliaSearch\Model\Source\Suggestions</source_model>
<comment>
<![CDATA[
Choose if you want to use Magento Suggestions (populated by the <code>search_query</code> table) or the Algolia Suggestions from your Algolia dashboard.
]]>
</comment>
<depends>
<field id="is_popup_enabled">1</field>
</depends>
</field>
<field id="nb_of_queries_suggestions" translate="label comment" type="text" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1">
<validate>validate-digits</validate>
<label>Number of queries</label>
<comment>
Expand All @@ -177,6 +189,7 @@
</comment>
<depends>
<field id="is_popup_enabled">1</field>
<field id="suggestions_mode">1</field>
</depends>
</field>
<field id="min_popularity" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
Expand All @@ -189,6 +202,7 @@
</comment>
<depends>
<field id="is_popup_enabled">1</field>
<field id="suggestions_mode">1</field>
</depends>
</field>
<field id="min_number_of_results" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
Expand All @@ -201,6 +215,33 @@
</comment>
<depends>
<field id="is_popup_enabled">1</field>
<field id="suggestions_mode">1</field>
</depends>
</field>
<field id="suggestions_index_name" translate="label comment" type="text" sortOrder="45" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Search suggestions index name</label>
<comment>
<![CDATA[
Specify the index name you have configured in Algolia to use for search suggestions.<br/>
For more information, check the <a target="_blank" href="https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions">Algolia official documentation</a>
]]>
</comment>
<depends>
<field id="is_popup_enabled">1</field>
<field id="suggestions_mode">2</field>
</depends>
</field>
<field id="nb_of_algolia_suggestions" translate="label comment" type="text" sortOrder="47" showInDefault="1" showInWebsite="1" showInStore="1">
<validate>validate-digits</validate>
<label>Number of Algolia Suggestions</label>
<comment>
<![CDATA[
How many query Algolia Suggestions do you want to display ? (Must be higher than 0)
]]>
</comment>
<depends>
<field id="is_popup_enabled">1</field>
<field id="suggestions_mode">2</field>
</depends>
</field>
<field id="sections" translate="label comment" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
<autocomplete_selector>.algolia-search-input</autocomplete_selector>
<nb_of_products_suggestions>6</nb_of_products_suggestions>
<nb_of_categories_suggestions>2</nb_of_categories_suggestions>
<suggestions_mode>0</suggestions_mode>
<nb_of_queries_suggestions>0</nb_of_queries_suggestions>
<min_popularity>1000</min_popularity>
<min_number_of_results>2</min_number_of_results>
<suggestions_index_name>query_suggestions_test</suggestions_index_name>
<nb_of_algolia_suggestions>2</nb_of_algolia_suggestions>
<sections><![CDATA[{"_1600351750374_374":{"name":"pages","label":"Pages","hitsPerPage":"2"}}]]></sections>
<excluded_pages><![CDATA[{"_1600351757831_831":{"attribute":"no-route"}}]]></excluded_pages>
<render_template_directives>1</render_template_directives>
Expand Down