php - 将自定义产品排序设置为默认 Woocommerce 排序选项

标签 php wordpress sorting woocommerce product

在 Woocommerce 中,我使用以下代码按修改日期向商店目录添加自定义排序选项。

add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_modified_date' );
function enable_catalog_ordering_by_modified_date( $args ) {
    if ( isset( $_GET['orderby'] ) ) {
        if ( 'modified_date' == $_GET['orderby'] ) {
            return array(
                'orderby'  => 'modified',
                'order'    => 'DESC',
            );
        }
    }
    return $args;
}

add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_by_modified_date' );
function add_catalog_orderby_by_modified_date( $orderby_options ) {
    // Rename 'menu_order' label
    $orderby_options['modified_date'] = __("Sort by modified date", "woocommerce");

    return $orderby_options ;
}

来自这个答案:Add sorting by modified date in Woocommerce products sort by

我想将该自定义排序选项设置为默认排序选项,将默认的 woocommerce 选项(按菜单顺序)保留为辅助选项。

如何将此排序设置为 woocommerce 默认排序?

最佳答案

您将使用以下略有不同的版本代码,以及一些附加的 Hook 函数:

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

add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_modified_date' );
function add_catalog_orderby_modified_date( $orderby_options ) {
    // Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
    return array(
        'modified_date' => __("Sort by modified date", "woocommerce"),
        'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
    ) + $orderby_options ;

    return $orderby_options ;
}


add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_modified_date' );
function default_catalog_orderby_modified_date( $default_orderby ) {
    return 'modified';
}

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

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并可以工作。

enter image description here

关于php - 将自定义产品排序设置为默认 Woocommerce 排序选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53589711/

相关文章:

php - CodeIgniter 抛出 "The URI you submitted has disallowed characters...",我看不出是什么原因造成的

html - 自定义 Font Awesome 图标在某些浏览器上不再适用于 Wordpress

基于字符串出现的 PHP 自定义排序函数?

css - wordpress中视频文件周围的蓝色边框

mysql - 统计某个类别和月份/年份的已发布帖子数量

python - 根据嵌套键对字典列表进行排序

python - 如何提高 python 中的合并排序速度

php - .htaccess 已被读取但重写 url 不起作用

php - 如何对数据表进行分页以便数据不会在 Laravel 中一起加载?

php - 在 Eloquent 模型上调用 chunk 时,/bootstrap/cache/compiled.php 中出现 BadMethodCallException