Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can products be included in site search? #102

Closed
AmandaLutzTO opened this issue Apr 1, 2024 · 3 comments
Closed

How can products be included in site search? #102

AmandaLutzTO opened this issue Apr 1, 2024 · 3 comments
Assignees
Labels
Plugins → Shopify For Linear sync Plugins For Linear sync

Comments

@AmandaLutzTO
Copy link

Description

Client wants products to be included in site search.

Steps to reproduce

  1. Example twig code for site search is {% set entries = craft.entries().search(query).orderBy('score').limit(4) %}
  2. Results includes pagination
  3. Plugin documentation includes many instances of "using the .search() param" so I know products can be searched
  4. But can site entries and products be searched and ordered by "score" together?

Additional info

  • Craft version: 4.8.3
  • PHP version: 8.1.8
  • Database driver & version: MySQL 8.0.32
  • Plugins & versions: Shopify 4.0.0
@AmandaLutzTO AmandaLutzTO added bug Something isn't working Plugins For Linear sync Plugins → Shopify For Linear sync labels Apr 1, 2024
Copy link

linear bot commented Apr 1, 2024

@nfourtythree nfourtythree self-assigned this Apr 2, 2024
@nfourtythree
Copy link
Contributor

Hi @AmandaLutzTO

Thank you for you message. Unfortunately, out of the box it is not possible to search across different element types.

However there is a bit of a work around solution that you could try which would be the following:

{% set query = 't-shirt' %}
{% set elementQuery = create('craft\\elements\\db\\ElementQuery', ['craft\\base\\Element']) %}
{% do elementQuery.search(query) %}
{% do elementQuery.orderBy('score') %}
{% do elementQuery.limit(4) %}
{% set elementIds = elementQuery.ids() %}

{% set entries = craft.entries.id(elementIds).fixedOrder().indexBy('id').all() %}
{% set products = craft.shopifyProducts.id(elementIds).fixedOrder().indexBy('id').all() %}

<ul>
  {% for id in elementIds %}
    <li>
      {% if entries[id] is defined %}
          {{ entries[id].title }}
        {% elseif products[id] is defined %}
          {{ products[id].title }}
        {% endif %}
    </li>
  {% endfor %}
</ul>

Hope this helps, thanks!

@nfourtythree nfourtythree removed the bug Something isn't working label Apr 2, 2024
@AmandaLutzTO
Copy link
Author

This was a good solution. Thanks for your help!

For anyone else who finds this: watch out! This method also returns matching assets, categories, etc to elementQuery. Listing entries and products with the for/if statements was fine but it really messed up my pagination.

I ended up doing a search query for elements, a search query for products, merging them to 1 collection, and then my for loop includes a twig sort with spaceship operator (https://twig.symfony.com/doc/3.x/filters/sort.html). I'm open to modifying my code if anyone has thoughts/suggestions for improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Plugins → Shopify For Linear sync Plugins For Linear sync
Projects
None yet
Development

No branches or pull requests

2 participants