Add custom field to global search - not working

hello, I would like to add custom search according to Add custom data to the search | Shopware Documentation

However, I feel like this guide has a lot of mistakes…

I added code like below

const { Application } = Shopware;

Application.addServiceProviderDecorator('searchTypeService', searchTypeService => {
    searchTypeService.upsertType('test', {
        entityName: 'test',
        placeholderSnippet: 'foo-bar.general.placeholderSearchBar',
        listingRoute: 'test',
        hideOnGlobalSearchBar: false,
    });

    return searchTypeService;
});

but when I try find something I get error „Can not get module by the entity name is test“ why? what I must do more ? Entity test exist and working.

I tried creating a new module… but what I need to type in decorator ?

searchTypeService.upsertType('test', {
        entityName: 'test',

My new module:

Shopware.Module.register('test', {
    entity: 'test',
    defaultSearchConfiguration: {
        _searchable: true,
        name: {
            _searchable: true,
            _score: 500,
        },
        description: {
            name: {
                _searchable: true,
                _score: 500,
            },
        },
    },
});

and my decorator

const { Application } = Shopware;
**import '../module/test/index'** // my module import
Application.addServiceProviderDecorator('searchTypeService', searchTypeService => {
    searchTypeService.upsertType('test', {
        entityName: 'test',
        placeholderSnippet: 'test',
        listingRoute: 'test',
        hideOnGlobalSearchBar: false,
    });

    return searchTypeService;
});

{imported module and using}

nothing changes, it same error.

main.js

import './decorator/searchTypeServiceDecorator'

structure

image

where is the problem ?

1 Like

Hey there,

See in my opinion: Here are the steps to ensure everything is correctly set up:

  1. main.js
import './decorator/searchTypeServiceDecorator';
import './module/test/index';  // Adjusted the import path based on your structure

  1. Module Registration (in index.js)
Shopware.Module.register('test', {
    entity: 'test',
    defaultSearchConfiguration: {
        _searchable: true,
        name: {
            _searchable: true,
            _score: 500,
        },
        description: {
            _searchable: true,
            _score: 500,
        },
    },
});

  1. Decorator (in searchTypeServiceDecorator.js)
const { Application } = Shopware;
import '../module/test/index'; // Ensure this path is correct

Application.addServiceProviderDecorator('searchTypeService', searchTypeService => {
    searchTypeService.upsertType('test', {
        entityName: 'test',
        placeholderSnippet: 'test.general.placeholderSearchBar',
        listingRoute: 'test',
        hideOnGlobalSearchBar: false,
    });

    return searchTypeService;
});

Make sure to clear the cache after making these changes to ensure they take effect. hope the above will be helpful.

Best,
@wolfsmith