Skip to content

Commit

Permalink
Add/jetpack search sort by price default (#35167)
Browse files Browse the repository at this point in the history
* Added price and rating to default sort options for Jetpack Search

* changelog

* [not verified] Tweaks!

* only allow the price sorting option when using the product display

* added changelog entry

---------

Co-authored-by: Jason Moon <jsnmoon@users.noreply.github.com>
  • Loading branch information
robfelty and jsnmoon authored Jan 24, 2024
1 parent d5ff142 commit 51fefaa
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added price and rating to default sort options
2 changes: 1 addition & 1 deletion projects/packages/search/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"link-template": "https://github.com/Automattic/jetpack-search/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.41.x-dev"
"dev-trunk": "0.42.x-dev"
},
"version-constants": {
"::VERSION": "src/class-package.php"
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/search/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetpack-search",
"version": "0.41.2-alpha",
"version": "0.42.0-alpha",
"description": "Package for Jetpack Search products",
"main": "main.js",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/search/src/class-package.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Search package general information
*/
class Package {
const VERSION = '0.41.2-alpha';
const VERSION = '0.42.0-alpha';
const SLUG = 'search';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import classNames from 'classnames';
import useEntityRecordState from 'hooks/use-entity-record-state';
import useSiteLoadingState from 'hooks/use-loading-state';
import useSearchOptions from 'hooks/use-search-options';
import { SERVER_OBJECT_NAME } from 'instant-search/lib/constants';
import { RESULT_FORMAT_PRODUCT, SERVER_OBJECT_NAME } from 'instant-search/lib/constants';
import ColorControl from './color-control';
import ExcludedPostTypesControl from './excluded-post-types-control';
import ThemeControl from './theme-control';
Expand Down Expand Up @@ -56,6 +56,19 @@ export default function SidebarOptions() {
const { isLoading } = useSiteLoadingState();
const isDisabled = isSaving || isLoading;

const sortOptions = [
{ label: __( 'Relevance (recommended)', 'jetpack-search-pkg' ), value: 'relevance' },
{ label: __( 'Newest first', 'jetpack-search-pkg' ), value: 'newest' },
{ label: __( 'Oldest first', 'jetpack-search-pkg' ), value: 'oldest' },
];
if ( resultFormat === RESULT_FORMAT_PRODUCT ) {
sortOptions.push(
{ label: __( 'Rating', 'jetpack-search-pkg' ), value: 'rating_desc' },
{ label: __( 'Price: low to high', 'jetpack-search-pkg' ), value: 'price_asc' },
{ label: __( 'Price: high to low', 'jetpack-search-pkg' ), value: 'price_desc' }
);
}

// TODO: ask the user if they attempt to navigate away from the page with pending changes.

return (
Expand Down Expand Up @@ -89,11 +102,7 @@ export default function SidebarOptions() {
disabled={ isDisabled }
label={ __( 'Default sort', 'jetpack-search-pkg' ) }
value={ sort }
options={ [
{ label: __( 'Relevance (recommended)', 'jetpack-search-pkg' ), value: 'relevance' },
{ label: __( 'Newest first', 'jetpack-search-pkg' ), value: 'newest' },
{ label: __( 'Oldest first', 'jetpack-search-pkg' ), value: 'oldest' },
] }
options={ sortOptions }
onChange={ setSort }
/>
<SelectControl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEntityProp } from '@wordpress/core-data';
import { PRODUCT_SORT_OPTIONS, RELEVANCE_SORT_KEY } from 'instant-search/lib/constants';
import { useMemo } from 'react';

/* eslint-disable react/jsx-no-bind */
Expand All @@ -11,7 +12,7 @@ const VALID_POST_TYPES = global.JetpackInstantSearchOptions.postTypes;
*/
export default function useSearchOptions() {
const [ theme, setTheme ] = useEntityProp( 'root', 'site', 'jetpack_search_color_theme' );
const [ resultFormat, setResultFormat ] = useEntityProp(
const [ resultFormat, setResultFormatRaw ] = useEntityProp(
'root',
'site',
'jetpack_search_result_format'
Expand Down Expand Up @@ -58,6 +59,18 @@ export default function useSearchOptions() {
const setExcludedPostTypes = postTypesArr =>
setExcludedPostTypesCsv( postTypesArr.filter( type => type in VALID_POST_TYPES ).join( ',' ) );

// Add special handling for product -> non-product result format changes.
const setResultFormat = format => {
const previousFormat = resultFormat;
setResultFormatRaw( format );

// If switching from product to non-product and the default sort is product-specific,
// reset to relevance sort.
if ( previousFormat === 'product' && PRODUCT_SORT_OPTIONS.has( sort ) ) {
setSort( RELEVANCE_SORT_KEY );
}
};

return {
color,
excludedPostTypes,
Expand Down
9 changes: 6 additions & 3 deletions projects/packages/search/src/customizer/class-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ public function customize_register( $wp_customize ) {
$id,
array(
'choices' => array(
'relevance' => __( 'Relevance (recommended)', 'jetpack-search-pkg' ),
'newest' => __( 'Newest first', 'jetpack-search-pkg' ),
'oldest' => __( 'Oldest first', 'jetpack-search-pkg' ),
'relevance' => __( 'Relevance (recommended)', 'jetpack-search-pkg' ),
'newest' => __( 'Newest first', 'jetpack-search-pkg' ),
'oldest' => __( 'Oldest first', 'jetpack-search-pkg' ),
'rating_desc' => __( 'Product rating', 'jetpack-search-pkg' ),
'price_asc' => __( 'Price: low to high', 'jetpack-search-pkg' ),
'price_desc' => __( 'Price: high to low', 'jetpack-search-pkg' ),
),
'description' => __( 'Pick the initial sort for your search results.', 'jetpack-search-pkg' ),
'label' => __( 'Default Sort', 'jetpack-search-pkg' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


Comment: Added price as default sorting option for Jetpack Search
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Jetpack Search: Added price as default sorting option
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Allow users to select price as default sorting option for search
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/search/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51fefaa

Please sign in to comment.