Sort the product catalog by product published date in WooCommerce

Sort the product catalog by product published date in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

// Ordering products based on the selected values
function filter_woocommerce_get_catalog_ordering_args( $args, $orderby, $order ) {    
    switch( $orderby ) {
        case 'publish_date':
            $args['orderby']  = 'post_date';
            $args['order']    = 'ASC';
            break;
    }

    return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'filter_woocommerce_get_catalog_ordering_args', 10, 3 );

// Orderby setting
function filter_orderby( $orderby ) {
    $orderby['publish_date'] = __( 'Sort by publish date', 'woocommerce' );
    return $orderby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'filter_orderby', 10, 1 );
add_filter( 'woocommerce_catalog_orderby', 'filter_orderby', 10, 1 );