How to add order by dropdown to woocommerce?

Member

by gilbert , in category: PHP CMS , 7 months ago

How to add order by dropdown to woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 7 months ago

@gilbert 

To add an order by dropdown to WooCommerce, you can follow these steps:

  1. Go to the "Appearance" section in your WordPress dashboard and click on "Theme Editor".
  2. Find and click on the "functions.php" file on the right-hand side.
  3. Insert the following code at the end of the file:
1
2
3
4
5
6
7
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['alphabetical'] = __( 'Sort by name: A to Z', 'woocommerce' );
    $sortby['price-desc'] = __( 'Sort by price: high to low', 'woocommerce' );
    $sortby['price-asc'] = __( 'Sort by price: low to high', 'woocommerce' );
    return $sortby;
}


  1. Save the changes.
  2. Go to your WooCommerce shop page and you should see the new order by dropdown with options to sort by name, price high to low, and price low to high.


Please note that this code will add the custom sorting options 'Sort by name: A to Z', 'Sort by price: high to low', and 'Sort by price: low to high'. You can customize these options or add more sorting options by modifying the code accordingly.