按Woocommerce中的DESC订单对产品进行排序

时间:2017-12-04 05:34:26

标签: php wordpress sorting woocommerce product

我在活跃主题的functions.php文件中写了这段代码:

    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 ( 'random_list' == $orderby_value ) {
        $args['orderby'] = 'date';
        $args['order'] = 'desc';
        $args['meta_key'] = '';
    }
    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['random_list'] = 'DESC SORT';
    return $sortby;
}

我正在尝试按描述对产品进行排序,但产品仍默认排序(ASC)

如何通过DESC对它们进行排序?

5 个答案:

答案 0 :(得分:5)

您可以通过这种方式使用隐藏在 add_action( 'woocommerce_product_query', 'default_catalog_ordering_desc', 10, 2 ); function default_catalog_ordering_desc( $q, $query ){ if( $q->get( 'orderby' ) == 'menu_order title' ) $q->set( 'order', 'DESC' ); } 操作挂钩中的自定义函数:

var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function() {
  /*
   * Runs every weekday (Monday through Friday)
   * at 11:30:00 AM. It does not run on Saturday
   * or Sunday.
   */
  }, function () {
    /* This function is executed when the job stops */
  },
  true, /* Start the job right now */
  timeZone /* Time zone of this job. */
);

代码放在活动子主题(或活动主题)的functions.php文件中或任何插件文件中。

经过测试和工作。

答案 1 :(得分:2)

您需要首先确定要在哪个字段中对价格标题进行排序等。但我已经编写了所有可能的方法来使用它,

add_filter('woocommerce_get_catalog_ordering_args','custom_query_sort_args');

function custom_query_sort_args(){

    // Sort by and order
    $current_order = ( isset( $_SESSION['orderby'] ) ) ? $_SESSION['orderby'] : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    switch ( $current_order ) {
        case 'date' :
            $orderby = 'date';
            $order = 'desc';
            $meta_key = '';
        break;
        case 'price' :
            $orderby = 'meta_value_num';
            $order = 'asc';
            $meta_key = '_price';
        break;
        case 'title' :
            $orderby = 'meta_value';
            $order = 'asc';
            $meta_key = '_woocommerce_product_short_title';
        break;
        default :
            $orderby = 'menu_order title';
            $order = 'asc';
            $meta_key = '';         
        break;
    }

    $args = array();

    $args['orderby']        = $orderby;
    $args['order']          = $order;

    if ($meta_key) :
        $args['meta_key'] = $meta_key;
    endif;

    return $args;

}

答案 2 :(得分:1)

此代码应添加到您主题的functions.php

add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');

function custom_default_catalog_orderby() {
     return 'date'; // Can also use title and price
}

答案 3 :(得分:0)

这段代码对我有用。

findIndex

我在这里https://gist.github.com/mikejolley/1622323

找到了它

答案 4 :(得分:-1)

add_action( 'woocommerce_product_query', 'default_catalog_ordering_desc', 10, 2 );
function default_catalog_ordering_desc( $q, $query ){
    if( $q->get( 'orderby' ) == 'date' )
        $q->set( 'order', 'DESC' );
}