django-oscar / django-oscar

Domain-driven e-commerce for Django

Home Page:http://oscarcommerce.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to show a filter of all attributes

dagNx opened this issue · comments

Hi,
I'm using Oscar 3.2 to build an e-commerce.
I need to show all attribute values as a filter in frontpage, similar to amazon sidebar filter.
Ex:

  • Color:
    • red
    • white
  • Format
    • large
    • small

etc.

Is the a way to do it in Oscar?

Thank you.

Hi!

Add the additional fields to your search index as faceted=True

  • fork the search app:
    ./manage.py oscar_fork_app search <yourapp>

  • in <yourapp>/search/search_indexes.py add the two fields in class ProductIndex:

% touch <yourapp>/search/search_indexes.py

from oscar.apps.search import ProductIndex as OscarProductIndex

class ProductIndex(OscarProductIndex):

    format = indexes.CharField(null=True, faceted=True)
    color = indexes.CharField(null=True, faceted=True)

    def prepare_format(self, obj):
        return obj.attribute_values.get(attribute__code="format").value_text

    def prepare_color(self, obj):
        return obj.attribute_values.get(attribute__code="color").value_text
  • If you work with Solr / Haystack search backend, add the additional fields to your solr - schema.xml:
    <field name="format" type="text" indexed="true" stored="true" multiValued="false" />
    <field name="format_exact" type="string" indexed="true" stored="true" multiValued="false" />

    <field name="color" type="text" indexed="true" stored="true" multiValued="false" />
    <field name="color_exact" type="string" indexed="true" stored="true" multiValued="false" />
  • reload schema.xml in the Solr Dashboard and restart Solr

  • add the fields in your settings.py to OSCAR_SEARCH_FACETS:

OSCAR_SEARCH_FACETS = {
    "fields": OrderedDict(
        [    # ....
            ("format", {"name": "Format", "field": "format"}),
            ("color", {"name": "Color", "field": "color"}),
       ],   # ...
  • rebuild your index with the two new faceted search fields:

./manage.py rebuild_index --noinput

The new fields should then appear automatically in the left column in the search facets below your category tree.