Sort Woocommerce products catalog to alphabetical

2019-07-15 04:05发布

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?

1条回答
冷血范
2楼-- · 2019-07-15 04:58

The following will sort by default your products catalog alphabetically:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_get_catalog_ordering_args' );
function custom_get_catalog_ordering_args( $args ) {
    if ( isset( $_GET['orderby'] ) ) {
        // Sort by "menu_order" DESC (the default option)
        if ( 'title_desc' === $_GET['orderby'] ) {
            $args = array( 'orderby' => 'title', 'order' => 'DESC' );
        }
        // Sort by "menu_order" ASC
        elseif ( 'title_asc' == $_GET['orderby'] ) {
            $args = array( 'orderby'  => 'title', 'order' => 'ASC' );
        }
        // Make a clone of "menu_order" (the default option)
        elseif ( 'natural_order' == $_GET['orderby'] ) {
            $args = array( 'orderby'  => 'menu_order title', 'order' => 'ASC' );
        }
    }
    return $args;
}

add_filter( 'woocommerce_catalog_orderby', 'custom_catalog_orderby' );
function custom_catalog_orderby( $orderby ) {
    // Insert "Sort alphabetically (desc.)" and the clone of "menu_order" adding after others sorting options
    return array(
        'title_desc'    => __('Sort alphabetically (desc.)', 'woocommerce'), // default
        'title_asc'     => __('Sort alphabetically (asc.)', 'woocommerce'),
        'natural_order' => __('Sort by natural shop order', 'woocommerce'), // <== To be renamed at your convenience
    ) + $orderby ;

    return $orderby ;
}

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
function custom_default_catalog_orderby( $default_orderby ) {
    return 'title_desc';
}

add_action( 'woocommerce_product_query', 'product_query_sort_alphabetically' );
function product_query_sort_alphabetically( $q ) {
    if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
        $q->set( 'orderby', 'title' );
        $q->set( 'order', 'DESC' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

enter image description here

查看更多
登录 后发表回答