Home:ALL Converter>Sort Woocommerce products catalog to alphabetical desc order by default

Sort Woocommerce products catalog to alphabetical desc order by default

Ask Time:2019-01-05T16:48:06         Author:Rango

Json Formatter

In Woocommerce with Avada theme, I am trying to sort products alphabetically in DESC order with the following code:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );

function custom_woocommerce_get_catalog_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 ( 'alphabetical' == $orderby_value ) {
        $args['orderby'] = 'title';
        $args['order'] = 'DESC';
    }

    return $args;
}

add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );

function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['alphabetical'] = __( 'Alphabetical' );
    return $sortby;
}

But it's not working. Default Woocommerce sorting options (Price, popularity etc …) are working fine.

What I am doing wrong?

Author:Rango,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54050418/sort-woocommerce-products-catalog-to-alphabetical-desc-order-by-default
yy