Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wordpress - Woocommerce how to sort products by "last modified" date?

So here is my link:

https://snake.cl/shop/?orderby=date

This shows products by date created.

The problem I have is that I update the inventory of products and I would like them to show as if "new" or "now available".

So if I could sort by last modified date, that would work I think.

I had a look at some WP parameters for queries, and tried:

https://snake.cl/shop/?orderby=modified

but that doesn't work.

Any ideas?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should try this for Sort by last modified date order by DESC.

// Apply Sort By Last Modified
add_filter( 'woocommerce_get_catalog_ordering_args', 'woo_add_postmeta_ordering_args' );
function woo_add_postmeta_ordering_args( $args_sort ) {

  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : '';

  switch( $orderby_value ) {
     case 'last_modified':
        $args_sort['orderby']  = 'modified';
        $args_sort['order']    = 'DESC';
     break;
  }
  return $args_sort;
}

// Add "Sort By Last Modified" option in dropdown
add_filter( 'woocommerce_default_catalog_orderby_options', 'woo_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'woo_add_new_postmeta_orderby' );

function woo_add_new_postmeta_orderby( $sortby ) {
    $sortby['last_modified'] = __( 'Sort By Last Modified', 'woocommerce' );
    return $sortby;
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...