Make sure you’ve installed the Algolia AI Search & Discovery extension on your Magento 2 website.
CustomAlgolia
To avoid having to bootstrap a lot of code to create a custom extension, use theCustomAlgolia starter code.
Installation
InstallCustomAlgolia with Composer by running the following commands in the command line:
app/code directory in your Magento 2 base directory where you can make further modifications to the implementation and commit to your own project repository.
CuastomAlgolia structure
To keep things simple, CuastomAlgolia uses the same data structure as is convention for Magento 2 extensions.
Look for these files under app/code/Algolia/CustomAlgolia:
Customize looks
This example overrides thefacet.phtml template.
This template is used for the InstantSearch feature, to display a for hits matching the .
Create a new template
Copy the chosen template from the original extension (view/frontend/templates/instant/facet.phtml) to the exact same path in the CustomAlgolia extension.
The file can now be modified as needed.
Register the new template
With the new template in place, Magento needs to know that it has to use this template instead of the original one. To do this, open the configuration filealgolia_search_handle.xml and add the following code block to it:
XML
It is important to use the correct template name in the snippet above. If unsure, please check the original extension’s layout file for the template names.
Customize frontend behavior
Change existing behavior
To customize some of the extensions’ behavior, you might need to override the JavaScript file. You should use RequireJS to override the JavaScript files, because it correctly handles the dependencies within your customizations. This example overrides theautocomplete.js file, which implements the autocomplete menu.
For more information about customizing the autocomplete feature,
see JavaScript mixins with RequireJS.
Create a new script
Copy the chosen template from the original extension (view/frontend/web/js/autocomplete.js) to the exact same path in the CustomAlgolia extension.
The file can now be modified as needed.
Register the new script
With the new script in place, Magento needs to know it has to use this script instead of the original one. Open or create the configuration filerequirejs-config.js and add the following code block to the config object.
JavaScript
Add custom behavior
To add features on top of the default behavior, a new JavaScript file needs to be added. In this file, custom methods can be used to modify any InstantSearch or Autocomplete features.Create a new script
Create a new file namedview/frontend/web/js/hooks.js.
Specify any dependencies of your hooks. For example:
JavaScript
Register the new script
With the new script in place, Magento needs to know how to find it and when to load it. Open or create the configuration filerequirejs-config.js and add the following code block to the config object.
JavaScript
It is important to use the module id
algoliaHooks to ensure that your hooks are registered prior to loading the InstantSearch and Autocomplete libraries that will ultimately invoke them.deps configuration.
For example:
JavaScript
- The
data-mage-initattribute - The
<script type="text/x-magento-init">tag
Customize backend behavior
To override backend behavior like indexing or settings, you need to add a listener on a backend custom event. The listener is composed from an Observer PHP class and it needs to be registered in theetc/events.xml file.
This example creates a listener on the algolia_products_index_before_set_settings event to modify Algolia’s index settings for your products’ index.
Register the observer
To register the observer, add the following snippet to theetc/events.xml file:
XML
Create observer
Create theObserver/UpdateProductsSettings.php file, and add a new Observer class to it.
PHP
execute block can be modified as needed. In this example, the snippetEllipsisText setting is modified.