默认情况下将 Woocommerce 产品目录按字母降序排序 [英] Sort Woocommerce products catalog to alphabetical desc order by default

查看:72
本文介绍了默认情况下将 Woocommerce 产品目录按字母降序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有 Avada 主题的 Woocommerce 中,我尝试使用以下代码按 DESC 顺序按字母顺序对产品进行排序:

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;
}

但它不起作用.默认的 Woocommerce 排序选项(价格、受欢迎程度等) 工作正常.

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

我做错了什么?

推荐答案

以下内容将默认按字母顺序排列您的产品目录:

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' );
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.

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

这篇关于默认情况下将 Woocommerce 产品目录按字母降序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆