How do I give split listings names generated from their attributes?

This code example extends the Aero\Search\Elastic\Documents\ListingDocument document to generate names for split listings from the attributes that the variant is made up of. You can learn more about extending the listing document here.

It’s important to note that you need to reindex for changes to the listing document to take effect. You can reindex using the php artisan aero:search:reindex command. You can learn more about reindexing here.


namespace Acme\MyModule;

use Aero\Common\Providers\ModuleServiceProvider;
use Aero\Search\Elastic\Documents\ListingDocument;

class ServiceProvider extends ModuleServiceProvider
   public function setup()
       ListingDocument::add('search-result', function ($document) {
           if (! $name = $document->getData()['search-result']['name'] ?? null) return [];
           if (! $variant = $document->getModel()->variants->first()) return [];
           if (! $attributeGroups = $document->getModel()->product->attribute_groups_to_split_by ?? []) return [];

           $attributes = $variant->attributes->whereIn('attribute_group_id', $attributeGroups);

           if ($attributes->isEmpty()) return [];

           $attributeNames = $attributes->map->getTranslation('name', $document->getLanguage())->join(' ');

           return ['name' => "$attributeNames $name"];

Articles in this section

Was this article helpful?
0 out of 0 found this helpful