Page 1 of 1

How do we build a Shortcode for Wordpress?

Posted: Tue Mar 26, 2019 6:17 am
by simonmlewis

Code: Select all

// Note: products shortcode docs https://docs.woocommerce.com/document/woocommerce-shortcodes/#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.

Re: How do we build a Shortcode for Wordpress?

Posted: Tue Apr 02, 2019 12:02 am
by Vegan
you might want to talk to the people with woocommerce who might be able to help with their code base