PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Mon Jan 27, 2020 8:15 am

All times are UTC - 5 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Mar 26, 2019 6:17 am 
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
// Note: products shortcode docs ... #section-6
// In order for the snippet to work, you must add the parameter orderby = "stock" to the shortcode
// You can add more parameters to it of course e.g. limit, paginate, etc. It's all in the docs
add_filter('woocommerce_shortcode_products_query', 'bbloomer_sort_by_stock_status_shortcode', 999, 3);
function bbloomer_sort_by_stock_status_shortcode( $args, $atts, $type ) {
    if ( $atts['orderby'] == "stock" ) {
        $args['orderby']  = array( 'meta_value' => 'ASC' );
        $args['meta_key'] = array('_stock_status';
    return $args;

This is a function we found to add 'stock' to the Shortcode "orderby".
Trouble is, we want it to sort by Stock then by Title.

So the page ends up showing it in Alpha order, but with all the 'in stock' coming first.

Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.

PostPosted: Tue Apr 02, 2019 12:02 am 
Forum Regular

Joined: Fri Sep 05, 2008 3:34 pm
Posts: 540
Location: Victoria, BC
you might want to talk to the people with woocommerce who might be able to help with their code base

Legendary is the Only Way to Play!
16 lbs grain to make only 1 lb meat, water for 6 months of showers and 34.1 lbs carbon dioxide
My sites are made with WordPress, which is a content management system which now accounts for more than 60% of all websites

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 11 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group