Here we learn about how to remove, rename or reorder sorting options on the product archive pages.
Custom code copied and paste into your child theme’s functions.php file.
1. Remove Default Options:
We can use one filter hook ‘woocommerce_catalog_orderby‘.
Below code example for remove default options.
<?php add_filter( 'woocommerce_catalog_orderby', 'myblog_remove_default_sorting_options' ); function myblog_remove_default_sorting_options( $options ){ unset( $options[ 'popularity' ] ); //unset( $options[ 'menu_order' ] ); //unset( $options[ 'rating' ] ); //unset( $options[ 'date' ] ); //unset( $options[ 'price' ] ); //unset( $options[ 'price-desc' ] ); return $options; } ?>
The following list of default sorting options:
- $options[ ‘popularity’ ] – It Sort by popularity.
- $options[ ‘menu_order’ ] – If the custom order is not set – alphabetically.
- $options[ ‘rating’ ] – It Sort by average rating.
- $options[ ‘date’ ] – It Sort by latest.
- $options[ ‘price’ ] – It Sort by price: low to high.
- $options[ ‘price-desc’ ] – It Sort by price: high to low.
2. Rename the Default Sorting Options
we are using the same filter hook ‘woocommerce_catalog_orderby’.
If you rename ‘Sort by price: low to high’ to ‘Sort by price’.
Example:
<?php add_filter( 'woocommerce_catalog_orderby', 'myblog_rename_default_sorting_options' ); function myblog_rename_default_sorting_options( $options ){ $options[ 'price' ] = 'Sort by price'; // rename return $options; } ?>
3. Change the Order of Sorting Options:
we can change the order of sorting options is to redefine ‘$options’ array.
Example:
<?php add_filter( 'woocommerce_catalog_orderby', 'myblog_change_sorting_options_order', 5 ); function myblog_change_sorting_options_order( $options ){ $options = array( 'menu_order' => __( 'Default sorting', 'woocommerce' ), 'price' => __( 'Sort by price', 'woocommerce' ), 'date' => __( 'Sort by latest', 'woocommerce' ), 'popularity' => __( 'Sort by popularity', 'woocommerce' ), 'rating' => 'Sort by average rating', // __() is not necessary 'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ), ); return $options; } ?>
- I set the filter hook priority to 5 , If you set the hook priority to 99999.
- You can set option names directly in your language, without __() function.
hi thank you. can you tell me how add category in this? thank you