Add sorting by modified date in Woocommerce products sort by

独自空忆成欢 提交于 2019-12-11 06:54:52

问题


In woocommerce, I would like to add the possibility to sort the products by "Modified date" in shop and archives pages.

How can I add "Sort By date modified " in woocommerce product sorting dropdown?

Any help is appreciated.


回答1:


This can be done very easily with the following code, which will add a sorting by modified date:

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

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



来源:https://stackoverflow.com/questions/53573475/add-sorting-by-modified-date-in-woocommerce-products-sort-by

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