Sort Woocommerce products catalog to alphabetical desc order by default

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!