Custom sorting options from woocommerce shop page

Custom sorting options from woocommerce shop page

This example code shows how you can add an order option, re-order option and delete option in shop page woocommerce.

Step 1: Unset to re-order and create new sorting option

function remove_woocommerce_catalog_orderby( $orderby ) {
  unset($orderby["popularity"]);
  unset($orderby["date"]);
  unset($orderby["price"]);
  unset($orderby["price-desc"]);
  $orderby['title'] = __( 'Courses Title', 'woocommerce' );
  $orderby['hours_desc'] = __( 'Sort By Hours (desc)', 'woocommerce' );
  $orderby['hours_asc'] = __( 'Sort By Hours (asc)', 'woocommerce' );
  /* $orderby['price_asc'] = __( 'Sort By Price (asc)', 'woocommerce' );*/
  $orderby["price"] = __( 'Sort By Price (asc)', 'woocommerce' );
  $orderby["price-desc"] =__( 'Sort By Price (desc)', 'woocommerce' );

  return $orderby;
}
add_filter( "woocommerce_catalog_orderby", "remove_woocommerce_catalog_orderby", 20 );

Step 2: Sort product

function custom_woocommerce_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  if ( 'title' == $orderby_value ) {
  $args['orderby'] = 'title';
  $args['order']   = 'DESC';
  }
  if ( 'hours_desc' == $orderby_value ) {
  	$args['order'] = 'DESC';
  	$args['meta_key'] = 'hours';
  	$args['orderby'] = 'meta_value_num';
  }
  if ( 'hours_asc' == $orderby_value ) {
  	$args['order'] = 'ASC';
  	$args['meta_key'] = 'hours';
  	$args['orderby'] = 'meta_value_num';
  }
  return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_ordering_args', 20 );

Final code:

function remove_woocommerce_catalog_orderby( $orderby ) {
  unset($orderby["popularity"]);
  unset($orderby["date"]);
  unset($orderby["price"]);
  unset($orderby["price-desc"]);
  $orderby['title'] = __( 'Courses Title', 'woocommerce' );
  $orderby['hours_desc'] = __( 'Sort By Hours (desc)', 'woocommerce' );
  $orderby['hours_asc'] = __( 'Sort By Hours (asc)', 'woocommerce' );
  /* $orderby['price_asc'] = __( 'Sort By Price (asc)', 'woocommerce' );*/
  $orderby["price"] = __( 'Sort By Price (asc)', 'woocommerce' );
  $orderby["price-desc"] =__( 'Sort By Price (desc)', 'woocommerce' );

  return $orderby;
}
add_filter( "woocommerce_catalog_orderby", "remove_woocommerce_catalog_orderby", 20 );

function custom_woocommerce_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  if ( 'title' == $orderby_value ) {
  $args['orderby'] = 'title';
  $args['order']   = 'DESC';
  }
  if ( 'hours_desc' == $orderby_value ) {
  	$args['order'] = 'DESC';
  	$args['meta_key'] = 'hours';
  	$args['orderby'] = 'meta_value_num';
  }
  if ( 'hours_asc' == $orderby_value ) {
  	$args['order'] = 'ASC';
  	$args['meta_key'] = 'hours';
  	$args['orderby'] = 'meta_value_num';
  }
  return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_ordering_args', 20 );

Copy and paste this code in your theme’s function.php

Comments

comments

Share