πŸŽ‰ Try the public beta of the new docs site at algolia.com/doc-beta! πŸŽ‰
Integrations / Magento 2

How to Customize the Extension

Introduction

Our extension can be a powerful out-of-the-box tool for a variety of search features. However, sometimes our plug-in needs to be customized in order to change the look or behavior to perfectly suit any use case. Keep in mind that any changes you make to the extension will be removed when installing any new versions of the extension.

In this guide, we will look at how you can safely create a custom implementation of our extension for Magento, without any danger of it being overwritten with future versioning of the extension.

Please make sure you have our extension installed on your Magento 2 website.

The best way to change any default style or behavior in Magento is to override it. For instance, when a template needs to be modified, it’s not supposed to be modified directly; a new template has to be created, and Magento needs to be told to use this new template instead of the old one. The new template will then be a new custom extension.

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

The CustomAlgolia boilerplate can be installed with Composer by running the following commands in the command line:

$
$
composer require algolia/algoliasearch-custom-algolia-magento-2
php bin/magento setup:upgrade

The boilerplate will be installed into the 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 under app/code/Algolia/CustomAlgolia:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
β”œβ”€β”€ etc
β”‚Β Β  β”œβ”€β”€ events.xml
β”‚Β Β  └── module.xml
β”œβ”€β”€ Observer
β”‚Β Β  β”œβ”€β”€ UpdateFrontendConfiguration.php
β”‚Β Β  └── UpdateProductsSettings.php
β”œβ”€β”€ view
β”‚   └── frontend
β”‚   Β Β   β”œβ”€β”€ layout
β”‚       β”‚   └── algolia_search_handle.xml
β”‚       β”œβ”€β”€ web
β”‚       β”‚   β”œβ”€β”€ template
|       |   |   β”œβ”€β”€ additional-section-mixin.js
|       |   |   β”œβ”€β”€ categories-mixin.js
|       |   |   β”œβ”€β”€ pages-mixin.js
|       |   |   β”œβ”€β”€ products-mixin.js
|       |   |   └── suggestions-mixin.js
β”‚       β”‚   β”œβ”€β”€ customalgolia.css
β”‚       β”‚   └── hooks.js
β”‚       β”œβ”€β”€ templates
β”‚       β”‚   └── instant
β”‚       β”‚       └── facet.phtml
|       └── requirejs-config.js
β”œβ”€β”€ composer.json
└── registration.php

Customizing Looks

For this example, we’ll override the facet.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 file algolia_search_handle.xml and add the following code block to it:

1
<referenceBlock name="algolia.instant.facet" template="Algolia_CustomAlgolia::instant/facet.phtml"/>

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.

Customizing frontend behavior

Overriding 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 the autocomplete.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/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 file requirejs-config.js and add the following code block to the config object.

1
2
3
4
5
6
7
var config = {
  map: {
    '*': {
      'autocomplete': 'Algolia_CustomAlgolia/autocomplete'
    }
  }
}

Adding 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 instant search or autocomplete features.

Create a new script

Create a new file named view/frontend/web/hooks.js.

Specify any dependencies of your hooks. For example:

1
2
3
4
5
6
7
define([
    'jquery',
    'algoliaAnalytics',
    'algoliaBundle',
 ], function ($, algoliaAnalyticsWrapper, algoliaBundle) {
    // Add your custom code here
});

The file can now be modified as needed.

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 file requirejs-config.js and add the following code block to the config object.

1
2
3
4
5
6
7
var config = {
  map: {
    '*': {
      'algoliaHooks': 'Algolia_CustomAlgolia/hooks'
    }
  }
}

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.

If you have a script that you wish to load on every page in your store front but load order is not essential then you can use the deps configuration.

For example:

1
2
3
4
5
var config = {
  deps: [
    'myCustomAlgoliaHooks'
  ]
}

You can learn more about this configuration in the RequireJS documentation.

Another way to load your JavaScript is via Magento’s declarative convention. This can conditionally load your scripts based on logic from your backend application.

You can use these two approaches for implementing this:

  1. The data-mage-init attribute
  2. The <script type="text/x-magento-init"> tag

For more information, see the Adobe Commerce documentation.

Customizing 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 the etc/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 the etc/events.xml file:

1
2
3
<event name="algolia_products_index_before_set_settings">
    <observer name="customalgolia_products_settings" instance="Algolia\CustomAlgolia\Observer\UpdateProductsSettings" />
</event>

Create observer

Create the Observer/UpdateProductsSettings.php file, and add a new Observer class to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
namespace Algolia\CustomAlgolia\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class UpdateProductsSettings implements ObserverInterface
{
  public function execute(Observer $observer)
  {
    // Observer execution code...

    // Here you can modify frontend configuration

    // Example:
    // $productsSettings = $observer->getData('index_settings');
    // $productsSettings['snippetEllipsisText'] = '…';
  }
}

The code in the execute block can be modified as needed. In this example, the snippetEllipsisText setting is modified.

Updating

When files are overridden, they will not receive updates from our original extension. If a bug fix needs to be integrated into the custom code, this will need to be done manually. Please make sure you read the change log of each release to see if there was any change to the file that was overridden.

Did you find this page helpful?