问题
I modified my code to add new solr sort for price.
INSERT_UPDATE FieldSolrSort;sort(indexedType(identifier),code)[unique=true];fieldName[unique=true];descending[unique=true];$IndexedType:price;priceValue;true
I have currently two solr sort
- Name Ascending
- Name Descending
I added new solr sort for price
- Price Ascending
- Price Descending
But I want my new solr sort to only show when the user has logged in. Anyone know? Thanks
回答1:
Step 1: Import impex to enable price sort option
If you refer any OOTB store(apparelstore
), you are able to see Price Sort (price-asc
) option in Impex. I have highlighted Impex below.
Step 2: Hide from the anonymous user
Don't render Price sort in case of the anonymous user. You can take help of JSTL condition to check that as I mentioned below.
Please Note: Below code is just reference for you, I haven't tested it
orderFormPagination.tag
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<c:set var="isLoggedInUser" value="false" />
<sec:authorize ifNotGranted="ROLE_ANONYMOUS">
<c:set var="isLoggedInUser" value="true" />
</sec:authorize>
<select id="sortOptions${top ? '1' : '2'}" name="sort" class="form-control">
<option disabled><spring:theme
code="${themeMsgKey}.sortTitle" /></option>
<c:forEach items="${searchPageData.sorts}" var="sort">
<c:if test="${isLoggedInUser || (!isLoggedInUser && !fn:startsWith(sort.code, 'price'))}">
<option value="${sort.code}"
${sort.selected? 'selected="selected"' : ''}>
<c:choose>
<c:when test="${not empty sort.name}">
${sort.name}
</c:when>
<c:otherwise>
<spring:theme code="${themeMsgKey}.sort.${sort.code}" />
</c:otherwise>
</c:choose>
</option>
</c:if>
</c:forEach>
</select>
Impex
Define SolrIndexedProperty
INSERT_UPDATE SolrIndexedProperty ; solrIndexedType(identifier)[unique=true] ; name[unique=true] ; type(code) ; sortableType(code) ; currency[default=false] ; localized[default=false] ; multiValue[default=false] ; useForSpellchecking[default=false] ; useForAutocomplete[default=false] ; fieldValueProvider ; ftsPhraseQuery[default=false] ; ftsPhraseQueryBoost ; ftsQuery[default=false] ; ftsQueryBoost ; ftsFuzzyQuery[default=false] ; ftsFuzzyQueryBoost ; ftsWildcardQuery[default=false] ; ftsWildcardQueryType(code)[default=POSTFIX] ; ftsWildcardQueryBoost ; ftsWildcardQueryMinTermLength
; $solrIndexedType ; name ; text ; sortabletext ; ; true ; ; true ; true ; ; true ; 100 ; true ; 50 ; true ; 25 ; ; ; ;
; $solrIndexedType ; priceValue ; double ; ; true ; ; ; ; ; productPriceValueProvider ; ; ; ; ; ; ; ; ; ;
Define the available sorts
INSERT_UPDATE SolrSort ; &sortRefID ; indexedType(identifier)[unique=true] ; code[unique=true] ; useBoost
; sortRef3 ; $solrIndexedType ; name-asc ; false
; sortRef4 ; $solrIndexedType ; name-desc ; false
; sortRef5 ; $solrIndexedType ; price-asc ; false
; sortRef6 ; $solrIndexedType ; price-desc ; false
Define the sort fields
INSERT_UPDATE SolrSortField ; sort(indexedType(identifier),code)[unique=true] ; fieldName[unique=true] ; ascending[unique=true]
; $solrIndexedType:name-asc ; name ; true
; $solrIndexedType:name-desc ; name ; false
; $solrIndexedType:price-asc ; priceValue ; true
; $solrIndexedType:price-desc ; priceValue ; false
Update sorts option in Indexed type Product
INSERT_UPDATE SolrIndexedType ; identifier[unique=true] ; type(code) ; variant ; sorts(&sortRefID)
; $solrIndexedType ; Product ; false ; sortRef3,sortRef4,sortRef5,sortRef6
Find detail post here
If you want to know how to add custom Sort By Option with custom AttributeComparator?
来源:https://stackoverflow.com/questions/49512571/hybris-solr-sort