Book Image

WooCommerce Cookbook

By : Patrick Rauland
Book Image

WooCommerce Cookbook

By: Patrick Rauland

Overview of this book

Table of Contents (17 chapters)
WooCommerce Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sorting products from the oldest to the most recent


If the preceding recipe doesn't give you enough options to sort your products, you can add custom code to meet your needs. With a bit of code, you can sort products from the oldest to the most recent.

How to do it…

We'll need to add two pieces of custom code. The first piece of code will add an option to the sorting drop-down.

  1. Add the following code at the bottom of your theme's functions.php file under wp-content/themes/your-theme-name/ or in your custom WooCommerce plugin:

    // Add a new sorting option
    add_filter( 'woocommerce_default_catalog_orderby_options', 'woocommerce_cookbook_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'woocommerce_cookbook_catalog_orderby' ); 
    function woocommerce_cookbook_catalog_orderby( $sortby ) { 
        $sortby['oldest_to_newest'] = __( 'Sort by oldest to newest', 'woocommerce' );
        return $sortby; 
    }

    This code should add a new option to the sorting drop-down.

    Unfortunately, WooCommerce doesn't...