Please make sure you have our extension installed on your Magento 2 website.
CustomAlgolia
In order to avoid having to bootstrap a lot of code to create a custom extension for our engine, we created a boilerplate for anyone to use. The boilerplate, named CustomAlgolia, is shipped with a few code samples to get started quickly.Installation
TheCustomAlgolia
boilerplate can be installed 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.
Boilerplate structure
To keep things simple, the boilerplate uses the same data structure as is convention for Magento 2 extensions. Look for these files underapp/code/Algolia/CustomAlgolia
:
Customizing looks
For this example, we’ll override thefacet.phtml
template.
This template is used for the InstantSearch feature, to display facets for hits matching the query.
Create a new template
Copy the chosen template from our 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
In order 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.
Click here to read more about customizing the autocomplete feature.
Create a new script
Copy the chosen template from our 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. To do this, open or create the configuration filerequirejs-config.js
and add the following code block to the config
object.
JavaScript
Add custom behavior
To add functionality 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. To do this, 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-init
attribute - 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.
For this example, we will create 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.